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!

  • Your second MWE does not compile for me: Package pgf Error: No shape named {xaxis} is known. But, I believe the problem is that tha \foreach forms a local group. So, any nodes you define inside it are not available outside in the next \foreach. – Peter Grill Sep 24 '13 at 0:14
  • Remove the space after the last {xaxis} which is picked up by \foreach. – Qrrbrbirlbel Sep 24 '13 at 0:25
  • @PeterGrill A node (or coordinate) is a global object. – Paul Gaborit Sep 24 '13 at 5:21
  • @PaulGaborit: Ok, haven't done drawing in a while so am forgetting things quickly :-(. I must have been thinking of the name path global issue as in Is there a way to clear paths previously defined with name path global in TikZ, but that is not a node. – Peter Grill Sep 24 '13 at 5:29
  • As @Qrrbrbirlbel noted you have a spurious space at the end but still your last foreach is \foreach \tilt in { 0,-45,-60}\draw [<->, rotate=\tilt] (yaxis) |- (xaxis); – percusse Sep 24 '13 at 7:56
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%
}

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.