TeX interprets all whitespace in the input file as a single "space" character for its purposes, with two exceptions:
Whitespace after the end of a "control word" (a macro such as \macro
but not something like the accent-producing macro \'
whose name is not a word) is ignored;
Anything after a comment is ignored, including even newlines (and the spaces at the beginning of the next line).
In your code, without that %
, the call to \DoNothing
is followed by an unguarded newline which TeX interprets as a blank space. This is true even though \DoNothing
is a macro, since spaces are ignored only directly after control words, and \DoNothing
has four arguments intervening.
Each iteration of the final \foreach
contributes one such space, and the increasingly long string of these spaces is fed back up to previous \foreach
's as their loop bodies. This is not the only space that contributes to that string, since in fact most of your newlines (both after \DoNothing
and also after closing braces) are unguarded, and it looks like this one (being the innermost loop and thus the most damaging) simply pushes the length of the argument to the first \foreach
over the length that TeX is ready to handle.
In your code, the way to get rid of all unwanted spaces is to write
\foreach \i in {1,2,3} {%
\foreach \j in {1,2,3,...,11} {%
\foreach \k in {1,2,...,100}{%
\DoNothing{}{}{}{}%
\foreach \a in {1,2,3,...,9} {%
\DoNothing{}{}{}{}%
\foreach \b in {1,2,3,...,9} {%
\DoNothing{}{}{}{}%
}%
}%
}%
}%
}
In this case, every line should be commented at the end, because none of them end in macro names. Here is a shorter, more illustrative example of when comments help and when they are pointless:
\documentclass{article}
\begin{document}
\def\Optional#1{%
\ifx[#1\relax
\OptionalYes
\else
(No optional argument) #1
\fi
}
\def\OptionalYes#1]{%
(Optional argument: #1)
}
\Optional text
\Optional[arg] text
\end{document}
(This is not how optional arguments are usually parsed.) There are two lines not currently ending in %
or a macro name, and each one produces an obvious visual error in the respective test cases. If you put %
directly after the text of those lines, the errors vanish. The other lines without %
are okay as is, because TeX ignores the whitespace after the macro names ending them.
%
. Here, the line ends with}
, and the space remains. You are then stacking 250000 spaces or so, which is a bit much for TeX.\citep{•}
with\citep{?}
. I don't know exactly what generated the error.