# "\end\end" causes "TeX capacity exceeded"

I know this is invalid LaTeX code, but the code \end\end results in the following error:

! TeX capacity exceeded, sorry [input stack size=5000].
\end #1->\csname end#1
\endcsname \@checkend {#1}\expandafter \endgroup \if@e...
l.1 \end\end


Why does this result in "TeX capacity exceeded" when this is simply invalid syntax?

• Welcome to TeX.SX! This is probably the shorter input for getting an infinite loop filling a part of the memory, but of course it relies on the definition of \end in the LaTeX kernel. With only primitives it should be \def\a{a\a}\a Jul 17, 2013 at 15:39

It's not simply invalid syntax it is an infinite loop.

\end\end


is

\end{\end}


so it is the end environment call and it expands its argument

\end is more or less \csname end#1\endcsname so that \end{table} executes \endtable.

so

\csname end\end\endcsname


so we have to expand the \end and get

\csname end\csname end\endcsname\endcsname


which is

\csname end\end\endcsname


and we loop and so on 500 times until you fill the input stack, each time \endcsname is taken as the argument of \end and re-inserted.