# Why can't TeX show us the actual “undefined control sequence”?

This is a question on the internals of the TeX system rather than on how to use it. Every time an undefined control sequence is used in a document, TeX prints the message

undefined control sequence [<code>]


where <code> is usually an excerpt from the code that contains the actual control sequence that caused the error.

Many times, however, it seems TeX cannot give us a precise error messages. It often happens in beamer presentations, for example, where all we get is

undefined control sequence [\end{frame}]


Of course, in this example, \end is defined, and the problem lies in some piece of code inside the environment. This often happens also with undefined control sequences expanded from complicated macros.

I understand that this behavior probably comes from the intricacies of how tokens get expanded in the frame environment, but since TeX itself is expanding the tokens, how hard can it be to print the name of the actual undefined control sequence? This could be immensely useful for debugging purposes.

So is there a technical reason why TeX does not print which is the control sequence which is actually undefined, or is it like this on purpose?

• It is because LaTeX turns off context (showing only the first and last lines IIRC), which in turn is because LaTeX macros are too complicated to make sense to the average user. You can try plain TeX for a while and enjoy a better error-reporting experience (at cost of having to implement all conveniences yourself). If you have a concrete example where you see the sort of behaviour you mean, we can show what the problem is in that case. Mostly, setting \errorcontextlines=100 (or some large number like that) will show all. Please post a concrete example that can be used to illustrate answers. – ShreevatsaR Nov 2 '17 at 3:52
• @ShreevatsaR I would be interested indeed to see an example in which an undefined control sequence error is not reported correctly. Other errors are more difficult for sure, but aren't undefined control sequences always reported accurately? – Alan Munn Nov 2 '17 at 4:32
• Perhaps this is not problem of TeX but this is problem of your UI which hides the important part of the error message. The undefined control sequence is always at the end of the first line of the message immediately after the text "undefined control sequence". What UI you are using? – wipet Nov 2 '17 at 5:28
• @ShreevatsaR no, latex does show the undefined command. sadly this will be texstudio or similar scrambling the error message: sourceforge.net/p/texstudio/feature-requests/1068 – David Carlisle Nov 2 '17 at 7:32
• @DavidCarlisle You're right, I was confusing it with the intermediate lines missing (fewer lines of context). The first and last lines are always shown as I understand, and the undefined control sequence is on the first line. – ShreevatsaR Nov 2 '17 at 14:30

Generally you can find the undefined control sequence if you look in the right place. Here's a simple beamer document:

\documentclass{beamer}
\begin{document}
\begin{frame}
\frametitle{A title}
\foo
\end{frame}
\end{document}


And the error it generates:

./undefined.tex:6: Undefined control sequence.
\beamer@doifinframe ...\frametitle {A title} \foo
\end {beamer@frameslide}
l.6 \end{frame}


The undefined control sequence is the last control sequence in the first line of code reported by the error. This is in fact fairly consistent, and if you ask TeX for help on the error (by entering h in the console), this is exactly what it tells you:

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., \hobx'), type I' and the correct
spelling (e.g., I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.


So you can tell the actual offending undefined control sequence, you just need to know where to look. Unfortunately many editing environments hide or otherwise filter the console output, so perhaps this is why it appears not to report the offending command.

This is not to say that all errors are as easy to diagnose in TeX; they're not, because often the line that the error gets noticed is after the line that causes the error. But for undefined control sequences, this is not the case.

• Oh my... so the editor used to hide all that useful information for all this time? This is the question with the higher (enlightment from the answer)*(stupidity of question)` score I've ever asked or seen anywhere o.o" – gigabytes Nov 3 '17 at 2:56
• Very surprised this got so many up votes though. – gigabytes Nov 3 '17 at 2:56
• :) Yes voting is often inversely proportional to the effort it took to write them. Complex debugging, for example, is underappreciated. But the votes may also reflect the fact that lots of people use a similar editor. – Alan Munn Nov 3 '17 at 3:00