6

I'm trying to define a new command to use in a TikZ picture. but it seems I can't work with variables (in this case the \defs) inside the newcommand definition. At least not the way I am doing it right now:

\documentclass{standalone}
\usepackage{tikz}

\newcommand{\drawbox}[3]{
    \def \angle {30}
    \def \xd {{2/3*cos(\angle)}}
    \def \yd {{2/3*sin(\angle)}}
    \def \x {{#1-1+(#2-1)*(\xd)}}
    \def \y {{#3-1+(#2-1)*(\yd)}}

    \draw (\x,\y) -- ({(\x)+1},\y) -- ({(\x)+1},{(\y)+1}) -- (\x,{(\y)+1}) -- cycle;
    \draw (\x,{(\y)+1}) -- ({(\x)+(\xd)},{(\y)+1+(\yd)}) -- ({(\x)+1+(\xd)},{(\y)+1+(\yd)}) -- ({(\x)+1},{(\y)+1}) -- cycle;
    \draw ({(\x)+1},{(\y)+1}) -- ({(\x)+1+(\xd)},{(\y)+1+(\yd)}) -- ({(\x)+1+(\xd)},{(\y)+(\yd)}) -- ({(\x)+1},\y) -- cycle;
}

\begin{document}
\begin{tikzpicture}
    \drawbox{1}{1}{1}
\end{tikzpicture}
\end{document}

This code works fine when I don't use any \def and I substitute the definition of 'angle' in every occurrence of \angle, and substitute the definition of 'xd' in every occurrence of \xd, and substitute the definition of 'yd' in every occurrence of \yd, and substitute the definition of 'x' in ... But of course that produces quite a long and very hard to read newcommand definition.

So what am I doing wrong that I don't seem to be able to do calculations based on my \defs?

7

The whole cube can be drawn in one \draw statement. The calculations can be also be done using the new TikZ library math, either via \tikzmath{...} or key evaulate:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\newcommand{\drawbox}[3]{%
  \draw[
    evaluate={
      \angle = 30;
      \xd = 2/3*cos(\angle);
      \yd = 2/3*sin(\angle);
      \x = (#1)-1+((#2)-1)*\xd;
      \y = (#3)-1+((#2)-1)*\yd;
    },
  ]
    (\x, \y) -- ++(1, 0) -- ++(\xd, \yd) -- ++(0, 1)
      -- ++(-1, 0) -- ++(-\xd, -\yd) -- cycle
    (\x, \y+1) -- ++(1, 0) -- ++(\xd, \yd)
    (\x+1, \y) -- ++(0, 1)
  ;%
}

\begin{document}
\begin{tikzpicture}
  \drawbox{1}{1}{1}
\end{tikzpicture}
\end{document}

Result

Remarks:

  • I have also made the path specification less verbose by removing now unnecessary brackets of different kinds.
  • Also I rearranged the path drawing. Line joins are done using rectangular or obtuse angles to avoid line joins with sharp angles, which stick outside the cube frame.

A variation using the new pic feature of TikZ 3.0 and XYZ coordinates:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\newdimen\unit
\newcommand*{\drawboxinit}{%
  \tikzset{%
    evaluate={
      \unit = 10mm;
      \angle = 30;
      \zx = 2/3*cos(\angle);
      \zy = 2/3*sin(\angle);
    },
    x=\unit,
    y=\unit,
    z={(\zx\unit, \zy\unit)},
    %
    drawbox/.pic={
      \draw
        (0, 0) -- ++(1, 0) -- ++(0, 0, 1) -- ++(0, 1)
        -- ++(-1, 0) -- ++(0, 0, -1) -- cycle
        (0, 1) -- ++(1, 0) -- ++(0, 0, 1)
        (1, 0) -- ++(0, 1)
      ;
    }  
  }  
}
\newcommand{\drawbox}[3]{%
  \path ({#1}, {#2}, {#3}) pic {drawbox};
}

\begin{document}
\begin{tikzpicture}
  \drawboxinit
  \drawbox{1}{1}{1}
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.