Understanding LaTeX Performance
As you transition from writing short documents to long reports, theses, or books, you may notice that the "compile time"—the time it takes for LaTeX to turn your code into a PDF—starts to increase. While LaTeX is incredibly powerful, certain tasks like processing high-resolution images, complex mathematical diagrams, and massive bibliographies can slow down your workflow.
Performance optimization in LaTeX isn't just about making the computer work faster; it's about optimizing your workflow. By using the right commands and organizational structures, you can focus on writing without waiting minutes for every minor preview.
Managing Large Documents: \input vs. \include
When working on a large project, the first step to better performance is breaking your document into smaller files (e.g., one file per chapter). LaTeX provides two primary commands for this, but they behave very differently regarding performance.
The \input Command
The \input{filename} command simply copies the text from another file into your main document at that exact spot. It offers no performance benefits other than keeping your main file clean.
The \include Command
The \include{filename} command is much more powerful. It starts a new page for the included content and creates a separate .aux file for that specific chapter. This allows you to use the \includeonly command.
Using \includeonly for Speed
The \includeonly command tells LaTeX to only compile specific files while still keeping all the cross-references (like page numbers and citations) from the other chapters intact.
\documentclass{book}
% In the preamble, specify ONLY the chapter you are currently writing
\includeonly{chapters/chapter2}
\begin{document}
\include{chapters/chapter1} % This will be skipped (but refs remain)
\include{chapters/chapter2} % This will be compiled
\include{chapters/chapter3} % This will be skipped
\end{document}Note: For
\includeonlyto work correctly, you must have compiled the entire document at least once so that LaTeX has the cross-reference data stored in the auxiliary files.
Speeding Up Graphics with Draft Mode
Graphics are often the biggest culprit behind slow compilation times. Every time you compile, LaTeX has to read the dimensions and data of every image file.
Enabling Global Draft Mode
You can add the draft option to your \documentclass. In draft mode, LaTeX will not render images; instead, it will print a placeholder box with the filename. This makes compilation nearly instantaneous.
\documentclass[12pt, a4paper, draft]{report}
\usepackage{graphicx}
\begin{document}
% In draft mode, this will just show a box with "my_huge_photo.jpg"
\includegraphics[width=\textwidth]{my_huge_photo.jpg}
\end{document}Local Draft Mode
If you only want to disable images in one specific section, you can pass the draft option directly to the graphicx package or individual images:
\usepackage[draft]{graphicx} % Disables all images
% OR
\includegraphics[draft, width=5cm]{diagram.pdf} % Disables only this imageOptimizing TikZ and PGFPlots
TikZ is a fantastic tool for creating vector graphics directly in LaTeX, but it is notoriously slow because LaTeX has to recalculate every coordinate and line every time you compile.
The Externalization Library
The best way to optimize TikZ is through "externalization." This process compiles each TikZ picture into a separate PDF file. On subsequent compiles, LaTeX simply checks if the code has changed; if not, it inserts the pre-compiled PDF instead of re-rendering the graphic.
\documentclass{article}
\usepackage{tikz}
% Enable externalization
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/] % Saves compiled figures in a subfolder
\begin{document}
\begin{tikzpicture}
\draw (0,0) circle (2cm);
% Complex drawing code here...
\end{tikzpicture}
\end{document}Important: To use externalization, you usually need to compile your document with the
--shell-escapeflag enabled in your LaTeX editor settings (e.g.,pdflatex --shell-escape main.tex).
Efficient Bibliography Management
As your bibliography grows to hundreds of entries, tools like the older BibTeX can become slow and struggle with memory.
Move to BibLaTeX and Biber
Modern LaTeX users should use the biblatex package with the biber backend. Biber is significantly more powerful, handles large files better, and supports full UTF-8 (Unicode) encoding.
\usepackage[style=authoryear, backend=biber]{biblatex}
\addbibresource{references.bib}
% To compile:
% 1. pdflatex main
% 2. biber main
% 3. pdflatex mainTip: Only include the citations you actually use. If you have a massive .bib file from a manager like Zotero or Mendeley, biblatex only processes the entries you \cite, which keeps performance high.
Best Practices and Common Pitfalls
To keep your LaTeX environment running smoothly, follow these industry best practices:
- Reduce Image Resolution: Don't use a 20MB 4K photograph if it's only going to be 5cm wide on a printed page. Resize and compress images using an external editor before importing them.
- Use PDF for Vector Graphics: For charts and diagrams, use
.pdfor.epsinstead of high-res.pngor.jpg. They are smaller and look better. - Clean Auxiliary Files: Sometimes LaTeX gets "clogged" with old temporary files. If you encounter strange errors or unexpected slowdowns, use your editor's "Clean" or "Delete Intermediate Files" function and recompile.
- Avoid "Package Bloat": Only load the packages you actually need. Every
\usepackageadds a small amount of processing time to the preamble. - Choose the Right Engine:
- pdfLaTeX: Generally the fastest for standard documents.
- LuaLaTeX: Slower to start but much more powerful for complex fonts and large memory-intensive tasks.
Summary Checklist for Performance
- Use
\includeand\includeonlyfor multi-chapter projects. - Switch on
draftmode when focusing purely on text. - Externalize TikZ and PGFPlots diagrams.
- Pre-compress large bitmap images.
- Use
biberfor bibliography management.
By implementing these strategies, you can maintain a fast, responsive writing environment regardless of how large or complex your LaTeX document becomes.
