# Tikz foreach with two variables and the remember option

The question is really simple but I am a beginner in the world of tikz.

I have the following example and I am trying to simplify the connections of the nodes.

Here the working example:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}
$$\begin{tikzpicture}[baseline,remember picture, every path/.style={-latex,thick}] \matrix (bruch) [matrix of math nodes,% column sep=.75cm, row sep=0.75cm,nodes={anchor=center},] { \dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\ \dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\ \dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\ \dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\ \vdots & \vdots & \vdots & \vdots &\vdots\\ }; \draw (bruch-1-1) -- (bruch-2-1); \draw (bruch-2-1) -- (bruch-1-2); \draw (bruch-1-2) -- (bruch-1-3); \draw (bruch-1-3) -- (bruch-2-2); \draw (bruch-2-2) -- (bruch-3-1); \draw (bruch-3-1) -- (bruch-4-1); \draw (bruch-4-1) -- (bruch-3-2); \draw (bruch-3-2) -- (bruch-2-3); \draw (bruch-2-3) -- (bruch-1-4); \draw (bruch-1-4) -- (bruch-1-5); \end{tikzpicture}$$
\end{document}


I tried the following foreach construction but it fails.

\foreach \x-\y [remember=\x-\y as \lastx-\lasty (initially 1-1)] in%
(2-1,1-2,1-3,2-2,3-1,4-1,3-2,2-3,1-4,1-5){
\draw (bruch-\lastx-\lasty) -- (bruch-\x-\y);}


How can I simplify the connecting of nodes?

EDIT

The solution of Altermundus works with a the CSV-version (see 1). The csv-version needs a manual installation. So is there a way to get a solution which works without the csv?

(1) CSV-Version from Altermundus.com or from the sourceforge website(it's newer)

-
With regard to the edit, see my comment on Altermundus' answer. –  Loop Space Oct 9 '11 at 19:49

## 3 Answers

Altermundus has given the actual answer to your question, but I'm afraid that I just can't stand all that repetition. Here's a slightly different implementation of the diagram you're drawing.

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
column sep=.75cm,
row sep=0.75cm,
nodes={anchor=center},
execute at empty cell={\node {\dfrac{\the\pgfmatrixcurrentcolumn}{\the\pgfmatrixcurrentrow}};},
]
{
& & & & \ldots \\
& & & & \ldots \\
& & & & \ldots \\
& & & & \ldots \\
\vdots & \vdots & \vdots & \vdots & \vdots \\
};
\foreach \i in {1,...,4}
\foreach \j in {1,...,\i} {
\pgfmathtruncatemacro{\ti}{Mod(\i,2) ? \j : \i - \j + 1}
\pgfmathtruncatemacro{\tj}{Mod(\i,2) ? \i - \j + 1 : \j}
\pgfmathtruncatemacro{\ni}{\j == \i ? \ti + (Mod(\i,2) ? 1 : 0) : \ti - (-1)^Mod(\i,2)}
\pgfmathtruncatemacro{\nj}{\j == \i ? \tj + (Mod(\i,2) ? 0 : 1) : \tj + (-1)^Mod(\i,2)}
\draw (bruch-\ti-\tj) -- (bruch-\ni-\nj);
}
\end{tikzpicture}
\end{document}


Things to note:

1. The contents of the cells are placed automatically using the execute at empty cell key. In the non-empty cells - the dots - this is ignored. In the others, it places the fraction with the numerator and denominator determined by the cell coordinates.

2. The path between the cells is computed using a double loop rather than a single loop with two variables. Within the loop, the current and next cells are computed explicitly, rather than being remembered, but due to this it is very easy to extend the diagram further.

Result:

-
+1 very nice. At the moment I don't understand some parts of your code. But I will read the manual now. –  Marco Daniel Oct 11 '11 at 7:28
@Marco: You should definitely read the manual, but there is a lot of information in it so don't expect to take it all in first go! I'm still finding much that I didn't know. If you have any questions about parts of this code, feel free to ask me in the main chat room. –  Loop Space Oct 11 '11 at 7:37

