-
Notifications
You must be signed in to change notification settings - Fork 110
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
Suggestion to comment in the manual on using \ifnum
in paths
#966
Comments
Testing on overleaf.com, each of its texlive versions (ranging from 2014 to 2020) raises errors with the first one being
So this at least is not caused by recent changes. |
Well, the first question that comes to mind is, why are you doing all of this on a single path? If you just use regular nested \documentclass[border=3mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \X in {1,...,6} {
\node[circle,draw] at (\X,0) (\X) {};
}
\foreach \X [evaluate=\X as \Xm using {int(\X-1)}] in {1,...,6} {
\foreach \Y in {1,...,6} {
\ifnum\Y<\X
\ifnum\Y=\Xm
\draw (\Y) edge[->] (\X);
\else
\ifodd\Y
\draw (\Y) edge[->,bend left] (\X);
\fi
\fi
\fi
}
};
\end{tikzpicture}
\end{document} What you are observing is just an expansion problem with nested If you do complicated things an a path make sure that everything is expandable, i.e. just use expandable tests. \documentclass[border=3mm,tikz]{standalone}
\usepackage{etoolbox}
\begin{document}
\begin{tikzpicture}
\path foreach \X in {1,...,6} {%
(\X,0) node[circle,draw](\X) {}%
} foreach \X [evaluate=\X as \Xm using {int(\X-1)}] in {1,...,6} {%
foreach \Y in {1,...,6} {%
\ifnumless{\Y}{\X}{%
\ifnumequal{\Y}{\Xm}{%
(\Y) edge[->] (\X)%
}{%
\ifnumodd{\Y}{%
(\Y) edge[->,bend left] (\X)%
}{}%
}%
}{}%
}%
};
\end{tikzpicture}
\end{document} |
I understand that there are many ways of improving the above code (which is why I wrote "I totally understand that this is a stupid way to get the output, it is just a small example."), and that there are tools for the expansion. Yet there are real-world examples in which the nested |
Using nested \def\X{2}
\edef\foo{%
\ifnum0<\X
\ifnum0=\X
foo%
\else
\ifodd\X
bar%
\fi
\fi
\fi
}
\show\foo Teaching the intricacies of TeX is outside of the scope of the PGF manual in my opinion. There are better resources for learning TeX elsewhere. |
@davidcarlisle Just provided a nice explanation of this issue in the TeX.SX chat: https://chat.stackexchange.com/transcript/message/56505161#56505161 |
If nested |
Fixed in 17a95e4 |
In other words avoid TeX code while TikZ parser is active. |
This can be fixed by making \documentclass[border=3mm,tikz]{standalone}
\usepackage{xpatch}
\makeatletter
\edef\tikz@frozen@relax@token{\ifnum0=0\fi}
\def\tikz@handle@next{%
\afterassignment\tikz@handle\let\pgf@let@token=%
}
\xpatchcmd\tikz@handle@more
{\let\pgfutil@next=\tikz@expand}
{%
\ifx\pgf@let@token\relax
\let\pgfutil@next=\tikz@handle@next
\else
\ifx\pgf@let@token\tikz@frozen@relax@token
\let\pgfutil@next=\tikz@handle@next
\else
\let\pgfutil@next=\tikz@expand
\fi
\fi
}
{}{\fail}
\makeatother
\begin{document}
\begin{tikzpicture}
\path
foreach \X in {1,...,6} { (\X,0) node[circle,draw] (\X) {} }
foreach \X[evaluate=\X as \Xm using {int(\X-1)}] in {1,...,6}
{
foreach \Y in {1,...,6}
{
\ifnum\Y<\X\relax % recommended
\ifnum\Y=\Xm\relax % optional because "(" is not expandable
(\Y) edge[->] (\X)
\else
\ifodd\Y
(\Y) edge[->,bend left] (\X)
\fi
\fi
\fi
}
};
\end{tikzpicture}
\end{document} |
@muzimuzhi PR please |
What's your suggestions about the naming and the location (added in which source file) of |
@ilayn But why? First of all, TikZ does that internally, and second it works. Now the user will get to read
I am sure that everybody will precisely understand what is meant by an "expandable expansion". Luckily we are living in an expanding universe, and the expansion is even accelerating, so this is all fine. (Anyway, now we have one more reason to forgive users for not reading the manual.) |
Because tikz cannot solve all of the world's TeX problems. You have to draw the line somewhere. If they don't read it's on them. |
@ilayn I beg to disagree. I never suggested that TikZ should solve all the problems. All I was suggesting is to inform the reader what they may do to make some |
You are not disagreeing or suggesting. You want Sometimes you have to give in if others don't agree with you. We are dealing with such an obscure case that I am having difficulty to actually keep discussing this. If somebody drills down this much then they should better know what they are doing. The statement at the top of the manual "TikZ is parsing don't do funny stuff" should be enough for all possible edge cases. There is a limited maintainer attention and time that can be spent much more valid issues instead of this. I'm pretty sure all of us here answered hundreds of questions using ifnums or other esoteric stuff back in the day. So they can google it instead of arming tikz parser more and more not-so-much-used features. |
I have amended the wording, “the expansion has to be fully expandable” was indeed misleading. However, I don't think it is reasonable for users to expect that if they pass broken |
@ilayn Just one comment: I personally do not care if this will be part of the manual because I found a way that works for me. The only purpose of this request was to provide users information if they get error messages that they may not understand. All I can do is to make suggestions. And. as a matter of fact there is a number of questions in the Q & A sites asking about
|
This is not a real issue, but a suggestion. It may have to do with a LaTeX kernel change, but I am not really sure (yet I would have thought that this problem wasn't there with some older kernel). Anyway, consider the example
On my updated TeXLive2020 installation this yields an error. (I totally understand that this is a stupid way to get the output, it is just a small example.) According to what I find, these errors can be avoided by adding seemingly useless
\numexpr
"commands". In this example, replacing\ifnum\Y<\X
by\ifnum\Y<\numexpr\X
does the trick.So this "issue" is to propose to add a comment in the manual that informs users about the possibility to potentially fix nested (?)
\ifnum
s by adding\numexpr
in places where they do not seem to be required.The text was updated successfully, but these errors were encountered: