Introduction to LaTeX Best Practices
Learning LaTeX is more than just memorizing commands; it is about adopting a workflow that ensures your documents are professional, maintainable, and easy to debug. Unlike a word processor where you "see what you get," LaTeX focuses on the logical structure of the document.
By following best practices from the start, you avoid the "spaghetti code" that often leads to compilation errors and inconsistent formatting. This guide covers the essential habits that separate a beginner from a proficient LaTeX user.
1. Document Structure and Logical Markup
The most important principle in LaTeX is logical markup. This means you should describe what a piece of text is (a section, a caption, an emphasis) rather than how it should look (bold, 14pt font, centered).
Use Sectioning Commands
Never manually format a title by using \textbf{\large My Title}. Use the built-in sectioning hierarchy. This allows LaTeX to automatically generate a Table of Contents and maintain consistent spacing.
% GOOD: Logical structure
\section{Introduction}
This is the start of my paper.
\subsection{Background}
Here is some history on the topic.
% BAD: Manual formatting
\vspace{10pt}
\noindent \textbf{\Large 1. Introduction}
\vspace{5pt}Organize Large Projects
For long documents like theses or books, do not keep everything in one .tex file. Use \input{filename} to break your work into manageable chapters.
\documentclass{book}
\usepackage[utf8]{inputenc}
\begin{document}
\frontmatter
\include{chapters/abstract}
\mainmatter
\include{chapters/introduction}
\include{chapters/methodology}
\include{chapters/results}
\end{document}Note: Use
\include{}for major components like chapters (it starts a new page), and\input{}for smaller snippets or tables that don't require a page break.
2. Preamble Management and Package Usage
The preamble is the area before \begin{document}. A messy preamble is the leading cause of "command conflicts."
Load Packages Wisely
- Don't over-import: Only load packages you actually use.
- Order matters: Most packages can be loaded in any order, but
hyperrefshould almost always be the last package loaded in your preamble to avoid conflicts with other commands. - Use modern packages: Avoid obsolete packages. For example, use
mcodeorlistingsfor code instead of the oldverbatimenvironment for complex tasks.
Avoid Obsolete Commands
LaTeX has evolved. Ensure you are using modern equivalents for better font rendering and spacing:
- Use
\textit{}instead of{\it ...} - Use
\textbf{}instead of{\bf ...} - Use
\textsf{}instead of{\sf ...}
3. Mathematical Typesetting Best Practices
LaTeX is famous for math, but there are "the right way" and "the old way" to write equations.
Use LaTeX Delimiters
While the single dollar sign $x$ is standard for inline math, the double dollar sign $$ ... $$ for displayed equations is actually an old TeX command that can cause inconsistent spacing in LaTeX.
% GOOD: Modern LaTeX delimiters
This is inline math: \( E = mc^2 \).
This is a displayed equation:
\[
a^2 + b^2 = c^2
\]
% BAD: Old TeX syntax
This is a displayed equation:
$$ a^2 + b^2 = c^2 $$Use the amsmath Environment
For multi-line equations, always use the environments provided by the amsmath package (like align) rather than the obsolete eqnarray. The align environment provides much better spacing around equals signs.
\usepackage{amsmath}
\begin{align}
f(x) &= (x + 3)^2 \\
&= x^2 + 6x + 9
\end{align}4. Consistent Cross-Referencing
One of LaTeX's greatest strengths is its ability to handle numbering automatically. To use this effectively, you must follow a consistent labeling convention.
Use Prefixes for Labels
When you use \label{name}, it is a best practice to use prefixes so you don't get confused between a figure, a table, and a section.
fig:for figures (e.g.,\label{fig:graph})tab:for tables (e.g.,\label{tab:data})sec:for sections (e.g.,\label{sec:intro})eq:for equations (e.g.,\label{eq:theory})
\section{Experimental Results} \label{sec:results}
As seen in Figure~\ref{fig:output}, the data in Section~\ref{sec:results}
suggests a correlation.
\begin{figure}[h]
\centering
% [graphics command here]
\caption{A visualization of the output.}
\label{fig:output}
\end{figure}Tip: Use the tilde symbol
~before a\refcommand. This creates a "non-breaking space," ensuring that "Figure" and the number "1" always stay together on the same line.
5. Coding Style and Readability
Since LaTeX source code is just text, keeping it readable will help you (and others) find errors quickly.
Indentation and White Space
Indent the content inside environments (like itemize, figure, or abstract). It makes the structure visible at a glance.
\begin{itemize}
\item First point of interest.
\item Second point, which includes:
\begin{itemize}
\item A sub-point.
\end{itemize}
\end{itemize}One Sentence Per Line
This is a "pro tip" for LaTeX: write each sentence on its own line in your editor. Because LaTeX ignores single line breaks in the output, the document will look the same, but it makes version control (like Git) and finding specific sentences much easier.
6. Common Pitfalls to Avoid
Even experienced users fall into these traps. Being aware of them will save you hours of troubleshooting.
- Smart Quotes: Never use standard keyboard quotation marks (
"quotes") for both sides. Use two backticks for the left and two apostrophes for the right.- Correct: ``This is quoted text.''
- Incorrect: "This is quoted text."
- Special Characters: Characters like
%,&,$,#, and_have special meanings. If you want them to appear in text, you must escape them with a backslash (e.g.,\%). - Forcing Placement: Avoid using the
[H](HERE!) parameter for figures unless absolutely necessary. LaTeX's floating mechanism is designed to optimize page layout. Trust the engine or use[htbp]for more flexibility. - Manual Spacing: If you find yourself using
\vspace{}or\newlinerepeatedly to "fix" how the page looks, you are likely fighting the system. Check if you should be using a different environment or package instead.
Summary Checklist
- Use
\sectionrather than manual bolding. - Use
\[ ... \]for equations. - Use
\label{fig:...}with consistent prefixes. - Load
hyperrefat the end of the preamble. - Escape special characters (e.g.,
\&). - Use proper LaTeX quotes (`` '').