I don't know if it's possible to use a syntax like remember=\x-\ybut you can write something like this

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}
$$\begin{tikzpicture}[baseline,remember picture, every path/.style={-latex,thick}] \matrix (bruch) [matrix of math nodes,% column sep=.75cm, row sep=0.75cm,nodes={anchor=center},] { \dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\ \dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\ \dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\ \dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\ \vdots & \vdots & \vdots & \vdots &\vdots\\ }; \foreach \x/\y [remember=\x as \lastx (initially 1), remember=\y as \lasty (initially 1) ] in% {2/1,1/2,1/3,2/2,3/1,4/1,3/2,2/3,1/4,1/5}{ \draw (bruch-\lastx-\lasty) -- (bruch-\x-\y);} \end{tikzpicture}$$

$$\begin{tikzpicture}[baseline,remember picture, every path/.style={-latex,thick}] \matrix (bruch) [matrix of math nodes,% column sep=.75cm, row sep=0.75cm,nodes={anchor=center},] { \dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\ \dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\ \dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\ \dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\ \vdots & \vdots & \vdots & \vdots &\vdots\\ }; \draw (bruch-1-1) -- (bruch-2-1); \draw (bruch-2-1) -- (bruch-1-2); \draw (bruch-1-2) -- (bruch-1-3); \draw (bruch-1-3) -- (bruch-2-2); \draw (bruch-2-2) -- (bruch-3-1); \draw (bruch-3-1) -- (bruch-4-1); \draw (bruch-4-1) -- (bruch-3-2); \draw (bruch-3-2) -- (bruch-2-3); \draw (bruch-2-3) -- (bruch-1-4); \draw (bruch-1-4) -- (bruch-1-5); \end{tikzpicture}$$
\end{document}


There is a problem with this code if you use pgf 2.1 . pgf 2.1 CVS is necessary. The next comment is from Andrew Stacey :

The problem appears to be with the initially bit. If you take that out, it works except that it complains about the first values. So if you put \def\lastx{1}\def\lasty{1} just before the \foreach loop then it works. I guess that the TikZ/PGF team are aware of this and fixed it in the CVS.

-
Do you get the correct output with this? I tried the same, but all the arrows are drawn from bruch-1-1, so it doesn't work as it should. –  Torbjørn T. Oct 8 '11 at 16:43
Yes it's the same. With remember pictureyou need to compile twice ! –  Alain Matthes Oct 8 '11 at 16:50
@Altermundus: I have the same problem like Torbjorn. –  Marco Daniel Oct 8 '11 at 17:06
I use this one : pgf.cvs.sourceforge.net/pgf . You can find the version (27/07/2011) on my site : altermundus.com/pages/builds/index.html –  Alain Matthes Oct 8 '11 at 20:09
I just tried this code with PGF2.10. From reading the manual, it ought to work but it doesn't. The problem appears to be with the initially bit. If you take that out, it works except that it complains about the first values. So if you put \def\lastx{1}\def\lasty{1} just before the \foreach loop then it works. I guess that the TikZ/PGF team are aware of this and fixed it in the CVS. –  Loop Space Oct 9 '11 at 19:48

Keeping in mind the bug that was mentioned in Altermundus' nice answer, the following also works. Well, by "works" I mean at least the syntax is accepted by TikZ and the result is the same with the figures given with strange behavior. Hence, by extrapolating, I assume that this would also work after the bug fix.

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}

\begin{tikzpicture}[baseline,remember picture,
every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
column sep=.75cm,
row sep=0.75cm,nodes={anchor=center},]
{
\dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\
\dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\
\dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\
\dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\
\vdots      & \vdots      & \vdots      & \vdots      &\vdots\\
};
\def\lastx{1-1}
\foreach \x [remember=\x as \lastx] in%
{2-1,1-2,1-3,2-2,3-1,4-1,3-2,2-3,1-4,1-5}{
\draw (bruch-\lastx) -- (bruch-\x);}
\end{tikzpicture}

\end{document}


It would be great if one can verify this, since I don't have the CVS version.

EDIT The code above now works with Andrew's fix.

-