32

I don't understand why this simple (but lengthy) iteration results in TeX capacity exceeded, sorry [main memory size=3000000], if I remove the last % following the call to \DoNothing. I thought that a % was not necessary following the use of macro.

\documentclass{article}
\usepackage{pgfplots}

\newcommand{\DoNothing}[4]{}

\begin{document}
Processing....
\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{}{}{}{}%
                }
            }
        }
    }
}
\end{document}

This runs as is, but not if the last % is removed.

4
  • 11
    Quick "answer": spaces (and the space at the end of the line) are ignored when the line directly ends with a control sequence (macro (really, control word)), or %. Here, the line ends with }, and the space remains. You are then stacking 250000 spaces or so, which is a bit much for TeX. Commented Jun 13, 2011 at 2:17
  • I resolved that error replacing \citep{•} with \citep{?}. I don't know exactly what generated the error.
    – user14204
    Commented May 3, 2012 at 16:31
  • @Elizabeth: This error can be triggered by a lot of different things. I converted your post to a comment here, because it not a direct answer to this question. Feel free to post a question of your own if you need any help yourself. Commented May 3, 2012 at 16:37
  • Related Question: What is the use of percent signs (%) at the end of lines? Commented Sep 24, 2018 at 20:09

1 Answer 1

31

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.

2
  • So, I should have a % after every use of a macro, unless I want a space there? Should I also be putting a % after each closing brace? Commented Jun 13, 2011 at 16:45
  • 1
    @Peter: In ordinary text, there is no need to use % except to write actual comments. For stuff inside a {...} pair, to be safe, you could just put a % at the end of every line, but it is only strictly necessary (for removing spaces) after lines that don't end with a macro name. I will add examples of this to my answer.
    – Ryan Reich
    Commented Jun 13, 2011 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .