LaTeX error · Fatal errors

TeX capacity exceeded, sorry

Quick answer

The bracket names the memory TeX ran out of. "input stack size" or "grouping levels" nearly always mean infinite recursion: a command redefined in terms of itself, such as \renewcommand{\ref}[1]{(\ref{#1})}. Save the original first with \let\oldref\ref and call \oldref in the new definition. "main memory size" or "pool size" can also come from a genuinely huge document or plot; then split the work or use externalized TikZ figures or LuaLaTeX, which allocates memory dynamically.

The error in the log

This is how the message appears in the .log file (and in the compile output). The line starting with l. is the line number where TeX noticed the problem; the real mistake is there or just before it.

! TeX capacity exceeded, sorry [input stack size=10000].
\ref #1->(\ref {#1
                  })
l.5 See Section~\ref{sec:intro}

Why it happens

  • A command redefined to call itself: \renewcommand{\ref}[1]{(\ref{#1})} or \renewcommand{\emph}[1]{\textbf{\emph{#1}}}.
  • Two macros that call each other, or a loop whose stop condition is never reached.
  • A fragile command in a moving argument (section title, caption) that expands into itself, often with old packages.
  • A genuinely large job: huge TikZ or pgfplots figures, very long tables, or thousands of labels (reported as main memory, save size or hash size).

How to fix it

Read the line just under the error: it shows the macro that was expanding, here \ref. If you redefined that command, keep a copy of the original with \let and use the copy inside the new definition.

For memory problems with large figures, externalize them (\usetikzlibrary{external} with \tikzexternalize) or compile with LuaLaTeX, whose memory grows as needed.

Broken: reproduces the error

\documentclass{article}
\renewcommand{\ref}[1]{(\ref{#1})}
\begin{document}
\section{Intro}\label{sec:intro}
See Section~\ref{sec:intro}.
\end{document}

Try this in Octree — free LaTeX editor with AI, compiles in your browser

Fixed: compiles cleanly

\documentclass{article}
\let\oldref\ref
\renewcommand{\ref}[1]{(\oldref{#1})}
\begin{document}
\section{Intro}\label{sec:intro}
See Section~\ref{sec:intro}.
\end{document}

Related errors

Frequently Asked Questions

Can I just increase TeX memory?

Only if the document is really that large. Limits such as main_memory can be raised in texmf.cnf, but an infinite loop will exhaust any amount of memory, so first check whether a command calls itself. "input stack size" almost always means recursion.

How do I redefine a command using its old definition?

Copy it first with \let\oldcmd\cmd, then use \oldcmd in the new definition: \renewcommand{\cmd}[1]{...\oldcmd{#1}...}. For commands with optional arguments use \NewCommandCopy (LaTeX 2020 and later) instead of \let.

Why does this happen with large TikZ or pgfplots figures?

Each figure is built in TeX memory before it is written out. Many plot points can exceed main memory. Reduce samples=, externalize the figures, or compile with LuaLaTeX.

Paste your document into Octree — compile errors get a Fix with AI button

When a compile fails, Octree shows the error next to the line that caused it and offers a one-click fix that you can review before applying. Free, nothing to install.

Fix it in Octree →