TeX capacity exceeded when building repeatable documentation pipelines
0 reputation · 08 Sept 2021, 01:37 UTC
0 reputation · 08 Sept 2021, 01:37 UTC
The goal is to establish a repeatable CI build that compiles the same LaTeX source with pdftex, XeTeX and LuaTeX without encountering the "TeX capacity exceeded" error.
Because each engine imposes different default limits on token stacks, register counts and auxiliary file handling, a preamble that succeeds under XeTeX or LuaTeX may fail under pdftex, and repeated runs can accumulate .aux/.log files that push the token count over the threshold only on later builds.
31275 reputation · 08 Sept 2021, 04:28 UTC
There is no universal "maximum number of auxiliary files" that triggers a capacity error, as TeX limits are primarily based on internal memory pools (main memory, save stack, and token registers) rather than file counts. However, the token count—the total number of expanded macros and characters held in memory—is the primary trigger. When a document grows or a pipeline runs repeatedly without cleaning auxiliary files, the accumulation of cross-references and citations in .aux files can push the engine past its static memory allocation.
The three engines handle memory differently, which explains why a build may succeed in LuaTeX but fail in pdfTeX:
To achieve deterministic builds across all three back-ends without simply masking inefficient macro usage, follow these steps:
.log file immediately following the failure. Look for the specific pool mentioned (e.g., main memory, save stack, or token stack).texmf.cnf file using kpsewhich texmf.cnf. Increase the specific limit identified in the log. For example, to increase main memory:
main_memory = 5000000texmf.cnf are not dynamic. You must run the format generation utility to apply them:
fmtutil-syscfg.aux file accumulation from affecting token counts in repeatable pipelines, ensure your CI script executes a cleanup command (e.g., rm *.aux *.log) before each fresh build cycle.To verify that the limits are applied, you can run a test compilation of a known "heavy" subset of your documentation. If the error persists despite increased limits, the issue is likely an infinite macro loop rather than a resource shortage.
Missing Diagnostic Detail: Please provide the specific "capacity exceeded" message from your .log file (e.g., is it save stack or main memory?). This determines whether you need to adjust memory allocation or refactor recursive macros.
Use comments to ask for clarification. Post a solution as an answer.
31,275 reputation · 08 Sept 2021, 07:03 UTC
Each time TeX reads an .aux file, its contents are tokenised and placed in the main memory pool. The token count grows with the total size of all auxiliary data, not merely with the number of files. Consequently, a pipeline that retains .aux files across runs can steadily increase memory usage even if the source document stays unchanged.
To verify this in a CI job, compare the .log output after a clean build versus after several incremental builds: look for lines like "main memory" or "token stack" and note the incremental increase. Reducing auxiliary file size (e.g., by using \includeonly to limit processed chapters or by periodically cleaning the build directory) directly lowers the token load and helps keep all three engines within their static limits.