Purplelink
← All LaTeX errors

TeX capacity exceeded, sorry

TeX filled one of its fixed-size internal tables and stopped. The name in square brackets says which one. Most of the time the cause is a command that calls itself, not a document that is too big.

What it means

TeX keeps several internal tables with sizes fixed when the distribution is set up: one for the macros it is in the middle of expanding, one for open groups, one for command names, one for macro definitions and the boxes being built, and others. When one fills up, TeX stops at once. This is a fatal error: the build ends and no PDF is written.

The words in square brackets name the table, and the number is its size. The numbers below are the TeX Live 2026 defaults for pdfLaTeX.

The log line

This small file triggers the error. The second definition was meant to add text to \term, but it calls \term itself:

\documentclass{article}
\newcommand{\term}{effect size}
\renewcommand{\term}{\term\ (ES)}
\begin{document}
The \term{} is large.
\end{document}

Compiled with pdfLaTeX (TeX Live 2026), it writes this to the .log file:

! TeX capacity exceeded, sorry [input stack size=10000].
\term ->\term
              \ (ES)
l.5 The \term
             {} is large.
If you really absolutely need more capacity,
you can ask a wizard to enlarge me.


Here is how much of TeX's memory you used:
 427 strings out of 468979
 8157 string characters out of 5454369
 438367 words of memory out of 5000000
 29654 multiletter control sequences out of 15000+600000
 628489 words of font info for 40 fonts, out of 8000000 for 9000
 36 hyphenation exceptions out of 8191
 10000i,0n,38p,154b,36s stack positions out of 10000i,1000n,20000p,200000b,200000s
!  ==> Fatal error occurred, no output PDF file produced!

The line after the message shows the macro TeX was expanding, \term, and its definition split at the point TeX had reached: just after the \term at the start. Each \term starts another one before it finishes, so the stack of unfinished macros grows until it holds 10,000. The l.5 line is the first use in the document. The memory summary that follows shows which table is full: here 10000i out of 10000i, the input stack.

To try a fix without touching your project, save the example as a .tex file and compile it online with the LaTeX to PDF tool.

Why it happens

The causes below are in order of likelihood. Each has a before and an after you can compare with your own source.

1. A command is redefined in terms of itself

Inside \renewcommand, the name always means the new definition, so the command calls itself forever. To extend a command, save a copy of the original under another name first and call the copy. \NewCommandCopy copies any command; for a command without an optional argument, \let\oldterm\term does the same.

Before

\newcommand{\term}{effect size}
\renewcommand{\term}{\term\ (ES)}

After

\newcommand{\term}{effect size}
\NewCommandCopy{\oldterm}{\term}
\renewcommand{\term}{\oldterm\ (ES)}

2. The same mistake inside braces or a group

When the self-call sits inside braces, or inside a command such as \textit that opens a group, TeX runs out of group levels first and the bracket says grouping levels=255. The command named in the log can then be an internal LaTeX one, such as \maybe@load@fontshape, because it was the one opening the group. The fix is the same as for cause 1.

Before

\renewcommand{\term}{\textit{\term}}
% ! TeX capacity exceeded, sorry [grouping levels=255].

After

\NewCommandCopy{\oldterm}{\term}
\renewcommand{\term}{\textit{\oldterm}}

3. A loop that never ends or makes too much

A loop whose end condition is never met, or that defines a new command name on every pass with \csname, runs out of strings, names or save space. Check the end condition, and store data in fewer names.

Before

\loop\advance\n by 1 \expandafter\def\csname item\the\n\endcsname{x}\ifnum\n<2000000 \repeat
% ! TeX capacity exceeded, sorry [number of strings=468979].

After

\loop\advance\n by 1 \expandafter\def\csname item\the\n\endcsname{x}\ifnum\n<200 \repeat

4. A macro that grows without end

A definition that adds to itself on every expansion fills main memory, especially inside \edef, which expands everything at once. Give the macro a way to stop.

Before

\def\a{x\a}
\edef\b{\a}
% ! TeX capacity exceeded, sorry [main memory size=5000000].

After

\def\a{x}
\edef\b{\a}

5. A very large TikZ or pgfplots figure

A plot with many thousands of points can fill main memory in pdfLaTeX even when nothing is wrong with the code. Plot fewer points with pgfplots' each nth point key, externalize the figure so it is built once as its own PDF, or compile with LuaLaTeX, which allocates memory as it needs it. Externalizing needs shell escape, the -shell-escape option to pdflatex.

Before

\usepackage{pgfplots}
% a plot of 200,000 data points

After

\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzexternalize
% compile with: pdflatex -shell-escape main.tex

Raising the limits

The sizes come from texmf.cnf, and they can be raised. That is rarely the right fix. A command that calls itself fills any size, so the error only takes longer to arrive. A changed texmf.cnf also applies only to your own computer, so co-authors and a journal's build system still fail on the same file. Raise a limit only when you know the document is large and nothing in it repeats without end.

How to find the line

Read the line just below the message first. It names the macro TeX was expanding and shows where in its definition TeX had got to. If it is your own command, look at its definition. If it is an internal LaTeX command, look at the l. line for the command of yours that started it.

When you run pdflatex in a terminal without -interaction=nonstopmode, TeX stops at the error and prints a ? prompt. Press Return to carry on past the error, type x to quit, or type h for TeX's own help text, which is the paragraph you see after the l. line in the log. Editors run TeX in non-stop mode, so you get the log and never see the prompt.

What each editor shows:

Related errors

Frequently asked questions

Should I increase TeX's memory in texmf.cnf?
Rarely. When a command calls itself, it fills any limit, so a larger limit only makes the error take longer. A changed texmf.cnf also applies only to your computer, so co-authors and journal systems still fail. Find the recursion first.
Why does the log name a LaTeX command I never used?
The line after the message shows the command TeX was expanding when the table filled. That can be an internal LaTeX command called by your own definition. Look at the l. line for the command of yours that started it.
Does LuaLaTeX avoid this error?
For main memory, often yes: LuaLaTeX allocates memory as it needs it, so a large figure that fails in pdfLaTeX may build. A command that calls itself still fails in LuaLaTeX with the same input stack message.

From the maker of these tools

Writing LaTeX on a Mac?

ModernTex is a native macOS LaTeX studio. It has multi-file manuscript navigation, a PDF preview that stays in sync, BibTeX completion that searches by author or title, and compile errors in plain language. $19.99 once, all updates included, forever.

Requires macOS 14 or later. Free 7-day trial, no account.