Understanding the Compilation Process
Unlike word processors like Microsoft Word or Google Docs, LaTeX follows a "What You Get Is What You Mean" (WYGIWYM) philosophy. This means you do not see the final layout as you type. Instead, you write plain text combined with commands, and a compiler translates that code into a beautiful document, typically a PDF.
Think of LaTeX code as a recipe and the compiler as the chef. You provide the instructions (the .tex file), and the compiler processes those instructions to produce the final dish (the .pdf file).
The Basic Workflow
- Write: Create your document in a
.texfile using an editor (like Typevia or VS Code). - Compile: Run a TeX engine to process the file.
- Preview: View the resulting PDF to check the layout and content.
- Fix: If there are errors or layout issues, go back to step 1.
Choosing Your Engine: PDFLaTeX, XeLaTeX, and LuaLaTeX
When you click "Compile" or "Build," your computer is running a specific "engine." While there are several, beginners usually encounter these three:
1. PDFLaTeX
This is the traditional and most common engine. It is fast, stable, and works perfectly with standard fonts.
- Best for: Standard academic papers and documents using classic LaTeX fonts.
2. XeLaTeX
XeLaTeX was designed to support modern font technologies (like OpenType and TrueType) and Unicode (which allows you to type in any language directly).
- Best for: Using fonts already installed on your computer (like Arial or Times New Roman) and multi-lingual documents.
3. LuaLaTeX
The successor to XeLaTeX, it is highly customizable because it includes the Lua programming language. It is generally considered the modern standard for the future of LaTeX.
- Best for: Complex layouts and users who want the most modern features.
Note: Most beginners should start with PDFLaTeX. If you need specific fonts or non-Latin characters, switch to XeLaTeX.
Why You Often Need to Compile Twice (The "Multi-Pass" System)
One of the most confusing things for beginners is why a single compilation often isn't enough. If you add a Table of Contents, a cross-reference, or a citation, you might see a question mark ?? or a blank space where the reference should be.
The Auxiliary Files
When you compile, LaTeX creates several "helper" files. The most important is the .aux file.
- First Pass: LaTeX reads the document. It sees you want a Table of Contents and sees your section headings, but it doesn't know what page numbers they are on yet. It writes these locations into the
.auxfile. - Second Pass: LaTeX reads the
.auxfile from the previous run. Now it knows exactly where everything is and can correctly fill in the Table of Contents and references.
Example: Cross-Referencing
\documentclass{article}
\begin{document}
\section{Introduction} \label{sec:intro}
As seen in Section \ref{sec:conclusion}, LaTeX is powerful.
\newpage
\section{Conclusion} \label{sec:conclusion}
This is the end of the document.
\end{document}If you compile this once, the output will show "Section ??". Compile it a second time, and it will correctly show "Section 2".
Compiling Bibliographies
Bibliographies (using BibTeX or BibLaTeX) require an even more specific sequence. Because the bibliography information is usually stored in a separate .bib file, a specific tool must link the two.
A typical compilation sequence for a bibliography looks like this:
- pdflatex (Records which citations you used)
- bibtex (or biber) (Looks up those citations in your
.bibfile) - pdflatex (Incorporates the bibliography into the document)
- pdflatex (Ensures all citations in the text point to the correct entry)
Tip: Many modern editors have a "Magic" or "Build" button that automates this entire sequence for you so you don't have to click "Compile" four times!
Understanding Errors and the Log File
When the compiler encounters something it doesn't understand, it will stop and issue an Error.
Common Error Messages
- Undefined control sequence: You misspelled a command (e.g.,
\begun{document}instead of\begin{document}). - Missing $ inserted: You used a mathematical symbol (like
_or^) outside of "Math Mode." - Emergency stop: A major error occurred, often a missing file or a catastrophic syntax mistake.
The .log File
Every time you compile, LaTeX generates a .log file. If your compilation fails, this file contains the detailed "autopsy" of what went wrong.
- Warnings: These are non-fatal. Your PDF will still be created, but things might look weird (e.g., "Overfull \hbox" means your text is leaking into the margin).
- Errors: These are fatal. The compiler will stop, and no PDF will be produced until the error is fixed.
Practical Examples to Try
Example 1: A Basic Document
Copy this into your editor and compile it using PDFLaTeX.
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{My First Compiled Document}
\author{LaTeX Learner}
\date{\today}
\begin{document}
\maketitle
Hello, world! This is a simple LaTeX document.
If I change this text and re-compile, the PDF will update.
\end{document}Example 2: Testing Multi-Pass
Compile this once, then compile it again to see the ?? change into a number.
\documentclass{article}
\begin{document}
\tableofcontents
\section{The Beginning}
I want to refer to the end of this document, which is on page \pageref{pg:end}.
\newpage
\section{The End} \label{pg:end}
Here we are!
\end{document}Best Practices for Compilation
- Compile Frequently: Don't write 10 pages before compiling. Compile every paragraph or two. This makes it much easier to find the exact line where you made a mistake.
- Clean Auxiliary Files: Sometimes the helper files (
.aux,.out,.log) get "corrupted" with old information. Most editors have an option to "Clean" or "Delete Intermediate Files." If your document is behaving strangely, try cleaning these files and compiling from scratch. - Check the First Error First: If you get 20 errors, focus only on the very first one. Often, one missing bracket at the top of your file causes a "domino effect" of errors below it.
- Use an Online Compiler (Optional): If you are struggling with local installation and engines, tools like Typevia handle the compilation process and engine selection in the cloud, which is a great way for beginners to get started.
Warning: Never ignore errors! While some editors allow you to "scroll past" errors to see a partial PDF, this can lead to unstable documents that eventually refuse to compile at all. Fix every error as it appears.
