Consider the following MWE, reproducing the problem from this question:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{kotex}
\usepackage{amsmath}
\newtheorem{Cvičení}{Cvičení}
\begin{document}
\tracingmacros2\tracingcommands2
\begin{align*}
a = b
\end{align*}
\end{document}
The problem here is the use of special characters as an environment name in \newtheorem. While this was quickly identified, I found the emerging warning and error somewhat obscure:
! Missing \endcsname inserted.
<to be read again>
\protect
l.16 \end{align*}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
and
! TeX capacity exceeded, sorry [input stack size=5000].
\@spaces ->\space
\space \space \space
l.16 \end{align*}
If you really absolutely need more capacity,
you can ask a wizard to enlarge me.
These errors appear without the use of \begin{Cvičení}, but only when kotex is loaded.
I investigated using the \tracing commands in the MWE above and found the relevant part of the log file to be lines 5038 ff:
\@elt #1->\global \csname c@#1\endcsname \the \csname c@#1\endcsname
#1<-CviÄenÃ
{\csname}
Ä#1->\ifcsname u8:\string Ä\string #1\endcsname \csname u8:\string Ä\string #1\
expandafter \endcsname \else \expandafter \unihangul@two@octets \expandafter Ä\
expandafter #1\fi
#1<-
{\ifcsname}
{\string}
{\string}
{true}
{\csname}
{\string}
{\string}
{\expandafter}
{\else}
\u8:Ä ->\IeC {\v c}
\IeC ->\ifx \protect \@typeset@protect \expandafter \@firstofone \else \noexpan
d \IeC \fi
{\ifx}
{true}
{\expandafter}
{\else}
\@firstofone #1->#1
#1<-\v c
\v ->\T1-cmd \v \T1\v
\T1-cmd #1->\ifx \protect \@typeset@protect \@inmathwarn #1\else \noexpand #1\e
xpandafter \@gobble \fi
#1<-\v
{\ifx}
{true}
\@inmathwarn #1->\ifmmode \@latex@warning {Command \protect #1 invalid in math
mode}\fi
#1<-\v
{\ifmmode}
{true}
\@latex@warning #1->\GenericWarning {\space \space \space \@spaces \@spaces \@s
paces }{LaTeX Warning: #1}
#1<-Command \protect \v invalid in math mode
\GenericWarning ->\csname m@gobble\iffirstchoice@ \else 4\fi \endcsname \protec
t \GenericWarning
{\csname}
{\iftrue}
{true}
{\else}
\m@gobble ->
! Missing \endcsname inserted.
<to be read again>
\protect
l.16 \end{align*}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
\GenericWarning #1#2->\begingroup \def \MessageBreak {
#1}\set@display@protect \immediate \write \@unused {
#2\on@line .
}\endgroup
#1<-\space \space \space \@spaces \@spaces \@spaces
#2<-LaTeX Warning: Command \protect \v invalid in math mode
\space ->
\space ->
\space ->
\@spaces ->\space \space \space \space
\space ->
\space ->
\space ->
\space ->
\@spaces ->\space \space \space \space
\space ->
\space ->
\space ->
\space ->
\@spaces ->\space \space \space \space
\space ->
\space ->
\space ->
\space ->
\set@display@protect ->\let \protect \string
{\string}
\v ->\T1-cmd \v \T1\v
What I gathered from this is the following:
The
align*environment causes many counters to be globally updated using\@elt, including\c@Cvičení(which appears as\c@CviÄenÃin the log file).Äis an active character (this seems to be the part thatkotexis responsible for) and eventually results in a\vbeing expanded.Since
\vcannot be used in math mode, LaTeX tries to warn me about that.Then something goes wrong.
\csname m@gobble\iffirstchoice@ \else 4\fi \endcsname \protect \GenericWarningresults in\m@gobblebeing expanded to nothing (despite its name not gobbling anything), but then TeX seems to still want to read a\csnameand complains about\protect(leading to the warning we see).- This happens only this one time, despite this same line of code being expanded over and over again in the coming loop.
LaTeX now proceeds expanding
\GenericWarningand finally tries to write "Command \v invalid in math mode" to the log file by essentially saying\let\protect\stringand then writingCommand \protect \v invalid in math modeto the log file. According to the log file ({string}) this should have worked, but subsequently\vis expanded anyway.This causes an infinite loop that goes on until TeX's memory capacity is exceeded, leading to the final error.
So, my questions are
Why does the issue in 4. only appear this one time and not at subsequent expansions of the same line of code?
Why does basically executing
\let\protect\string \protect\vnot have the desired effect in 5.?