Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am new to Tikz and have been largely following the PGFManual to learn about how to use it correctly. The PGFManual shows examples of using foreach but they do not show how to pass it to a command. The code I am writing populates space with tetrahedra to define a pyrochlore lattice. I declare commands to draw a tetrahedron given a specific coordinate.

\newcommand{\tetra}[4]{
    \coordinate (A#1) at (#2+0.00,#3+0.00,#4+0.00);
    \coordinate (B#1) at (#2+0.00,#3+0.25,#4+0.25);
    \coordinate (C#1) at (#2+0.25,#3+0.00,#4+0.25);
    \coordinate (D#1) at (#2+0.25,#3+0.25,#4+0.00);
}

\newcommand{\drawtetra}[1]{
    \draw[lf line, opacity=1] (A#1)--(B#1)--(C#1)--cycle;
    \draw[df line, opacity=1] (A#1)--(B#1)--(D#1)--cycle;
}

and then call them in the document to print out my desired tetrahedra

\tetra{1}{0.00}{0.00}{0.00}
\tetra{2}{0.00}{0.50}{0.50}
\tetra{3}{0.50}{0.00}{0.50}
\tetra{4}{0.50}{0.50}{0.00}

\drawtetra{1}
\drawtetra{2}
\drawtetra{3}
\drawtetra{4}

I would like to automate the process by doing something like

foreach \i in {1,...,4}
    \drawtetra{\i}

This gives a slew of errors that mostly have to do with endcsname

I have not found any solution for this online although the closest relation I could find used pgfkeys unfortunately I was unable to relate it back to my own program. Any help would be appreciated. The complete code to generate the image is given below:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{tikz-3dplot} %mentioned by Kevin C in comments
\usetikzlibrary{arrows}  %mentioned by Kevin C in comments

\newcommand{\tetra}[4]{
  \coordinate (A#1) at (#2+0.00,#3+0.00,#4+0.00);
  \coordinate (B#1) at (#2+0.00,#3+0.25,#4+0.25);
  \coordinate (C#1) at (#2+0.25,#3+0.00,#4+0.25);
  \coordinate (D#1) at (#2+0.25,#3+0.25,#4+0.00);
}

\newcommand{\drawtetra}[1]{
  \draw[lf line, opacity=1] (A#1)--(B#1)--(C#1)--cycle;
  \draw[df line, opacity=1] (A#1)--(B#1)--(D#1)--cycle;
}

\begin{document}
  \tdplotsetmaincoords{110}{70}
  \begin{tikzpicture}[
    scale = 12,
    axis/.style={very thick, ->, >=stealth'},
    df line/.style={thick, fill=darkgray!60, opacity=1},  %darkface
    lf line/.style={thick, fill=gray!30, opacity=1},      %lightface
    tdplot_main_coords
    ]

    \draw[axis] (0,0,0) -- (0,0,1);
    \draw[axis] (0,0,0) -- (0,1,0);
    \draw[axis] (0,0,0) -- (1,0,0);

    \tetra{1}{0.00}{0.00}{0.00}
    \tetra{2}{0.00}{0.50}{0.50}
    \tetra{3}{0.50}{0.00}{0.50}
    \tetra{4}{0.50}{0.50}{0.00}

    \drawtetra{1}
    \drawtetra{2}
    \drawtetra{3}
    \drawtetra{4}
  \end{tikzpicture}
\end{document}
share|improve this question
    
Your code compiles if you add \usepackage{tikz-3dplot} (instead of \usepackage{3dplot}) and \usetikzlibrary{arrows}. But I don't know if the result is what you wanted –  Kevin C Jun 11 at 16:04
    
Thank you for the notice. Mine won't compile when I use the \usepackage{tikz-3dplot} instead of \usepackage{3dplot} but I assume this is because the folder was named 3dplot after the extraction. Oddly enough tough I do not need arrows. However since others will not be able to compile I shall change them as you suggested. Once again, thank you for the notice. –  MrBrightside Jun 11 at 16:09

1 Answer 1

up vote 4 down vote accepted

You were just missing a semicolon. Semicolons are very important in TikZ. In fact, one could say that TikZ is less like LaTeX and more like C or Java in this respect. The following works:

\foreach \i in {1,...,4} 
  \drawtetra\i;

You could alternately use a group, then you don't need the semicolon.

\foreach \i in {1,...,4} { 
  \drawtetra\i
}
share|improve this answer
    
Brilliant thank you so much for pointing this out. –  MrBrightside Jun 11 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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