-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make an option to turn off repeat behavior of \foreach
when variable values are missing.
#1329
Comments
Maybe what you need here is a way to set default value(s) for last entry/entries, through for example Currently this can mostly be emulated by \documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz{
\foreach
% pgfmath function ifthenelse() cannot be used to compare strings
% ifthenelse("a"=="a", "yes", "no") raises error
[evaluate=\nodecolor using \ifx\nodecolor\x"blue!20"\else"\nodecolor"\fi]
\x/\nodecolor in {1,2,3/pink,4,5} {
\node [fill=\nodecolor, inner sep=7,circle] at (\x, 0) {};
}
}
\end{document} |
Yes, that's the corner case the |
I'd solve this differently: \tikz[color 3/.style=pink]
\foreach \x in {1, ..., 5} % allows ... notation!
\node [fill=blue!20, color \x/.try, inner sep=7pt, circle] at (\x, 0) {}; where you also provide something like \tikzset{
set color/.style={@set color/.list={#1}},
@set color/.style args={#1:#2}{color #1/.append style={#2}}} and then you can use The original notation may be achieved with:
but that loses somehwat of the charm of PGFFor. With \tikz
\foreach[xparser Om] \Options/\x in {1, 2, [pink]3, 4, 5}
\node [fill=blue!20, \Options, inner sep=7,circle] at (\x, 0) {}; (this technically needs \tikz
\foreach[xparser={ O{blue!20} }{{#2}/{#1}}]
\x/\nodecolor in {1, 2, [pink]3, 4, 5}
\node [fill=\nodecolor, inner sep=7, circle] at (\x, 0) {}; for a true default value. |
Brief outline of the proposed feature
The
\foreach
command allows users to specify several variables separated by slashes. The manual gives the exampleThere is also a behavior if some variables are not specified, namely
The manual gives this nice example:
However, I frequently run into situations where I would prefer a different fallback behavior, namely that unspecified variables should be left empty. For example, suppose I want to make a bunch of blue circles, with some getting a special color.
At the moment, one way to do this would be
However, it's annoying having to always have to specify the "default" color
blue!20
. It would be nice to only need to specify it in cases when the color is "special". Ifpgffor
would provide an option to turn off the "repeat" behavior, this would be possible together with anif
, for example:Counterargument to the need for such a
repeat=false
option: the same effect can already be achieved by providing a final end-slash, i.e.,{1/,2/,3/pink,4/,5/}
which would set\nodecolor
to be empty. But this is still a bit annoying, especially if I'm wrapping this code as a macro like\circlelist{1,2,3,4,5}
where the user quite frequently wants none of the nodes to be special.For one way to implement this option, see this diff:
12f1783...DominikPeters:pgf-tikz-html-manual:pgffor-repeat
Usage example
No response
The text was updated successfully, but these errors were encountered: