Old version before the fixes you mention.
\def\tikz@expand{%
\advance\tikz@expandcount by -1%
\ifnum\tikz@expandcount<0\relax%
\tikzerror{Giving up on this path. Did you forget a semicolon?}%
\let\pgfutil@next=\tikz@finish%
\else%
\let\pgfutil@next=\tikz@@expand
\fi%
\pgfutil@next}%
What's the problem? When \tikz@expand
is expanded, TeX finds (line breaks just not to produce a too long line)
\advance\tikz@expandcount by -1\ifnum\tikz@expandcount<0\relax
\tikzerror{Giving up on this path. Did you forget a semicolon?}
\let\pgfutil@next=\tikz@finish\else\let\pgfutil@next=\tikz@@expand\fi
\pgfutil@next
and its processing rules require that \ifnum
is expanded because there might be another digit coming along. What happens? Suppose the current value of \tikx@expandcount
is 2. Then the \ifnum
test returns false and TeX sees
\advance\tikz@expandcount by -1\else\let\pgfutil@next=\tikz@@expand\fi
\pgfutil@next
Note that the \advance
instruction is still pending. Now `\else is expanded and TeX sees
\advance\tikz@expandcount by -1\let\pgfutil@next=\tikz@@expand\fi
\pgfutil@next
No digit comes along and the \advance
instruction is finalized. Then TeX proceeds with
\let\pgfutil@next=\tikz@@expand\fi\pgfutil@next
Suppose, on the contrary, that the value of \tikz@expand
is 0. The \ifnum
test will return false, because the \advance
instruction hasn't been finalized yet. The wrong path would be followed! Indeed \pgfutil@next
would become \tikz@@expand
instead of the wanted \tikz@finish
after the error message.
How to correctly write that code?
\def\tikz@expand{%
\advance\tikz@expandcount by -1
\ifnum\tikz@expandcount<0
\tikzerror{Giving up on this path. Did you forget a semicolon?}%
\let\pgfutil@next=\tikz@finish
\else
\let\pgfutil@next=\tikz@@expand
\fi
\pgfutil@next
}
Note that I removed the useless \relax
, which can be simply replaced with a space (the endline). The rule when TeX is looking for a number is that the search for digits proceeds with macro expansion and ends when something unexpandable that's not a digit is found. A space is not a digit and TeX will gobble it (it won't gobble any other token in the same situation).
Using %
characters in other places (where I removed them) denotes poor command of TeX's syntax rules.