Exceeding TeX’s save_size with auto-multiple-choice
This semester in Calculus II we are doing our common exam with auto-multiple-choice. I have used this in my own sections for a couple of years and I wanted to see how it went on a grander scale.
You never know what’s going to happen when you scale up. After finalizing the exam content, we went to generate the copies. The exam had 18 pages. AMC makes individualized exam copies, each with its own randomization, and puts them in the same PDF. We have seven sections, and we wanted the exam copy numbers to indicate the section. AMC only generates series numbers sequentially. So even though we have only 200 students we needed to generate 750 exams in order to give each student from section n an exam with a three-digit code whose first digit was n. A little arithmetic says the resulting PDF should have 13,500 pages.
The number of pages was not a problem; the resulting PDF was only about 35MB large. The issue was TeX’s save stack. There is a limit to the number of control sequences TeX can reserve. Something in the process was overflowing this stack. I was getting errors over the form “TeX capacity exceeded; save size=80000”. The error only happened at the moment of reading the .aux file for the last time.
Now most of the time when TeX gives you an error like this, it means you’ve done something wrong that’s avoidable. But I thought it might be true that my 13,500 page document really did need more memory.
Googling around I found several posts from frantic users (for instance here and here) saying they need to increase TeX’s memory. In each of the cases that were resolved, the user had just done something wrong like created an infinite loop in their TeX document. Something like \def\a{\a} will do that.
Yet I remained convinced, especially after I found this page on the French AMC wiki about how to increase TeX’s main memory (not save size). After several attempts to edit my texmf.cnf and increase the save size, I could not get any change in behavior. In fact, I could not even get TeX to acknowledge that I had increased the save size. Then I learned that in addition to editing the configuration file, the TeX executables needed to be recompiled to account for the new configuration. I tried running fmtutils --byfmt xelatex (I use the xelatex engine in order to use TrueType fonts), but this didn’t have any effect either. I must have been doing something (else) wrong.
I started looking at the point of stack overflow to see what was raising the problem. It turned out my aux file was getting insanely long with tens of thousands of lines of the form
\pgfsyspdfmark{pgfid12345}{foo}{bar}where foo and bar were large integers. These macros expand to other macros that save the numbers foo and bar as control sequences of their own. Right around pgfid79973 was where TeX barfed (Remember save_size=80000 ?)
I knew that AMC didn’t use the .aux file; just an .xy file it creates on its own. So what if the macros in the .aux file were neutered? I tried putting
\renewcommand{\pdfsyspdfmark}[3]{\relax}in my TeX file preamble. Success! At least, the process was finishing without error. I was not all that confident that ignoring all of those macros was going to work.
Then I got to thinking why those lines would get written that often to the aux file in the first place. There were actually only two tikzpicture environments in the whole document. Actually, no, AMC uses tikz (or maybe pgf, which amounts to the same thing) for each of the bubbles on the document1. There are about 100 bubbles on the whole exam, so on the order of 750,000 tikz/pgf groups. Something that was creating a macro for each of them will overflow the stack by tenfold!
So why was it adding a line to the aux file for each picture? Because I asked it to. The TikZ remember picture key allows named TikZ nodes to be accessible outside the enclosing picture group. This is handy if you want to point from text to a node within a picture.
TikZ’s key configuration mechanism is so flexible it’s easy to do things globally. I looked at my TikZ code, and sure enough, inside a library file I had created, was the line:
\tikzstyle{every picture}+=[remember picture]Because why wouldn’t you want to do that? Now I know why: if you have 750K tikzpictures in the same document!
Rather than change the library file, I negated this setting in my local tex file by invoking \tikzstyle{every picture}+=[remember picture=false] right after loading the library. Now things ran just as expected.
Once again, the moral of the story is: If you think you need to increase TeX’s memory, you probably don’t.
Footnotes
If you opt for round bubbles in your test, rather than square boxes.↩︎