You're right, it's a bit of a shame that the expressions aren't evaluated before the loop is started.
Three approaches:
Use the count=\xi
expression to make the outer loop counter accessible. In this case, the outer loop only has to run from 2 to 4, while the counter will run from 1 to 3, which happens to be the starting point of the inner loop. This solution only works in this special case, though, as soon as you need the inner loop to be larger by a different value, you'll have to resort to something else.
Alternatively, you can parse the expression at the start of the outer loop using \pgfmathtruncatemacro
, which will save the result as an integer, or using the evaluate=\x as \result using <expression>
syntax.
Note that the outer loop actually only needs to run from 1 to 3.
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x [count=\xi] in {2,...,4} {
\foreach \y in {\x,...,4} {
\node at (\xi,\y) {(\xi,\y)};
}
}
\end{tikzpicture}
\begin{tikzpicture}
\foreach \x in {1,...,3} {
\pgfmathtruncatemacro\ystart{\x+1}
\foreach \y in {\ystart,...,4} {
\node at (\x,\y) {(\x,\y)};
}
}
\end{tikzpicture}
\begin{tikzpicture}
\foreach \x [evaluate=\x as \ystart using int(\x+1)] in {1,...,3} {
\foreach \y in {\ystart,...,4} {
\node at (\x,\y) {(\x,\y)};
}
}
\end{tikzpicture}
\end{document}
When using macros in node names, the macros have to be expandable in an \edef
context. \pgfmathparse
is not. So you need to do the computation beforehand and only use the result of it in the node name. One way is to use the evaluate
key on the \foreach
as in the following.
\documentclass{article}
%\url{http://tex.stackexchange.com/q/141259/86}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain=circle placed {at=(\tikzchaincount*30:1.5)},regular/.style={draw,circle,inner sep=0,minimum size=4mm}]
\foreach \i in {0,...,11}
\node [on chain, regular] (\i) {\i};
\foreach[evaluate=\i as \ni using {int(mod(\i+1,12))}] \i in {0,...,11}
\draw [->] (\i) to (\ni);
\end{tikzpicture}
\end{document}
Best Answer
I think all you need to to do is to add an extra
{}
around the expression as the comma is probably confusing the parser.However, I would recommend a slightly different approach and that is to use
pagemathtruncatemacro
(or\pgfmathsetmacro
if you need real number values) instead:Code: