Mastering LaTeX
An In-Depth Guide to Common Mistakes and Advanced Techniques
content/posts/2025/latex-mistakes-and-solutions.md — front matter: date 2025-01-13, filed under Tutorial. Rendered through the collection's own Markdown pipeline; 184 headings.
The register reads this same file: a post is announced on the date its own front matter carries, so the announcement cannot drift from the post.
LaTeX is a powerful typesetting system that has become the standard for producing scientific and mathematical documents of high typographical quality. Its strengths lie in its ability to handle complex structures and its extensive customization capabilities. However, its complexity can also lead to common mistakes that can be challenging to troubleshoot, even for experienced users. To this end, I present this comprehensive guide to help you navigate these pitfalls. Each point is detailed with thorough explanations, examples, solutions, and alternative approaches, ensuring the information is invaluable for your LaTeX mastery.
Introduction
LaTeX’s extensive capabilities allow for the creation of complex documents with precise control over formatting and structure. However, its steep learning curve can lead to common mistakes that hinder productivity and document quality. This guide aims to equip you with the knowledge to avoid these pitfalls, optimize your LaTeX workflow, and harness advanced techniques, which are valuable for researchers preparing scholarly papers.
Simple Mistakes
1. Misplaced or Missing Braces {}
The Issue
LaTeX uses braces {} to group content and define the scope of commands. Missing or misplacing braces can lead to compilation errors or unintended formatting.
Symptoms
- Compilation Errors: Errors pointing to unexpected parts of the code.
- Formatting Issues: Text appearing in the wrong format or location.
- Error Messages: Such as
Runaway argument?orExtra }, or forgotten \endgroup.
Example of the Mistake
\textbf{This is bold text.
Explanation
The \textbf{} command applies bold formatting to the text within the braces. LaTeX cannot determine where the command ends without a closing brace, resulting in an error.
Solution
Ensure every opening brace { has a corresponding closing brace }.
Corrected Code
\textbf{This is bold text.}
Alternative Solutions
- Use an Editor with Brace Matching: Editors like TeXstudio, Overleaf, or Visual Studio Code highlight matching braces, helping you spot mismatches easily.
- Structure Your Code: Break down complex commands into smaller parts to make brace matching more manageable.
Out-of-the-Box Tip
Consider using a linter or code analysis tool that checks for unbalanced braces and other syntax errors as you type. This proactive approach can prevent errors before they occur.
2. Undefined Control Sequences
The Issue
Using commands that LaTeX does not recognize, often due to typos or missing packages, leads to Undefined control sequence errors.
Symptoms
- Error Messages:
Undefined control sequence. - Compilation Failure: Document fails to compile, or certain elements do not appear as expected.
Example of the Mistake
\begin{document}
\includegraphics{image.png}
\end{document}
Explanation
The \includegraphics{} command is provided by the graphicx package. Without including this package in the preamble, LaTeX does not recognize the command.
Solution
Include the necessary package in the preamble.
Corrected Code
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{image.png}
\end{document}
Alternative Solutions
- Check for Typos: Ensure all commands are spelled correctly and use the correct syntax.
- Create a Personal Template: Include commonly used packages in a template to avoid forgetting them.
Out-of-the-Box Tip
Use the \RequirePackage{} directive in custom class or package files to ensure necessary packages are always loaded, especially when collaborating with others.
3. Ignoring Special Characters
The Issue
Certain characters have special meanings in LaTeX and cannot be used directly in text. These include #, %, $, &, _, {, }, ^, ~, and \.
Symptoms
- Compilation Errors: When these characters are used without proper handling.
- Error Messages:
Missing $ insertedorUndefined control sequence.
Example of the Mistake
The cost is $100 & tax included.
Explanation
The & character is used for alignment in tables and arrays. Using it outside of these environments without escaping causes an error.
Solution
Escape special characters by prefixing them with a backslash \.
Corrected Code
The cost is \$100 \& tax included.
Alternative Solutions
-
Use the
verbatimEnvironment: For code snippets or text where special characters should be displayed as-is.\begin{verbatim} The cost is $100 & tax included. \end{verbatim} -
Use the
\texttt{}Command: For inline monospace text.Use the command \texttt{git status} to check the repository status.
Out-of-the-Box Tip
Use the listings or minted package to handle special characters automatically and provide syntax highlighting for code listings.
4. Overfull and Underfull Boxes
The Issue
Overfull or underfull boxes occur when LaTeX cannot properly fit text within the specified margins, leading to too wide or too narrow lines.
Symptoms
- Warning Messages:
Overfull \hboxorUnderfull \hbox. - Visual Issues: Text protruding into the margins or uneven spacing between words.
Example of the Mistake
This long line of text will likely cause an overfull hbox warning because it cannot be broken properly.
Explanation
LaTeX tries to adjust the spacing to justify text, but it reports overfull or underfull boxes when it cannot. Long words or URLs without breakpoints exacerbate the issue.
Solution
-
Allow Hyphenation: Ensure words can be hyphenated or manually insert hyphenation points using
\-. -
Use the
microtypePackage: Enhances justification and hyphenation.\usepackage{microtype}
Corrected Code
\usepackage{microtype}
This long line of text will likely cause an over\-full hbox warning because it cannot be broken properly.
Alternative Solutions
- Adjust Tolerance Settings: Use
\sloppyor set\tolerance=1000to allow more flexible spacing. - Handle Long URLs: Use the
urlorbreakurlpackage to manage line breaks in URLs.
Out-of-the-Box Tip
Set \emergencystretch=3em to allow LaTeX to adjust spacing in emergency situations without affecting overall formatting.
5. Incorrect Figure and Table Placement
The Issue
Figures and tables (floats) only sometimes appear where you expect them due to LaTeX’s float placement algorithm and incorrect placement specifiers.
Symptoms
- Misplaced Floats: Figures and tables appear at the top or bottom of pages instead of where they are placed in the code.
- Accumulation of Floats: Multiple floats stack up at the end of a chapter or section.
- Gaps in Text: Large gaps in the document where floats are supposed to appear.
Example of the Mistake
\begin{figure}
\includegraphics{image.png}
\caption{An example image}
\label{fig:example}
\end{figure}
No placement specifier is provided, so LaTeX defaults to [tbp].
Explanation
LaTeX places floats according to allowed positions:
h(here): Place the float where it appears in the source code.t(top): At the top of a page.b(bottom): At the bottom of a page.p(page of floats): On a dedicated float page.
LaTeX uses its algorithm without explicit instructions, which may not align with your expectations.
Solution
Specify float placement options to guide LaTeX.
Corrected Code
\begin{figure}[htbp]
\centering
\includegraphics{image.png}
\caption{An example image}
\label{fig:example}
\end{figure}
Detailed Explanation of [htbp] vs. [htb]
[htbp]: Allows placement here, at the top or bottom of a page, or on a separate float page (p). Includingpgives LaTeX more flexibility when placing the float.[htb]: Allows placement here, at the top or bottom of a page, but not on a separate float page. Omittingpcan cause floats to pile up if they do not fit the specified positions.
Alternative Solutions
-
Use the
!Modifier:[!htbp]tells LaTeX to ignore certain restrictions and try harder to place the float as specified. -
Force Exact Placement with the
floatPackage:\usepackage{float} \begin{figure}[H] \centering \includegraphics{image.png} \caption{An example image} \label{fig:example} \end{figure}- The
[H]specifier forces the float to appear precisely where it is in the code, which can disrupt the document’s flow.
- The
Out-of-the-Box Tip
Use the placeins package and the \FloatBarrier command to prevent floats from moving past a certain point, such as the end of a section.
\usepackage{placeins}
...
\section{Results}
% Figures and tables
\FloatBarrier
6. Incorrect Use of Math Mode
The Issue
Failing to correctly enter or exit math mode results in incorrect formatting and errors.
Symptoms
- Improper Formatting: Mathematical symbols appearing as regular text.
- Compilation Errors: Error messages like
Missing $ inserted.
Example of the Mistake
The solution to ax + b = 0 is x = -b/a.
Explanation
Mathematical expressions need to be enclosed in $...$ or \[...\] to be typeset correctly.
Solution
Ensure all mathematical content is within math mode delimiters.
Corrected Code
The solution to $ax + b = 0$ is $x = -b/a$.
Alternative Solutions
-
Use
\( ... \)and\[ ... \]for inline and display math mode, respectively.The solution to \( ax + b = 0 \) is \( x = -\frac{b}{a} \).
Out-of-the-Box Tip
Use the amsmath package for enhanced mathematical typesetting capabilities, providing environments like align, gather, and multline.
7. Improper Use of \label and \ref Commands
The Issue
Incorrectly placing \label commands or not using \ref and \pageref properly leads to broken or incorrect cross-references.
Symptoms
- Broken References: References showing up as ”??”.
- Incorrect Numbering: References pointing to wrong sections or figures.
Example of the Mistake
\section{Introduction}
\label{sec:intro}
As discussed in Section \ref{sec:intro}, ...
If \label is not immediately after the \section command, it may not correctly capture the section number.
Explanation
To correctly reference the number, the \label command should come immediately after the counter-stepping command (like \section, \caption, etc.).
Solution
Place \label immediately after the sectioning or captioning command.
Corrected Code
\section{Introduction}\label{sec:intro}
As discussed in Section \ref{sec:intro}, ...
Alternative Solutions
-
Use Meaningful Labels: To organize labels, use prefixes like
sec:,fig:, andtab:.\section{Methodology}\label{sec:method} See Figure~\ref{fig:example} for an illustration.
Out-of-the-Box Tip
Consider using the cleveref package to automate reference formatting and reduce errors for complex documents.
8. Not Loading Necessary Packages
The Issue
Neglecting to include packages that provide essential commands or functionalities results in errors or missing features.
Symptoms
- Undefined Commands: Errors like
Undefined control sequence. - Missing Symbols: Improper display of mathematical symbols or special characters.
Example of the Mistake
Using \mathbb{R} without including the amssymb package.
Let $x \in \mathbb{R}$.
Explanation
The \mathbb{} command is provided by the amssymb package.
Solution
Include the necessary package in the preamble.
Corrected Code
\usepackage{amssymb}
Let $x \in \mathbb{R}$.
Alternative Solutions
- Consult Documentation: Refer to package documentation to identify required packages for specific commands.
- Use the Comprehensive LaTeX Symbol List: Available online to find symbols and their corresponding packages.
Out-of-the-Box Tip
Create a personal package or class file that includes frequently used packages for consistency across documents.
9. Improper Font Encoding
The Issue
Using characters that are not supported by the default font encoding leads to errors or missing characters.
Symptoms
- Error Messages:
Command \textquotedbl unavailable in encoding OT1. - Missing Characters: Accented characters or special symbols not displaying correctly.
Example of the Mistake
Using accented characters without specifying the proper font encoding.
El Niño phenomenon affects climate patterns.
Explanation
By default, LaTeX uses the OT1 font encoding, which does not support accented characters directly.
Solution
Include the fontenc and inputenc packages.
Corrected Code
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
El Niño phenomenon affects climate patterns.
Alternative Solutions
- Use Unicode-aware Compilers: Switch to XeLaTeX or LuaLaTeX, which handle Unicode natively.
Out-of-the-Box Tip
Set the document to use modern fonts and encoding from the start to avoid encoding issues.
10. Forgetting to Save Files with the Correct Encoding
The Issue
Saving files in the wrong encoding causes LaTeX to misinterpret special characters.
Symptoms
- Compilation Errors: Involving unexpected characters.
- Misrendered Characters: Special characters not displaying correctly.
Example of the Mistake
Writing the document with UTF-8 characters but saving it in ANSI encoding.
Explanation
LaTeX reads the file according to the specified encoding. Mismatches between file encoding and declared encoding lead to errors.
Solution
- Ensure your text editor saves files in UTF-8 encoding.
- Match the encoding in
\usepackage[...]{inputenc}with the file encoding.
Corrected Code
\usepackage[utf8]{inputenc}
Alternative Solutions
- Use Editors that Handle Encoding Well: Editors like TeXstudio or Overleaf manage encoding automatically.
- Use XeLaTeX or LuaLaTeX: These engines handle Unicode natively, eliminating encoding issues.
Out-of-the-Box Tip
Ensure all team members use the same encoding settings to prevent conflicts when collaborating.
Advanced Mistakes
1. Misuse of Math Operators (\sin, \log, etc.)
The Issue
Not using the proper commands for mathematical operators leads to incorrect formatting, such as operators appearing italicized like variables.
Symptoms
- Improper Formatting: Function names like “sin” or “log” are italicized.
- Incorrect Spacing: Around operators in mathematical expressions.
Example of the Mistake
Let $y = sin(x) + log(x)$.
Explanation
In math mode, LaTeX treats letters as variables and italicizes them. Without proper commands, “sin” and “log” are interpreted as products of variables s, i, n, etc.
Solution
Use LaTeX’s built-in commands for standard mathematical operators.
Corrected Code
Let $y = \sin(x) + \log(x)$.
Alternative Solutions
-
Use
\operatorname{}for Custom Operators:Let $y = \operatorname{customFunc}(x)$. -
Define New Operators with
\DeclareMathOperator:\usepackage{amsmath} \DeclareMathOperator{\sech}{sech} Let $y = \sech(x)$.
Out-of-the-Box Tip
If you need an operator with limits like \sum or \lim, use \DeclareMathOperator*:
\DeclareMathOperator*{\Max}{Max}
Let $\Max_{x \in S} f(x)$.
This ensures proper formatting and placement of limits.
2. Inadequate Spacing in Math Mode
The Issue
LaTeX may not automatically provide appropriate spacing in mathematical expressions, especially around certain symbols or complex equations.
Symptoms
- Cluttered Expressions: Mathematical expressions look crowded or need to be readied.
- Inconsistent Spacing: Symbols and operators are too close or far apart.
Example of the Mistake
If $x>y$, then $f(x)=g(y)$.
Explanation
LaTeX does not always insert adequate spacing around relational operators (=, <, >) and between function names and variables.
Solution
Use spacing commands in math mode to adjust space as needed.
Corrected Code
If $x > y$, then $f(x) = g(y)$.
-
The default spacing is usually acceptable, but if more space is needed:
If $x \, > \, y$, then $f(x) \, = \, g(y)$.
Spacing Commands
\,: Thin space\:: Medium space\;: Thick space\!: Negative thin space\quad: Space equal to the current font size\qquad: Double\quad
Alternative Solutions
-
Use
\mathrel{}for Relational Operators:If $x \mathrel{>} y$, then $f(x) \mathrel{=} g(y)$.
Out-of-the-Box Tip
Define custom commands for frequently used spaced expressions:
\newcommand{\sgeq}{\; \geq \;}
Use as:
If $x \sgeq y$, then...
This ensures consistent spacing throughout your document.
3. Misuse of \mid vs. | in Math Mode
The Issue
Using the vertical bar | instead of \mid in mathematical expressions can result in incorrect spacing, affecting the readability and meaning of expressions involving conditional probabilities or set notation.
Symptoms
- Inconsistent Spacing: Around the vertical bar.
- Ambiguity: Potential confusion in interpreting mathematical statements.
Example of the Mistake
P(A|B) = \frac{P(A \cap B)}{P(B)}
Explanation
In math mode, | is treated as a math operator with binary relation spacing, which may not be appropriate for all contexts. The \mid command provides the correct spacing for “middle” separators.
Solution
Use \mid for conditional expressions and set notation.
Corrected Code
P(A \mid B) = \frac{P(A \cap B)}{P(B)}
Alternative Solutions
-
Use
\bigm|for Adjustable Size:P\bigl(A \bigm| B\bigr) -
Define Custom Commands:
\newcommand{\given}{\mid} P(A \given B)
Out-of-the-Box Tip
For complex expressions, use \left and \right with \middle|:
P\left( A \,\middle|\, B \right)
This ensures that the size of the delimiters adjusts appropriately to the content.
4. Not Using Non-Breaking Spaces for Units and Numbers
The Issue
Numbers and their associated units or symbols can be separated across lines if LaTeX inserts a line break between them, leading to awkward and confusing formatting.
Symptoms
- Separated Units: Units or symbols appearing at the beginning of a new line, separated from the associated number.
- Disrupted References: Expressions like “Figure 1” split between lines.
Example of the Mistake
The distance is 10 km.
See Figure 1 for details.
If a line break occurs after “10” or “Figure,” the unit or figure number is separated.
Explanation
By default, LaTeX treats spaces as possible breakpoints. To keep numbers and units together, non-breaking spaces are needed.
Solution
Use a tilde ~ to create a non-breaking space between the number and the unit or reference.
Corrected Code
The distance is 10~km.
See Figure~1 for details.
Alternative Solutions
-
Use
\nobreakspace:The distance is 10\nobreakspace km. -
Employ the
siunitxPackage for Units:\usepackage{siunitx} The distance is \SI{10}{\kilo\metre}.
Out-of-the-Box Tip
For dates and other connected phrases, use non-breaking spaces:
On April~15, we observed...
Refer to Equation~(2).
This ensures that connected information stays together, enhancing readability.
5. Incorrect Use of \DeclarePairedDelimiter
The Issue
Manually sizing delimiters using \left and \right can lead to inconsistent formatting and excessive whitespace.
Symptoms
- Oversized Delimiters: Parentheses or brackets larger than necessary.
- Inconsistent Sizing: Across similar expressions, leading to a lack of uniformity.
Example of the Mistake
\left( \frac{a}{b} \right) + \left( \frac{c}{d} \right)
Explanation
Using \left and \right forces LaTeX to size delimiters based on the content, which may not always be visually optimal.
Solution
Use the \DeclarePairedDelimiter command from the mathtools package to define paired delimiters with flexible sizing.
Corrected Code
\usepackage{mathtools}
\DeclarePairedDelimiter{\paren}{(}{)}
\[
\paren*{\frac{a}{b}} + \paren*{\frac{c}{d}}
\]
- The
*in\paren*{}automatically adjusts the size of the delimiters.
Alternative Solutions
-
Specify Delimiter Size Manually:
\[ \paren[\bigg]{\frac{a}{b}} + \paren[\Big]{\frac{c}{d}} \] -
Use
\mleftand\mrightfor Better Spacing:\[ \mleft( \frac{a}{b} \mright) + \mleft( \frac{c}{d} \mright) \]
Out-of-the-Box Tip
Define additional paired delimiters for brackets, braces, or absolute values:
\DeclarePairedDelimiter{\bracket}{[}{]}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\[
\abs*{\frac{a}{b}}
\]
This approach ensures consistent delimiter sizing throughout your document.
6. Overloading Commands Unintentionally
The Issue
Redefining existing LaTeX commands can cause unexpected behavior and conflicts, leading to errors or loss of functionality.
Symptoms
- Unexpected Behavior: Original commands no longer work as expected.
- Compilation Warnings: About command redefinitions.
- Formatting Issues: Unintended changes in document appearance.
Example of the Mistake
\newcommand{\table}{This is my table.}
Explanation
By redefining \table, you overwrite the existing command, which can interfere with LaTeX’s internal operations or other packages.
Solution
Use unique names for custom commands, preferably with a prefix.
Corrected Code
\newcommand{\mytable}{This is my table.}
Alternative Solutions
-
Check Command Existence Before Defining:
\providecommand{\mycommand}{...} -
Use Package-Specific Namespaces: Some packages allow defining commands within their own namespace.
Out-of-the-Box Tip
Develop a naming convention for your custom commands, such as prefixing them with your initials or project abbreviation, e.g., \ABsection.
7. Not Understanding Error Messages
The Issue
LaTeX error messages can be cryptic, making identifying and fixing issues challenging.
Symptoms
- Frustration: Due to unclear error messages.
- Time Wasted: Spending excessive time troubleshooting simple errors.
Example of the Mistake
An error message like:
! Missing $ inserted.
Explanation
This error often occurs when a math-mode-only symbol is used outside of math mode or when a $ is missing.
Solution
- Read the Error Message Carefully: It often indicates the line number and nature of the error.
- Check for Missing Math Mode Delimiters: Ensure that mathematical expressions are enclosed within
$...$or\[...\].
Alternative Solutions
- Use an Editor with Error Highlighting: Editors like TeXstudio or Visual Studio Code with LaTeX extensions can pinpoint errors.
- Compile Frequently: Catch errors early by compiling your document regularly during editing.
Out-of-the-Box Tip
Consult online resources like TeX Stack Exchange, where similar errors are discussed. Copy the error message into the search bar to find potential solutions.
8. Inefficient Handling of Large Documents
The Issue
Not properly structuring large documents leads to slow compilation times and difficulty in navigation.
Symptoms
- Slow Compilation: Especially with documents containing many images or chapters.
- Difficulty Managing Content: Hard to navigate and edit specific sections.
Example of the Mistake
Writing an entire thesis in a single .tex file.
Explanation
Large documents become unwieldy when not broken into manageable parts.
Solution
- Use
\includeand\inputCommands: Split the document into multiple files.
Corrected Code
% Main document (main.tex)
\documentclass{book}
\begin{document}
\tableofcontents
\include{chapters/introduction}
\include{chapters/methodology}
\include{chapters/results}
\end{document}
Alternative Solutions
-
Use the
subfilesPackage: Allows individual compilation of chapters.\usepackage{subfiles}
Out-of-the-Box Tip
Use \includeonly{} to compile only specific chapters, speeding up the compilation process during editing.
9. Neglecting the Use of Bibliography Tools
The Issue
Manually formatting citations and bibliographies is time-consuming and prone to errors.
Symptoms
- Inconsistent Citation Styles: Across the document.
- Formatting Errors: In bibliography entries.
Example of the Mistake
Manually typing bibliography entries at the end of the document.
Explanation
Manual management is inefficient and does not scale well with many references.
Solution
Use bibliography management packages like biblatex with biber.
Corrected Code
\usepackage[
backend=biber,
style=authoryear,
]{biblatex}
\addbibresource{references.bib}
As discussed in \cite{Doe2020}, the results are significant.
...
\printbibliography
Alternative Solutions
- Use Reference Management Software: Like Zotero or JabRef to manage
.bibfiles.
Out-of-the-Box Tip
Customize citation styles extensively with biblatex options, ensuring compliance with journal requirements.
10. Improper Use of \newcommand and \renewcommand
The Issue
Using \newcommand to redefine existing commands or \renewcommand to define new commands leads to errors.
Symptoms
- Compilation Errors: Such as
Command \foo already defined. - Unexpected Behavior: Commands not working as intended.
Example of the Mistake
\newcommand{\section}[1]{\textbf{#1}}
Explanation
Using \newcommand on an existing command (\section) results in an error because it is already defined.
Solution
Use \renewcommand to redefine existing commands.
Corrected Code
\renewcommand{\section}[1]{\textbf{#1}}
Alternative Solutions
- Avoid Redefining Core Commands: Unless absolutely necessary.
Out-of-the-Box Tip
When redefining commands, consider the impact on document structure and package compatibility.