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:
Carrying out the
\begin-macro—without expanding them—triggers defining a macro\@currenvirfrom the tokens that form its argument.Carrying out the
\end-macro—without expanding them—by calling the kernel-macro\@checkend{⟨tokens that form the argument of the—triggers defining a macro\end-macro⟩}\reserved@afrom the tokens that form its argument whereafter\reserved@aand\@currenvirget compared via\ifx.If that comparison yields the
\else-branch, then via\@badend{⟨tokens that form the argument of thethe error-message\end-macro⟩}! 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 theis delivered.\end-macro⟩}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 theexpand to the same textual phrase.\end-macro⟩
\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. :-)
\let\oldpar=\par \def\par{\oldpar And physics sucks.\oldpar}. – Gaussler Jul 02 '21 at 09:48\oldparand not\endgraf(or\@@par) ? – Ulrich Diez Jul 02 '21 at 11:18;-)– Gaussler Jul 02 '21 at 11:21;-). I have often been thinking of creating a topic about how you can tease your LaTeX collaborators during group projects. Examples could be stuff like what I just mentioned, but also e.g. LuaTeX replacement of their names by Donald Duck or whatever. – Gaussler Jul 02 '21 at 12:00