The shape of an error, read left to right
Every TeX error has the same skeleton. A line beginning with ! states what the parser could not do. The following lines echo the input around the point of failure, split into pieces by markers in angle brackets. The last line shows l.number followed by the input TeX was reading. That is the whole format; there is no hidden prose and no stack trace, because TeX has no idea what you were trying to achieve.
The three markers are the useful part. <to be read again> is the token TeX had just picked up and did not expect. <argument> is the material being fed to a macro when things went wrong, so it tells you which macro. <recently read> is what TeX looked at when it gave up. If you read only the ! line you will guess; if you read the markers you usually know.
The line number is where TeX noticed, not where you erred, and the gap is systematic. TeX reads a paragraph as a stream of tokens and only knows a group is unclosed when it hits something that cannot be in it. So a missing } on line 30 is reported on line 58, at the next \end{document} or blank line. When the number does not match your intuition, move upwards in the file and look for an unbalanced brace or an environment that was never closed.
Inside brackets the log also tells you which file it is in: (./chapter3.tex appears as an opening parenthesis and the matching ) marks the return. That is how you find out whether the error is in your preamble, in an \input file or in a class you did not write. Add -file-line-error and the same information arrives in the parseable form chapter3.tex:42: Undefined control sequence.
The question mark prompt, and why your editor hides it
When TeX hits an error in interactive mode it prints ? Enter type return to continue, other keys for possibility of scrolling and waits for a keystroke. If that run is inside an editor, the editor shows nothing new and the build appears to hang. On Windows this is the moment people kill the process and assume LaTeX crashed.
Four keys matter. Return takes the error recovery and carries on, which is usually what produced the half-broken PDF you are looking at. H prints help text about the error. I lets you type tokens to insert at that point, and R means "ignore the rest of this error and just go" — the fastest way past a message you already understand. X exits, Q quits without further output.
For any non-interactive run, pass -interaction=nonstopmode. The engine then records errors in the log and keeps going, so you get a PDF plus a complete list of complaints rather than a frozen process. -interaction=batchmode is quieter and useful in scripts; -halt-on-error is the opposite trade and is right in continuous integration, where a half-built PDF is worse than no PDF.
If your editor swallows the log, find the .log file next to the source and open it in a text editor. It is plain text and it is long: search downward for the first !, and read the two sections below before you conclude anything from the messages after it.
# Non-interactive runs: the engine must never stop and wait for a keypress.
pdflatex -interaction=nonstopmode -file-line-error -halt-on-error thesis.tex
# -file-line-error -> "thesis.tex:42: Undefined control sequence."
# which is what editors parse to make a clickable link.
# -halt-on-error -> stop at the first error instead of writing junk.
# -draftmode -> run without writing a PDF: fast syntax checking.
# -recorder -> write thesis.fls, the exact list of files read.
# latexmk drives the whole loop and reports the interesting lines for you:
latexmk -pdf -silent -interaction=nonstopmode thesis.tex
latexmk -g -pdf thesis.tex # force a rebuild, ignore timestamps
latexmk -c # delete the intermediates
# Only the errors, from the log, once the run is over:
grep -n "^!" thesis.log
grep -n "^Overfull\|^Underfull" thesis.log
grep -n "Warning" thesis.log | head -30
Undefined control sequence: the number one
! Undefined control sequence. means TeX met a backslash-name it has no definition for. It is not a syntax error and it says nothing about your intentions; it is a lookup miss.
Three causes, in decreasing order of frequency. The command belongs to a package you never loaded: \text, \intertext and aligned need amsmath; \mathbb and \setminus need amssymb; \textbf is fine but \sout needs ulem, and \sout also collides with strikethru in older templates. Second, a typo: \alpa for \alpha, \eqref without amsmath or hyperref, or \dow for \dot. Third, a command your class defines and yours does not — a template whose \keywords{...} or \affiliation{...} only exists in the shipped .cls.
The recovery is mechanical. Take the command name from the <recently read> line, look it up in the Comprehensive LaTeX Symbol List or on CTAN, and see which package owns it: tlmgr search --global --file "/amsmath.sty" on TeX Live, the package search in the MiKTeX Console on that distribution. If you cannot tell which package a name belongs to, search the installed tree for the definition itself — texmf-dist/tex/latex/*/*.sty with a plain grep for \newcommand\... finds it faster than guessing, and the file that answers is the one your preamble is missing.
A special case worth recognising: the error appears before \begin{document}, or the context line reads <write>. That means the preamble itself failed part-way through, so every command defined by the package that blew up is now missing. Fix the earliest message in the log and the whole cluster goes with it.
a_{1} + a_{2} = \sum_{i=1}^{2} a_{i}
Missing $ inserted, and the catcode family
! Missing $ inserted. means you used a maths-only character outside maths mode. The characters with a special meaning in text are # $ % & _ ^ ~ \ { }, and eight of them are exactly the ones that appear constantly in scientific prose.
The usual triggers: x_1 written outside $...$, a filename like my_data.csv typed as plain text, a percent sign in 50% (which is not an error but silently comments out the rest of your line), an ampersand in R&D, and a hash in a C# identifier or a #define. TeX is being helpful here: it inserts the $ for you and then keeps going, which is why you often get a garbled page and a dozen follow-on errors instead of a clean stop.
The fixes are a short list you should memorise. Wrap maths in $x_1$ or \(x_1\). Escape the character: \%, \&, \#, \_, \{. Or declare it literal: \verb|my_data.csv| for code fragments, \url{...} from hyperref for web addresses (where _ and % pass through correctly), and \begin{verbatim}...\end{verbatim} for a block.
The companion message is ! Misplaced alignment tab character &. — that is an ampersand outside tabular, aligned or another alignment construct. And ! Missing { inserted. usually means a ^ or _ tried to attach to something that cannot take a subscript, such as \frac^2. Both point at the same underlying fact: those seven characters are not letters, they are syntax.
\left( \frac{x^{2}}{1 + x^{2}} \right)^{n}
\left and \right must be paired: leaving out one \right) is reported far from where it belongs.Runaway arguments and unclosed braces
Two messages travel together: ! Runaway argument? followed by the text TeX swallowed, and ! Paragraph ended before \title was complete. A third, ! File ended while scanning use of \author., is the same disease at end of file. All three mean: TeX started collecting an argument and never met the closing brace, so it kept reading — and a blank line is a \par token, which many macro arguments refuse to contain.
The everyday causes are a missing }; a \begin{itemize} with no matching \end{itemize}; a commented-out \end{...} line (you put a % in front of the closing tag and kept the opening one); and a stray blank line inside the argument of a command such as \section{...} or \caption{...}. The last one surprises people: \item\label{a} across two lines with a gap is fine in a body paragraph but not inside a macro argument.
The matching message for the opposite direction is ! Too many }'s., which means a group was closed that was never opened — commonly an editor that inserted a brace for you and another you typed. And when an environment name does not match, LaTeX says it explicitly: ! LaTeX Error: \begin{tabular} on line 12 ended by \end{tabularx}. or, at the end of the file, ! LaTeX Error: \begin{document} ended by \end{figure}.
There is one variant that is not a brace problem at all: ! Missing } inserted. inside a maths display usually means you wrote \\ or a blank line inside \parbox/\begin{array} incorrectly, or put a \multicolumn cell count wrong in a tabular. Count the & in the header against the columns declared in {llr}; that mismatch reports as an alignment or brace error several lines later.
\begin{aligned} 2x + y &= 7 \\ x - 3y &= -1 \end{aligned}
Warnings worth reading, and warnings worth silencing
Overfull \hbox (12.4pt too wide) in paragraph at lines 41--44 is not an error: the text was set, but it does not fit the column and sticks into the margin. You can see the exact offender with \overfullrule switched on, which draws a black bar at the intrusion. The fixes, in the order you should try them: reword the sentence, allow hyphenation with \- inside a long word, load microtype, raise \emergencystretch, or — for a displayed formula — break it with aligned or multline. Last resort: \sloppy for that paragraph, which loosens the tolerance and lets ugly spacing through.
Underfull \hbox (badness 10000) means a line was stretched to fill because it could not be broken, typically a paragraph ending in a short last line inside a narrow column or a tabular cell. It is cosmetic. Underfull \vbox (badness 10000) has occurred while \output is active means a page was stretched vertically, usually because a float would not fit; the real signal is the page, not the message.
Three warnings are not cosmetic and are the ones to check on every build. LaTeX Warning: Reference `fig:plot' on page 3 undefined on input line 88. means the label was never seen — a typo, or the \label sits outside the environment it belongs to, which is the standard way a figure caption points at a section number. LaTeX Warning: Citation `smith2020' undefined means the bibliography step has not run yet. And LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right. means your output is one pass short — run it again, or use latexmk so you never think about it.
Everything else in the log is bookkeeping. LaTeX Font Info: Try loading font information for T1+cmr is a trace, not a complaint. Package hyperref Warning: Token not allowed in a PDF string means a maths expression in a heading cannot be encoded as a bookmark — harmless, fixable with \texorpdfstring{$\alpha$}{alpha}. At the top of the file \listfiles writes the version of every file used; that block is what you paste when asking a question online.
Errors that are about your machine, not your document
! LaTeX Error: File `someclass.cls' not found. and its package twin mean the lookup failed. Either the file is not installed, or it is installed somewhere the engine does not search, or the file database is stale. Check with kpsewhich someclass.cls: a path means LaTeX can find it and your working directory is wrong; no output means it genuinely is not there. On MiKTeX, copy files in and then refresh the file name database. On both, the user tree reported by kpsewhich -var-value TEXMFHOME is the correct place to put a class file you were sent by email. One more cause of not found that is entirely Windows: a project folder whose path contains a non-ASCII letter or a space.
Font failures have their own grammar: !pdfTeX error: pdflatex (file ecti1000): Font ecti1000 at 600 not found or ! Font T1/cmr/m/n/10=ecrm1000 at 10.0pt not loadable: Metric (TFM) file not found. These mean the font metrics or map files are missing, which on a partial TeX Live install is fixed by installing the collection that owns them, and on a mixed installation is fixed by picking one distribution. The message Missing character: There is no ☃ in font cmr10! is different and much simpler: that glyph is not in the text font, so the character is silently dropped from the output and no error is raised — install a Unicode-capable font and switch engine, or replace the character.
! Fatal Package fontspec Error: The fontspec package requires either XeTeX or LuaTeX. is an engine mismatch and the fix is in the build command, not the source. A misspelled class option, meanwhile, is not an error at all: LaTeX reports LaTeX Warning: Unused global option(s): [foocartridge] and then builds the document ignoring what you asked for, which is why an option you typed wrongly produces no visible change rather than a complaint. Options for a \usepackage line behave the same way unless the package validates them.
The hard stop is ! Emergency stop. followed by ! ==> Fatal error occurred, no output PDF file produced!. It means TeX gave up and wrote nothing. Scroll up: the real message is above. The classic triggers are a file that does not exist, an error before \begin{document}, and a source saved in an encoding the engine cannot read.
A missing bibliography is almost never a LaTeX error, because a second program writes it. BibTeX logs to thesis.blg, and that is the file to read. I found no \citation commands---while reading file thesis.aux means the .aux contained no citations — you ran it before writing any. I couldn't open database file refs.bib is a working-directory or naming problem; note that \bibliography{refs} wants the name without the extension. Warning--I didn't find a database entry for "smith2020" is a citation-key mismatch between \cite{...} and the @article{...} key, and it is the usual reason a reference list comes out with gaps.
If you use biblatex, the backend is Biber, not BibTeX, and LaTeX says so in the friendliest message in the whole system: Package biblatex Warning: Please (re)run Biber on the file: thesis and rerun LaTeX afterwards. That is the entire explanation for "my references vanished when I changed style". The general rule for any of these: this is a multi-program pipeline, and each program keeps its own transcript — .log for the engine, .blg for BibTeX or Biber, .bbl for the formatted result the engine then reads. Checking the right file is most of the diagnosis.
The loop that finds anything: bisect, then shrink
Staring at a log does not scale; cutting the problem in half does. Comment out the second half of the document with \end{document} placed at the midpoint, recompile, and keep whichever half still fails. Four or five rounds of that on a 200-page thesis leaves you with a fifteen-line file that reproduces the bug. This is not a beginner trick — it is what experienced users do first, because the diagnosis is trivial once the sample is small.
For a preamble problem the same cut works: comment out \usepackage lines from the bottom upwards. If the error disappears when a package goes, you have an interaction — usually load order. hyperref wants to be loaded last apart from cleveref, which wants to come after it; fontenc must come after inputenc on older setups; and two packages that both define \qty or \coloneqq will produce a Command ... already defined message naming the second loader.
Two switches make the log talk in more detail. \errorcontextlines=100 in the preamble lifts the cap on how much surrounding context TeX prints, which is exactly what you want for a runaway argument. \listfiles as the very first line makes the log end with a version-stamped list of every file that was read, which is how you prove you are compiling against the distribution you think you are. For a single stubborn macro, \typeout{...} prints a value into the log mid-run, and \tracingmacros=1 dumps every macro expansion — the latter is genuinely last-resort because it produces tens of thousands of lines.
In a multi-file project, find out which file is open when the error fires: the log shows it as (./chapter3.tex, and \includeonly{chapter2,chapter3} narrows a book-class build to the parts you care about without touching the \include lines. Then, when you ask someone else: post the smallest file that still fails, the first ! line with its context, and the \listfiles block. That is a question that can be answered; a screenshot of page 40 cannot.
% A minimal working example: the smallest file that still shows the bug.
% Anyone reading it should be able to compile it in ten seconds and
% see exactly what you see -- so include the preamble, not just the line.
\documentclass{article}
\usepackage{amsmath} % list every package the failing code needs
\begin{document}
% Reduce until it breaks: delete sections until the error disappears,
% then delete one thing more. What remains IS the bug.
Here the text stops working:
\[
f(x) = \frac{1}{x} \label{eq:f} % <+ the offending construct goes here +>
\]
\end{document}
Lesson cheat sheet
One key line of code from each section — jump to it or copy it straight from here.
# Non-interactive runs: the engine must never stop and wait for a keypress.
a_{1} + a_{2} = \sum_{i=1}^{2} a_{i}
\left( \frac{x^{2}}{1 + x^{2}} \right)^{n}
\begin{aligned} 2x + y &= 7 \\ x - 3y &= -1 \end{aligned}
\documentclass{article}
Common mistakes
- Reading the last error in the log instead of the first. TeX recovers and then misbehaves everywhere downstream, so a cascade of thirty messages is usually one mistake.
- Taking the printed
l.42as the place you went wrong. It is where TeX noticed; the missing brace is above it. - Fixing an error by adding another
}, or by deleting the\end{}that was complaining, until the file compiles and the structure is silently wrong. - Running the build non-interactively without
-interaction=nonstopmode, so the engine sits at a?prompt and looks like a hang. - Treating
Overfull \hboxas an error and switching warnings off, instead of finding the one line that does not fit. - Concluding that references are broken when the message says Please (re)run Biber, or Rerun to get cross-references right — those are instructions, not complaints.
- Asking for help with a screenshot of page 40 instead of the first
!line and a minimal file that reproduces it.
Questions about this lesson
Where is the LaTeX log file?
What does undefined control sequence mean in LaTeX?
Why does LaTeX say missing $ inserted?
Why are there no errors but no output either?
\begin{document}. Adding -interaction=nonstopmode keeps the run going far enough to write a partial PDF so you can see how far it got.What is overfull hbox and should I care?
\overfullrule to mark each one, then reword, allow a hyphenation point, or split the formula.How do I find which line causes a LaTeX error?
Live playground: edit the LaTeX, see it render
The preview updates as you type — break it on purpose to see how errors look.
Keep reading
Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.