0

I have a simple TeX file like this

\documentclass{article}
\begin{document}

\def\printcsnameof #1{ \escapechar=-1 \string #1 }

\printcsnameof \test

\end{document}

When I compile it the first time, it works well and print test on the page.

However, from the second run, it raises the error

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ->\errmessage LaTeX Error: Missing \protect \begin {document}.

See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help@err@

l.2 g def @abspage@last{1} ?

What is going on?

user202729
  • 7,143
  • I did actually encounter this issue before, but I debugged it myself, and can't find a similar question on the site (searching by the error message) so post here in case someone search for the error message online. – user202729 Dec 30 '21 at 08:30

1 Answers1

0

The issue is that the code modifies the value of escapechar globally, which makes the behavior of the code that runs at the end of the document that writes to the .aux file incorrect.

If you check the .aux file, the content would be

\relax 
gdef @abspage@last{1}

Solution:

  • Modify the code so that it does not modify escapechar globally, e.g.

    \documentclass{article}
    \begin{document}
    

    \def\printcsnameof #1{% {% \escapechar=-1 \string #1% }% }

    \printcsnameof \test

    \end{document}

  • Delete the old .aux file (otherwise you'll keep getting the error message)

  • Recompile the file.

With the corrected file, the .aux file content should be

\relax 
\gdef \@abspage@last{1}

with the \ before gdef and @abspage@last.

user202729
  • 7,143