2

Inspired by an answer I gave here, the question arose how I need to properly define a macro, if I want to pass it to a vector declaration in tikz.

If you want to modify a vector, the syntax used is basically x={(1,-0.5,0)}. If I want to put it in a macro, how has the definition to look like, to not causing problems with the curly brackets?

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{intersections,calc}

\begin{document}

\newcommand{\xvec}{\{(1,-0.5,0)\}}

\begin{tikzpicture}

    \draw[thick, x={(1,-0.5,0)}](-1,0,0)--(1,0,0);
    \draw[thick, x=\xvec](-1,0,0)--(1,0,0);

\end{tikzpicture}

\end{document}
JMP
  • 3,238

1 Answers1

3

There are two problems:

  • \{ and \} are not argument braces.
  • TikZ needs to know, what kind of object follows, thus it cannot be hidden in a macro.

The following example expands first the optional argument of \draw to solve the latter issue. During the definition, the \{ is redefined to replace \{...\} by {...}:

\documentclass[tikz,border=3mm]{standalone}

\begin{document}

\newcommand{\xvec}{\{(1,-0.5,0)\}}

\begin{tikzpicture}

    \draw[thick, x={(1,-0.5,0)}](-1,0,0)--(1,0,0);
    \begingroup
      \def\{#1\}{{#1}}
    \edef\x{\endgroup
      \noexpand\draw[thick, x=\xvec]%
    }\x(-1,0,0)--(1,0,0);

\end{tikzpicture}

\end{document}

Result

If there is some degree of freedom, how xvec is provided, then it could be defined as coordinate, for example:

\documentclass[tikz,border=3mm]{standalone}

\begin{document}
\begin{tikzpicture}

    \draw[thick, x={(1,-0.5,0)}](-1,0,0)--(1,0,0);

    \coordinate (xvec) at (1, -0.5, 0);

    \draw[thick, x=(xvec)](-1,0,0)--(1,0,0);

\end{tikzpicture}
\end{document}
Heiko Oberdiek
  • 271,626
  • The second solution is exactly what I am looking for. Then I can define the vector in the beginning of the figure and then tune the look of the figure by tuning the vector at one central position only. Nice. Thanks – JMP Mar 25 '16 at 13:19