Tx · LaTeX & TikZ

How to Fix “TeX Capacity Exceeded: Input Stack Size=5000”

Published By the 9to5Science editorial team

At a glance

  1. 1. Read the log

    Find the first error and inspect the lines immediately before it for repeated macros, files, or TikZ styles.

  2. 2. Check for recursion

    Look for a macro, style, or input file that calls itself directly or through a chain.

  3. 3. Isolate the trigger

    Comment out parts of the document, then restore them until the smallest failing example remains.

  4. 4. Correct the cause

    Break the recursive cycle or fix the repeated input; increasing stack limits usually hides rather than solves it.

The error ! TeX capacity exceeded, sorry [input stack size=5000]. means TeX ran out of room for its stack of active input sources and expansions. It is usually a sign that something is being processed recursively or nested unusually deeply—not that your automaton simply has too many states.

In a TikZ document, the cause may be in the picture, but it can also be in a macro, style definition, or file included elsewhere in the document. The useful clue is often in the log just before the error: look for the same command, style, or file appearing repeatedly.

What the input stack error means

TeX processes input from the document, included files, and expanded commands. The input stack keeps track of these active levels. If a macro expands into itself, one file repeatedly inputs another, or styles call one another in a cycle, the stack can grow until TeX reports this error.

For example, this macro definition is recursive:

\newcommand{\repeatme}{\repeatme}
\repeatme

Calling \repeatme expands it to \repeatme again, with no stopping condition. A TikZ style can have the same problem if it refers to itself:

\tikzset{state/.style={state, draw}}

Here, applying state asks TikZ to apply state again. This example is especially relevant when using the automata library, which provides a state style.

Find the source of the recursion

Start with the first error in the log, not the last message printed after TeX gives up. Check the surrounding lines for a repeated command or a chain of file names. Then try these steps:

  1. Check custom definitions. Search for macros and TikZ styles that refer to themselves, directly or indirectly. A cycle can involve two definitions: style A invokes style B, and style B invokes style A.
  2. Check file inclusion. Make sure an input file does not include itself, either directly or through another file. For example, main.tex inputs diagram.tex, and diagram.tex accidentally inputs main.tex again.
  3. Reduce the document. Comment out sections or pictures until the error disappears. Restore them in smaller pieces to identify the smallest part that reproduces it.
  4. Show more context. Add \errorcontextlines=999 near the beginning of the document, then compile again. This can reveal more of the expansion context in the log. If needed, use \tracingmacros=1 briefly around the suspected code; macro tracing can produce a very large log, so avoid leaving it enabled for a full document.

A corrected automata example

If you want to customize automaton states, define a separate style that uses the library’s state style rather than redefining state in terms of itself. This complete example compiles with TikZ:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning}

\tikzset{
  highlighted state/.style={state, fill=blue!10}
}

\begin{document}
\begin{tikzpicture}[node distance=2cm, ->]
  \node[highlighted state, initial] (q0) {$q_0$};
  \node[state, right=of q0] (q1) {$q_1$};
  \path (q0) edge node[above] {a} (q1);
\end{tikzpicture}
\end{document}

The custom style highlighted state adds a fill color while still using the existing state appearance. The nodes have distinct names, q0 and q1, and the path connects them with a labeled transition. If the error occurs in a larger picture, compare its style and macro definitions with this pattern: customization should add options to an existing style, not create a cycle.

Why increasing the limit is usually not the fix

The number 5000 is the stack limit reported by the TeX engine in this run; it is not a measure of how many states or lines your diagram may contain. Raising engine capacities may be possible in some TeX installations, but it does not stop a recursive definition. The same error can return at a higher limit, or compilation can fail later for another reason.

A genuinely deeply nested document or macro system can require more stack space, but first confirm that the input is finite and intentional. If the error disappears when you remove one picture, also check whether that picture calls a macro or style that expands repeatedly; diagram size alone is not enough to diagnose the cause.

Common mistakes

  • Treating it as a generic memory error. input stack size points to nested input or expansion; it is different from errors about main memory or a buffer size.
  • Changing an engine setting before inspecting the log. This can conceal an accidental loop instead of fixing it.
  • Checking only the visible TikZ commands. A style, macro, or included file used by the picture may be where the recursion originates.
  • Redefining a library style by calling itself. Use a new style name for additions, such as highlighted state, and apply that style to the nodes.

If a reduced example still fails and contains no recursive definitions, compare the log and the exact code with the PGF/TikZ manual for the installed version; a small reproducible example makes the remaining cause much easier to identify.

References

  1. The PGF/TikZ Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.