Tikz foreach with two variables and the remember option
I don't know if it's possible to use a syntax like remember=\x-\y
but you can write something like this
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\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}
\end{equation}
\begin{equation}
\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{equation}
\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.
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:
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.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: