LaTeX debugging strategies and brace mismatching

The log file usually gives some help. For example if you start an environment without ending it then you get

! LaTeX Error: \begin{quote} on input line 16 ended by \end{document}.

which tells you where group started, if it doesn't tell you where it should have finished.

If you have mis-matched { } pairs it depends whether they are being used for grouping or argument delimiting.

In the former case you get

(\end occurred inside a group at level 1)

which tells you something is bad and if at the start of the document you put

\tracinggroups=1

you get in addition

### simple group (level 1) entered at line 16 ({)
### bottom level

which tells you where the { was.

If it is argument matching ie you have \textbf{ with no } then you get:

Runaway argument?
{ \par aa bb xx cc \par \par \par \par 4 $\hat {\hat {\mathcal {A}}} \ETC.
! File ended while scanning use of \textbf .

which gives you some context, but not a line number, although if you have the tracinggroups set as above it is easy to guess as you are told the last group that was entered or left before the error, in this case the log shows

{leaving math shift group (level 1) entered at line 14}
)
Runaway argument?

so it isn't too hard to spot the error on line 16 as line 14 was OK and line 15 (in this case) was empty.


the answers so far address problems in a single-file job. things get a bit more complicated if there are multiple files processed with \include or \input. when that happens, and there is a report that (\end occurred inside a group at level 1), if a line number is given, you don't know what file that line is in.

i use emacs, and take advantage of the fact that one can ask how-many for a file. first, get a list of the files being included. then, go into them one by one. ask how many { and then how-many }. if the counts don't match, you've potentially found the problem file. if you're lucky, your warning included a line number, so use it. if not, go halfway down the file, and (preferably at a "clean" location like a paragraph or section break) repeat the how-many mantra. this is usually enough to localize the problem well enough to inspect the text by eye.

another thing to check is matching counts of \begin{ and \end{ -- the open brace is important, since some users define their own \beginxxx commands, and you probably don't want to check those, at least in the first cut.

alternatively, use the approach of inserting \end{document} at various points in the problem file until the warning goes away, then eyeball the intervening text for the problem. (\endinput also works, although it's a primitive, not a latex command. this is actually more useful than \end{document} when searching an \included or \input file, as it affects only that particular file.)

yet another approach is to comment out \include and \input statements, starting with the last one; when the warning disappears, you've found the problem file, and can then use one of the other tactics inside that file.

(i used the 'how-many` approach on a 15-chapter book a few days ago, and found the problem in less than 10 minutes; it was in chapter 3, so i was lucky.)


I had similar problem and wanted to go through all pairs, but I got lost in the middle.

l.129 \input{chap_models/main.
I suspect you have forgotten a }', causing me..

so I wrote small script in python: https://github.com/sacherus/latex_tools/blob/master/para_parser.py

main_simple.tex:

\begin{document}
Lorem ipsum.
Don't bother %{}}
Don't bother2 \{
\enddocument}

run: $ ./para_parser.py -i main_simple.tex

output: Input file is main_simple.tex Found redundant parenthesis in ('}', 5)

So there is redundant parenthesis in line 5.

Tags:

Debugging