3

I am using the following code. It compiles fine without the middle foreach statement, but fails with Undefined Control Sequence with it:

\documentclass[border=4pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepgfplotslibrary{colormaps,external}
\usetikzlibrary{calc,3d,arrows,shapes.geometric}

\pgfplotsset{compat=1.9}

\tdplotsetmaincoords{60}{150}%


\begin{document}

\begin{tikzpicture}[tdplot_main_coords]

\begin{axis}[
    axis equal,
    axis lines = center,
    width = 16cm,
    height = 16cm,
    xmin=-30,
    xmax=30,
    ymin=-30,
    ymax=30,
    zmin=-30,
    zmax=30,
    view/h=25,
]


    \foreach \t in {20}% generatrices
      \draw[yellow,thin,dashed] (axis cs: {0.75+0.75*cos(\t)},0,     
 {0.75*sin(\t)})
   --(axis cs: {0.75+0.75*cos(\t)},{-33.0},{0.75*sin(\t)});     


    \draw[yellow,thin,densely dotted] (axis cs: 1.5,-33,0)% lower circle
    \foreach \t in {5,10,...,360}
      {--(axis cs: {0.75+0.75*cos(\t)},-33,{0.75*sin(\t)})}--cycle;

    \draw[yellow,thin,densely dotted] (axis cs: 1.5,0,0)% upper circle
    \foreach \t in {5,10,...,360}
       {--(axis cs: {0.75+0.75*cos(\t)},0,{0.75*sin(\t)})}--cycle;



\end{axis}
\end{tikzpicture}
\end{document}

I added the {} around the nested draw statement. It doesn't seem obvious to me what the problem is.

EDIT: MWE provided (bad practice for not doing so!) with \t limited to one value but still failing to compile.

  • 1
    You have to put ; inside the brace, as the \draw is also inside. – Symbol 1 May 10 '17 at 16:56
  • But it failed before those {} were added also. – stars83clouds May 10 '17 at 16:58
  • But it is a good practice to use braces for \foreach! I personally do not use a single semicolon to terminate both \foreach and \draw. Remember the goto fail bug made by Apple? – Symbol 1 May 10 '17 at 17:09
  • 3
    @stars83clouds Can you please add a minimal code wrapper so we can play with it without having to guess? I get Package tikz Error: Unknown coordinate system 'axis'. – egreg May 10 '17 at 17:17
  • 1
    \draw[yellow,thin,dashed]foreach\t in{20,40,...,360}{(axis cs: {0.75+0.75*cos(\t)},0,{0.75*sin(\t)})--(axis cs: {0.75+0.75*cos(\t)},{-33.0},{0.75*sin(\t)})};. – Symbol 1 May 10 '17 at 17:56
3

If there is only one argument for the loop you can use \pgfplotsinvokeforeach inside axis:

  \pgfplotsinvokeforeach{20,40,...,360}{
    \draw[yellow,thin,dashed]
      (axis cs: {0.75+0.75*cos(#1)},0, {0.75*sin(#1)})
      --(axis cs: {0.75+0.75*cos(#1)},{-33.0},{0.75*sin(#1)});
  }

enter image description here

\documentclass[border=4pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz-3dplot}
\usepgfplotslibrary{colormaps,external}
\usetikzlibrary{calc,3d,arrows,shapes.geometric}

\pgfplotsset{compat=1.9}

\tdplotsetmaincoords{60}{150}%
\begin{document}
\begin{tikzpicture}[tdplot_main_coords]
\begin{axis}[
    axis equal,
    axis lines = center,
    width = 16cm,
    height = 16cm,
    xmin=-30,
    xmax=30,
    ymin=-30,
    ymax=30,
    zmin=-30,
    zmax=30,
    view/h=25,
]

  \pgfplotsinvokeforeach{20,40,...,360}{
    \draw[yellow,thin,dashed]
      (axis cs: {0.75+0.75*cos(#1)},0, {0.75*sin(#1)})
      --(axis cs: {0.75+0.75*cos(#1)},{-33.0},{0.75*sin(#1)});
  }

  \draw[yellow,thin,densely dotted] (axis cs: 1.5,-33,0)% lower circle
    \foreach \t in {5,10,...,360}
      {--(axis cs: {0.75+0.75*cos(\t)},-33,{0.75*sin(\t)})}--cycle;

  \draw[yellow,thin,densely dotted] (axis cs: 1.5,0,0)% upper circle
    \foreach \t in {5,10,...,360}
       {--(axis cs: {0.75+0.75*cos(\t)},0,{0.75*sin(\t)})}--cycle;
\end{axis}
\end{tikzpicture}
\end{document}

Note that pgfplots version 1.9 is really old. Current version is 1.14.

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.