Errors and Debugging

A guide to understanding LaTeX compiler logs, deciphering common error messages, and fixing bugs in your documents.

Unlike modern word processors that silently fix mistakes, LaTeX is a strict programming language. If you make a syntax error, the compiler will halt and generate an error log.

While LaTeX error logs look intimidating, they usually point exactly to the line where the issue occurred.

How to Read an Error Log

When compilation fails, LaTeX outputs a log file (.log). The most important information is usually at the bottom.

! Undefined control sequence.
l.42 \mycustomcommand
                     {Some text here}

Let's break this down:

  1. ! Undefined control sequence. — This is the error type.
  2. l.42 — The exact line number in your .tex file where the compiler crashed (Line 42).
  3. \mycustomcommand — The specific code that caused the crash.

The Most Common Errors

1. Undefined Control Sequence

This happens when you try to use a command that LaTeX doesn't recognize.

  • Cause: A typo (e.g., \italics instead of \textit), or you forgot to include the required package in your preamble (e.g., using \includegraphics without \usepackage{graphicx}).

2. Missing $ Inserted

This happens when you use a math-exclusive symbol outside of math mode.

  • Cause: You typed an underscore _ or a caret ^ in regular text.
  • Fix: Either wrap the math in dollar signs ($x_2$) or escape the character if it's meant to be plain text (my\_file.txt).

3. File 'foo.sty' Not Found

This is a fatal environment error indicating a missing package.

  • Cause: Your local TeX Live installation does not have the package you are trying to use in \usepackage{...}.
  • Fix: You must manually install the package using your TeX package manager (e.g., tlmgr). Alternatively, you can use a cloud editor like LetX.app where all packages are pre-installed.
Warnings vs Errors

Not all messages are fatal. "Overfull \hbox" is a warning, meaning the document still compiled, but a line of text was too long and bled past the document margin.

Debugging Best Practices

If you have a massive document and can't isolate the error, use the Divide and Conquer method:

  1. Comment out large chunks of your document using the % symbol.
  2. Try compiling again.
  3. If it compiles, you know the error is somewhere inside the commented section.
  4. Slowly uncomment sections until the compiler crashes, revealing the exact location of the bug.