4

There is a commit on pgf that removes useless % at the end of lines. This commit appears to have broken pgf-plots. Why is this? User hmenke states that

the fact that it worked previously is a bug.

and provides a fix, but I don't understand what's going on.

% is the marker for comments, so how could an extra % at the end of a line change the behavior of a program? Why did theis extra % cause pgf-plots to work?

1
  • 1
    This fix broke pgfplots because it stopped expanding something that was accidentally expanded previously. Apr 23 at 8:41

2 Answers 2

6

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.

2

The end of the line is in this context space for TeX. When there is space after a number, TeX knows that it is a point to stop reading the number parameter (well, \relax is a stronger option). In the other case, TeX may assume that also the next command is, e.g., a part of the argument.

2
  • Thank you. And how is the % at the end of the line interpreted?
    – usernumber
    Apr 23 at 6:53
  • @usernumber It comments out everything, including the end of the line (which in this case cannot be interpreted as space). Apr 23 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.