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 want to use foreach to loop through two variables at the same time, in Tikz. I'd like to go through the variable \y from 0 to 7. At the same time, I'd like to go through \yy from 0 to 14, by increments of 2. I'd like to know how to do this without writing out the entire list.

For example, the following code works:

 \foreach \y / \yy in {0/0, 1/2, 2/4, 3/6, 4/8, 5/10, 6/12, 7/14}
   \node at (0,\y) {\y -- \yy};

But I can't seem to get this code to work:

 \foreach \y / \yy in {0/0, 1/2, ..., 7/14}
   \node at (0,\y) {\y -- \yy};

I'm planning on using LOTS of loops like this, so I'm really trying to figure out how to get a double loop with the dots to work. Any suggestions?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

Why not using a single variable and evaluate?

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \y [evaluate=\y as \yy using int(2*\y)] in {0,...,7}
   \node at (0,\y) {\y -- \yy};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Evaluate, from what I've seen, gives values like 2.0 and 4.0. I just want the integer value. But thanks for the idea, +1 for you! By the way, is there a way to get evaluate to give integer values? –  Matt Groff Feb 22 at 3:28
    
@MattGroff Of course, you can truncate the value. –  Gonzalo Medina Feb 22 at 3:28
    
@MattGroff: int(2*\y) –  Heiko Oberdiek Feb 22 at 3:29
    
@MattGroff as Heiko said in his comment, or with \pgfmathroundto. –  Gonzalo Medina Feb 22 at 3:30

Just for fun with fp.

\documentclass[preview,border=12pt,12pt,varwidth]{standalone}
\usepackage{multido,fp}
\begin{document}
\multido{\i=0+1}{10}{\FPeval\temp{round(sin(\i):6)} $(\i,\temp)$\endgraf}
\end{document}

enter image description here

share|improve this answer

Possible also

\documentclass{article}
\usepackage{tikz}    
\begin{document}

\begin{tikzpicture}
\foreach \y in {0,...,7}
{ \pgfmathtruncatemacro{\yy}{2*\y}
   \node  at (0,\y) {\y -- \yy};
   }
\end{tikzpicture}

\end{document}

share|improve this answer

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.