What is the maximum nesting depth of \input?

The maximum number of nested input files is limited to 127. tex.web contains:

@<Check the ``constant'' values for consistency@>=
bad:=0;
...
if max_in_open>=128 then bad:=6;

Thus this limit is hard-coded into the source, which is inherited by pdfTeX, LuaTeX, and XeTeX.

The second limitation is the (environment) variable max_in_open. texmf.cnf of TeX Live sets it to 15:

max_in_open = 15        % simultaneous input files and error insertions,

The default value for MiKTeX is 50, tested with MiKTeX-pdfTeX 2.9.5670 (1.40.16).

This value can be increased to 127, example for bash shell:

max_in_open=127 tex test

Higher values can be set in TeX Live, then the error message occurs at run time, when they are tried to be used. MiKTeX complains right at the beginning of the TeX run:

tex.exe: Bad parameter value.
tex.exe: Data: max_in_open

The TeX error message (TeX Live), when the limit is hit is:

! TeX capacity exceeded, sorry [text input levels=127].

Short test file for plain TeX format follows. It reads itself until \maxinputlevel is reached (or an error happens).

% test.tex
\def\maxinputlevel{150}
\ifx\inputlevel\undefined
  \csname newcount\endcsname\inputlevel
  \inputlevel=0
\fi
\advance\inputlevel 1
\immediate\write16{Input level: \the\inputlevel}
\def\maxinputlevel{150}
\ifnum\inputlevel < \maxinputlevel\relax
  \input \jobname
\fi
\end

In practice, it is very hard to reach these levels, even for complex documents. See the following example, which consumes one input level for each section level and some more:

  1. Main job file.
  2. Part
  3. Chapter
  4. Section
  5. Subsection
  6. Subsubsection
  7. Paragraph
  8. Subparagraph
  9. Figure
  10. Subfigure
  11. Some TikZ-Drawing
  12. Whatever

There are still some input levels left.

The most likely reason for hitting the error is an error in the TeX code like some infinite nesting loop.


The limit is stated in module 328 of tex.web

enter image description here

Module 11 sets this maximum to 6

enter image description here

However, this limit can be modified by each implementation; in TeX Live the relevant line of texmf.cnf is line 747

max_in_open = 15        % simultaneous input files and error insertions, 

Module 14 of tex.web tells us that the maximum value is 127:

enter image description here

So if you need a particular application where more than 15 files have to be simultaneously be open for input, you can change the value at runtime or in the top level texmf.cnf file.

This log file, obtained by calling a file that recursively does \input doesn't stop for error, when modifying the value of the relevant variable

max_in_open=32 pdflatex test

Here's the log, which shows we can arrive deeper than 15 (in this case 18).

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 81 language(s) loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo)) (./test.aux)
(./1.tex (./2.tex (./3.tex (./4.tex (./5.tex (./6.tex (./7.tex (./8.tex
(./9.tex (./10.tex (./11.tex (./12.tex (./13.tex (./14.tex (./15.tex (./16.tex
(./17.tex (./18.tex)))))))))))))))))) [1{/usr/local/texlive/2015/texmf-var/font
s/map/pdftex/updmap/pdftex.map}] (./test.aux) )</usr/local/texlive/2015/texmf-d
ist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on test.pdf (1 page, 9731 bytes).
Transcript written on test.log.

The same file chokes if called with the standard value:

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2016/02/01>
Babel <3.9q> and hyphenation patterns for 81 language(s) loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size10.clo)) (./test.aux)
(./1.tex (./2.tex (./3.tex (./4.tex (./5.tex (./6.tex (./7.tex (./8.tex
(./9.tex (./10.tex (./11.tex (./12.tex (./13.tex (./14.tex
! TeX capacity exceeded, sorry [text input levels=15].