2

I'm attempting to use TikZ to draw three different coordinate frames. I have successfully done it already. However, in an attempt to make my code more readable, I tried breaking it down into a more spread out and sequential manner. The second implementation (shown below) produces a totally different result than the first Below are both blocks of code, the first which works and second that does not. My question is why are these very similar implementations producing different results?

First implemenation

\documentclass[10pt, fleqn]{article}

\usepackage{tikz}
\usetikzlibrary{automata, arrows, positioning, fit, petri}

\begin{document}

\begin{tikzpicture}[ultra thick, scale = 2]

    \foreach \tilt / \ylength / \xlength / \ylabel / \xlabel in { 0/3/4/{y}/{x}, -45/4/1.5/{q'}/{d'}, -60/4/1.5/{q}/{d} } 
    \draw [<->, rotate=\tilt] (0, \ylength) node (yaxis) [above] {\ylabel} |- (\xlength, 0) node (xaxis) [right] {\xlabel};

\end{tikzpicture}


\end{document}

Second Implementation

\documentclass[10pt, fleqn]{article}

\usepackage{tikz}
\usetikzlibrary{automata, arrows, positioning, fit, petri}

\begin{document}

\begin{tikzpicture}[ultra thick, scale = 2]

    %% Define y-axis nodes for rotational reference frames
    \foreach \nodename / \axislength / \axislabel in { {yaxis}/3/{y}, {yaxis}/4/{q}, {yaxis}/4/{q'} }
    \node (\nodename) at (0, \axislength) [above] {\axislabel};

    %% Define x-axis nodes for rotational reference frames
    \foreach \nodename / \axislength / \axislabel in { {xaxis}/4/{x}, {xaxis}/1.5/{d}, {xaxis}/1.5/{d'} }
    \node (\nodename) at (\axislength, 0) [right] {\axislabel};

    %% Connect nodes to form rotational reference frames
    \foreach \tilt / \nodenamey / \nodenamex in { 0/{yaxis}/{xaxis}, -45/{yaxis}/{xaxis}, -60/{yaxis}/{xaxis} }
    \draw [<->, rotate=\tilt] (\nodenamey) |- (\nodenamex);

\end{tikzpicture}


\end{document}

Thanks for all help in advance!

Qrrbrbirlbel
  • 119,821
David
  • 21

1 Answers1

2

Remove the space after the last {xaxis}.

This space is picked up by \foreach so that PGF tries to find a node named {xaxis}␣ (which does not exist):

! Package pgf Error: No shape named {xaxis}  is known.

The same happens to the \axislabels which will be {q'}␣ and {d'}␣ respectively but since these are typeset (and \unskipped) they do not pose a problem.

You will need to have the last element without a preceding space as in

…, -60/yaxis/{xaxis}}

or just as well

…, -60/yaxis/xaxis}

If you put every element on its own line like in

\foreach \a/\b/\c in {
   a/b/c,
   d/e/f,
   g/h/i
}

you will also have a spurious space in the last \c definition: i␣.

This will either need to read

\foreach \a/\b/\c in {
   a/b/c,
   d/e/f,
   g/h/i}

or

\foreach \a/\b/\c in {
   a/b/c,
   d/e/f,
   g/h/i%
}
Qrrbrbirlbel
  • 119,821