[Tex/LaTex] Fun: How to generate funny/unexpected messages in LaTeX

funterminal-output

Knuth says in "Chapter 6: Running TeX" of the TeXbook:

Error messages can be terrifying when you aren't prepared for them; but they can be fun when you have the right attitude. Just
remember that you really haven't hurt the computer's feelings, and
that nobody will hold the errors against you. Then you'll find that
running TeX might actually be a creative experience instead of
something to dread.


How can we generate funny/unexpected messages in LaTeX?

Here are my two cents:

\documentclass{article}

\newenvironment{foobar}{}{}

\begin{document}

\begin{foobar}
\end{\csname @currenvir\endcsname}

\end{document}

yields:

! LaTeX Error: \begin{foobar} on input line 7 ended by \end{foobar}.

The message comes into being as follows:

  1. Carrying out the \begin-macro—without expanding them—triggers defining a macro \@currenvir from the tokens that form its argument.

  2. Carrying out the \end-macro—without expanding them—by calling the kernel-macro \@checkend{⟨tokens that form the argument of the \end-macro⟩}—triggers defining a macro \reserved@a from the tokens that form its argument whereafter \reserved@a and \@currenvir get compared via \ifx.

    If that comparison yields the \else-branch, then via \@badend{⟨tokens that form the argument of the \end-macro⟩} the error-message ! LaTeX Error: \begin{⟨tokens coming from expanding \@currenvir which in turn was defined from the tokens that form the argumment of the \begin-macro⟩} on input line 7 ended by \end{⟨tokens that form the argument of the \end-macro⟩} is delivered.

  3. In the scenario above the set of tokens forming the argument of the \begin-macro differs from the set of tokens forming the argument of the \end-macro. Therefore the \ifx-comparison, which is triggered by \@checkend, between \@currenvir, whose definition comes from the tokens forming the argument of the \begin-macro, and \reserved@a, whose definition comes from the tokens forming the argument of the \end-macro, yields delivering the error-message via \@badend, whereby
    ⟨tokens coming from expanding \@currenvir which in turn was defined from the tokens that form the argumment of the \begin-macro⟩ and ⟨tokens that form the argument of the \end-macro⟩ expand to the same textual phrase.


\documentclass{article}
\let\elsecopy\else\AtEndDocument{\def\else{\let\else\elsecopy}}
\begin{document}
\end{document}

yields the following error-message:

! LaTeX Error: \begin{document} ended by \end{document}.

In this example, too, you stumble over the \ifx-comparison done by \@checkend when tracking things down:

The \else of the \ifx-comparison done by \@checkend for finding out if the \begin– and the \end-macro were used with the same argument is not taken for something that introduces the branch for the case of the \ifx-condition not being fulfilled.


More funny examples are appreciated. 🙂

Best Answer

> pdflatex funmessage
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./funmessage.tex
LaTeX2e <2021-06-01> patch level 1
L3 programming layer <2021-06-18>
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/02/12 v1.4n Standard LaTeX document class
(/usr/local/texlive/2021/texmf-dist/tex/latex/base/size10.clo))

! LaTeX Error: Missing \begin{document}.

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

l.3 \begin{document}

Note that the compiler stops at \begin{document} saying it's missing.

How is this obtained? Simple!

\documentclass{article}
\renewcommand{\fi}{fi}
\begin{document}
Foo
\end{document}

Never do \renewcommand on commands you don't know about.