How to Debug “Too Deeply Nested” error

I see that numerous people have asked a similar question. At the risk of annoying you LaTeX experts, the problem is, as I have expressed before, that LaTeX is a truly awful language. Having said that, someone who is a reasonably competent programmer in Java/C++/Perl/Python etc ought to be able to debug a program that stops working.

Currently, the only way I have to debug a failure is to either quickly undo if I have luckily only written a few lines, or to comment out blocks of code until I identify the region where the problem is. This is completely unacceptable. If that's the only way, then the language is at fault. I am looking for any help in how to find an error WITHOUT having to edit hundreds of lines.

I compile my code and the console given an error on line 709:

\materials
\begin{itemize}
\item photoresistor (3k)
\item resistor (2.2\kohm)
\end{itemize}


The \materials command is defined as:

\newcommand{\materials}{{\bf Materials:}}


The item with photoresistor is the one giving the error. Presumably this means that there are unclosed \begin{itemize} commands before?

Q1: What are the possible causes of this error. Could there be any OTHER unclosed entity causing it? What about \begin{enumerate} ?

Q2: How can I find it, aside from searching every \begin{itemize} and manually looking for the matching \end{itemize}

Q3: Is there any code I can write to assert that everything must be closed, or to display the location of any open \begin? This would go a long way towards solving this problem!!!

• “Too deeply nested” means you are starting a seventh level nested list. I've seen this caused by improper usage of \quote or similar; are you perhaps using \flushleft or \flushright? – egreg Jun 14 '16 at 13:24
• The language isn't really at fault if it gives errors on bad input. the standard classes only allow itemize to be nested 6 times, so it is impossible to debug that from the fragment shown, perhaps you have that many nested lists or perhaps you have a bad definition generating lists to arbitrary depth. note \bf is deprecated and not defined by default (although unrelated to your error), there is lots of additional traing you can turn on in the log \tracinggroups=1 for example will give more info on groups, \tracingall gives more info on everything – David Carlisle Jun 14 '16 at 13:34
• one debugging approach you can take is to place \end{document} just before the cited \materials line, and run the job. at the end of the log, it will tell you at what "level" you finished on, if it's not zero. always look for this in a log, and close any open levels before adding more to the file. it will save you much grief later on. – barbara beeton Jun 14 '16 at 13:44
• In the emacs editor using AUCTeX you can type C-c ] to close the current environment, and so discover which environments are currently open. – Andrew Swann Jun 14 '16 at 14:30
• It turns out that a command I thought was closing was not closing, but the question here is not about the error, but rather how to find it. – Dov Jun 14 '16 at 15:58

If you suspect that you have unclosed groups then let TeX get to the end, or if other errors stop tex or obscure the problem, add \stop at a point that you want to test, and look at the terminal output or log.

So for example

\documentclass{article}

\begin{document}

aaa

\begin{itemize}
\item xxx
\item xxx
\end{itemize}

bbb

\begin{itemize}
\item xxx
\item xxx
%oops\end{itemize}

bbbb

\begin{itemize}
\item xxx
\item xxx
\end{itemize}

\stop

more stuff here

other \ERROR we are not concerned about


If you run the above, TeX stops at the \stop and the terminal output shows

(\end occurred inside a group at level 1)

### semi simple group (level 1) entered at line 14 (\begingroup)


so the level 1 means that you have exactly one extra open group and the second line tells you that it's a semi simple group, which means it is started with \begingroup not { so in latex most likely started by \begin

then it tells you it started on line 14 which is exactly the

\begin{itemize}


line for the list with the missing \end.

• The error message you get with \end{document} instead of \stop is even more informative: \begin{itemize} on input line 14 ended by \end{document}. – egreg Jun 14 '16 at 23:33
• @egreg that makes it too easy:-) – David Carlisle Jun 15 '16 at 6:48