\foreach
strips spaces before each list item, and then collects everything up to the next comma (or the list terminating token) before assigning to the relevant variables.
The \foreach
problem arises in this case because of the extra space given before the closing brace in the list. Consider the following:
\foreach \p/\q in {a/{1,2,3}, b/{4,5,6} }
\foreach \r in \q { (\r) }
This produces:
(1) (2) (3) (4,5,6 )
the same effect is found if there is a space before the comma in the first list item:
`\foreach \p/\q in {a/{1,2,3} , b/{4,5,6} }
\foreach \r in \q { (\r) }
which produces
(1,2,3 ) (4,5,6 )
Note, this effect will also appear if there is a space after the /
when using multiple assignment.
\foreach \p/\q in {a/ {1,2,3},b/{4,5,6}}
\foreach \r in \q { (\p:\r) }
which produces
(1,2,3 ) (4) (5) (6)
By removing these spaces the problems disappear...
\foreach \p/\q in {a/{1,2,3}, b/{4,5,6}}
\foreach \r in \q { (\r) }
which produces
(1) (2) (3) (4) (5) (6)
Should all spaces be stripped automatically? Automatically stripping spaces at the beginning is trivial (\pgfutil@ifnextchar x{...}{...}
is used all over the place in PGF parsing), however it is a relatively expensive operation.
Stripping space after items is hard and would involve character by character parsing, which would be very slow. So, it is easier (and not that inconvenient) just to be careful about spaces.
The other problem is the coordinate specification. Anything that isn't obviously one of the known coordinate systems is assumed to be a node, so 00
, 14
are treated as node names. There are many ways to split this specification up, here is one using the .expanded
key handler:
\documentclass[border=0.125cm]{standalone}
\usepackage{tikz}
\tikzset{
at xy split/.style 2 args={
at={(#1,#2)}
},
a/.style={circle, draw=red},
b/.style={rectangle, draw=blue}
}
\begin{document}
\begin{tikzpicture}
\foreach \n/\lst in {a/{00, 14, 20, 34}, b/{01, 11, 23, 33}} {
\foreach \xy in \lst {
\node[\n/.try, at xy split/.expanded=\xy] {\n};
}
}
\end{tikzpicture}
\end{document}

a
and one forb
. – Henri Menke Oct 30 '13 at 22:03\node (\n)
instead of\node[\n]
? And are nodes with names like00
,14
defined? Otherwise this won't work, as the format(x,y)
is expected. So please add a MWE. In general what you want works, try\foreach \x/\ylst in {1/{2, 3, 5, 7}, 2/{0, 1, 4, 9}} { \foreach \y in \ylst { \node (\x-\y) at (\x,\y) {\x,\y}; } }
– Tom Bombadil Oct 30 '13 at 22:50[\n]
since that'll be the style of the nodes. And the problem is in expansion and bracket removal, not in TikZ. – yo' Oct 30 '13 at 23:1533}
and}
which will get stuffed into the second\lst
. – Qrrbrbirlbel Oct 31 '13 at 0:23