Stage 1 · Foundations 1 / 48 ≈ 17 min read

Getting Started with LaTeX: First Document, Line by Line

What LaTeX is as a system, how to get one page compiling today, what every line of a minimal document actually does, where text mode stops and maths mode starts, and how the engine reads the arguments of a command.

    What LaTeX actually is

    LaTeX is not an application you type into. It is a language you write down, plus a program that reads that writing and builds a page. Your source file is plain text whose commands say what each piece is — a section heading, a displayed equation, a footnote, a citation — and the engine decides how it looks. Nothing is placed by pushing characters around, and the same decisions come out the same way on every machine that has the same class files.

    For mathematics that separation is the whole reason the system exists. x^{2} + \frac{1}{2} is unambiguous to write, to store, to search, to compare line by line and to convert, which is why arXiv, every serious maths editor and this site's own formula converter all speak LaTeX. A formula inside a word-processor document is closer to a picture of a formula; the same thing in LaTeX is a sentence in a small formal language that still means the same mathematics ten years from now, on somebody else's computer, inside a text diff.

    There is a stack under the name, and knowing it explains most of what you will read about later. TeX is the typesetting engine. LaTeX is a macro layer written in TeX's own language that adds the vocabulary you actually use — \documentclass, \section, environments, counters, cross-references. Above LaTeX sit the class and package files, which are more macro code again. That is why LaTeX behaves like a programming runtime rather than like a text box: a command nobody has defined stops the run, and the usual reason is a package you never loaded. You will not write macro code in this course, but from the first page you should treat the file as instructions, not as formatted text.

    The two ways to get a first compiled page

    There are exactly two routes to a PDF and both stay legitimate all the way to publication. The zero-install route is Overleaf: the editor, the distribution and the compiler all live in a browser tab, a project is a folder of files on a server, and Recompile is a button. The local route is to install a distribution — TeX Live on Windows and Linux, MacTeX on macOS — which puts the real pdflatex program on your machine, and then point any editor at it. Everything in this course works in either place, and nothing about the language changes when you move.

    Choose by what you need this week, not by ambition. If you have never compiled anything, the browser is the better teacher: there is nothing between you and a rendered page, and a .tex file someone sends you is one upload. Move local when you want version control, your own fonts, fast rebuilds on a document hundreds of pages long, or work you have to do on an aeroplane. Whichever you pick, the underlying transaction is a single command run inside a folder, and this lesson walks through it once by hand so that "it does not compile" becomes a diagnosable sentence rather than a mood.

    One fact settles most of the confusion in a first forum post: having LaTeX installed means that a program named pdflatex is reachable from a terminal. Open one and type pdflatex --version. Either a version line comes back or the installation is not finished. An editor without a distribution gives you a text box and nothing else, while a distribution without an editor is entirely usable — you can write LaTeX in Notepad and compile in a terminal, and plenty of people do exactly that for their first month.

    Your first document, line by line

    Save this as test.tex. It is a complete document, not an excerpt: it compiles, it prints one page, and every LaTeX file you will ever open — a letter, a problem set, a thesis — has this same skeleton. The only thing that changes as you get better is what you put inside it.

    Read line one as a question and an answer. \documentclass{article} asks for a layout, and article names the class that supplies it: single-sided, section headings rather than chapter headings, sized for short papers and notes. A class is a file of macro code full of decisions about widths, sizes and spacing, which is exactly why the same body typed under report comes out with chapters, a title page and running heads instead.

    Everything from \documentclass down to \begin{document} is the preamble, and it is configuration only: package lines, title data, your own definitions. The engine does not typeset a single character of it, so a paragraph you leave up there by mistake simply never appears in the PDF — with no error to tell you it was discarded. \begin{document} is where typesetting starts. \end{document} is not decoration either: it finishes unfinished pages, writes the cross-reference data into the .aux file and closes the PDF, so a file that just stops in mid-sentence produces either a truncated document or a run that aborts looking for the end of it.

    \documentclass{article}
    
    \begin{document}
    
    Hello, LaTeX! My first formula is $E = mc^2$.
    
    \[
        \int_0^1 x^2 \, dx = \frac{1}{3}
    \]
    
    \end{document}

    Your first formula

    Compile the document above and the displayed line comes out like this. The source is two commands and a handful of braces; every curve, gap and fraction bar on the page was chosen by the engine, not by you, and it would choose them the same way in a document you did not write.

    Notice that the maths reads aloud almost exactly the way you would say it: minus b, plus or minus, square root of b squared minus four a c, over two a. That property is not luck and it is the reason people tolerate backslashes. When a formula is wrong you can usually find the bad term by reading the source out loud, instead of squinting at a rendered page and guessing which symbol looks off.

    Two habits account for most beginner mathematics. Braces are not decoration: x^2 raises one symbol and x^{10} raises two, and forgetting them is the most common silent maths error in LaTeX, because the file still compiles and the page merely looks slightly strange. Spacing, on the other hand, is not your business: inside $…$ the spaces you type are ignored and the engine inserts its own around every operator, which is why \, and \; exist for the rare deliberate gap — and why a first draft should contain almost none of them.

    x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
    x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
    What renders here is KaTeX working in your browser, so it is a close picture of the output rather than the PDF itself: line-breaking inside a long displayed formula, page breaks and the final kerning only appear once you actually compile.
    Getting Started with LaTeX: First Document, Line by Line — Your first formula
    Rendered output — typeset from the source shown on this page.

    Two modes, one file

    LaTeX is always in one of two modes, and the same character means different things in each. In text mode, letters are letters and spaces are spaces, while _, ^, &, %, # and $ are typographic trouble that needs a backslash in front of it. In maths mode those very characters are the grammar: _ makes a subscript, ^ a superscript, & separates the columns of an alignment, and a Greek letter can only arrive as a command.

    The consequence surprises everyone once. In maths mode a run of letters is not a word, it is a product of variables, each set in its own italic: $alpha$ is a·l·p·h·a, while $\alpha$ is the single letter α. They are similar enough that a beginner does not always see the difference on screen, and it is glaring in print. Words that genuinely are words belong inside \text{…}, and the commands that look like ordinary English abbreviations — \sum, \int, \frac — only exist in maths mode at all, which is what turns a forgotten delimiter into an error message.

    So the first real reading skill is seeing where the boundaries are. Everything between a $ and its partner is maths; everything outside it is text; and \[ … \] opens maths, sets it on its own line and closes it again in one motion. An unbalanced $ is the most common cause of a page that reads perfectly for three paragraphs and then dissolves into nonsense: the engine is still in maths mode, and it has been for the last two thousand characters.

    \alpha \neq alpha \qquad \pi \neq pi
    \alpha \neq alpha \qquad \pi \neq pi
    A literal underscore in a file name or a web address in running text is the same error wearing a different costume: LaTeX is not in maths mode, so it reads the underscore as a subscript operator and complains about it. This course returns to maths mode in depth later; for now the only thing worth memorising is that a formula needs both of its delimiters.

    Braces, brackets and the three shapes of an argument

    Almost every LaTeX command takes its instructions in one of three shapes, and reading them correctly is half of what makes somebody else's source usable to you. Curly braces hold a mandatory argument: \frac{1}{2} can do nothing without both of them, and \documentclass{article} has to be told which class to load. Square brackets hold optional ones, usually switches: \documentclass[12pt,a4paper]{article} asks for the same class with two decisions overridden, and everything you do not mention keeps its default. That is why the same command can appear with two, one or no arguments in three different documents and still be correct.

    The third shape is the environment: \begin{…} and \end{…} in matched pairs, enclosing a region of the document instead of a single object — a displayed equation, a list, a table, the body of the file itself. Environments are the reason a forgotten \end{itemize} produces a message about something several hundred lines away. LaTeX is matching a nesting structure, and it can only report where it gave up, never where you slipped.

    Inside maths, braces also decide how much an operator swallows. ^ and _ take exactly one token — one character, or one {…} group — so x^10 is x to the power one followed by a zero, while x^{10} is the exponent you meant. The same rule makes \frac12 perfectly legal, because the two one-character arguments are obvious to the reader, and \frac12x a matter of luck. None of this is a quirk to memorise: the brace is the grouping operator of the language, in maths, in text and in macro definitions alike.

    x^{10} \neq x^10 \qquad a_{12} \neq a_12
    x^{10} \neq x^10 \qquad a_{12} \neq a_12
    This is the place where LaTeX fails quietly. A missing group usually still compiles and prints x raised to 1 followed by a 0 where you wanted x to the tenth, so when a formula looks oddly shaped, read the output the way you would read a proof and only then go back to the source.
    \frac{1}{2+3} \neq \frac{1}{2} + 3
    \frac{1}{2+3} \neq \frac{1}{2} + 3

    The loop: edit, compile, read

    Under every button there is one command. Put test.tex in a folder, open that folder in a terminal and run the compile line below. If the file is sound, test.pdf appears beside it a second later and you can open it in any reader. That is the whole transaction. An editor contributes nothing except calling this program for you and showing you what it wrote — which is precisely why a project that builds on your laptop builds unchanged on a machine you have never seen.

    The -interaction=nonstopmode flag is the part worth understanding on day one, because its absence looks like a hang. A plain run stops at the first error, prints a ? prompt and waits for a keystroke: nothing seems to be happening because the process is asking you a question. Press x and Enter to end such a run. With the flag the compile finishes whatever it can and leaves the complaints in test.log instead of holding your terminal.

    Keep the loop tight while you are learning: small edits, frequent compiles, and read what came back rather than only what you sent. A change that breaks fifty lines is far easier to localise when it was one of ten small steps than when it was one large one. And remember once you are done that everything the run left behind except the PDF is a build product — the .log, the .aux, later the .toc and .out — so deleting them and compiling again is a legitimate first move, not an act of despair.

    # 1. put the file in a folder, then make that folder the working directory
    cd ~/Documents/latex-test          # Windows PowerShell: cd C:\Users\you\Documents\latex-test
    
    # 2. prove the engine exists before you start blaming the editor
    pdflatex --version                 # a version line, or the install is not finished
    
    # 3. compile
    pdflatex -interaction=nonstopmode test.tex
    
    #    what comes back is a transcript, not a progress bar:
    #      This is pdfTeX, Version 3.141592653-2.6-1.40.26 (TeX Live 2024)
    #      entering extended mode
    #      (./test.tex
    #      LaTeX2e <2024-11-01>
    #      ...
    #      Output written on test.pdf (1 page, 12483 bytes).
    #      Transcript written on test.log.
    
    # 4. open the result, then read the log even when it worked
    open test.pdf                      # Linux: xdg-open   Windows: start test.pdf

    Your first ten minutes, broken on purpose

    The fastest way past the fear is to break the file deliberately and watch what each break costs. Type the document from earlier on this page, get a clean PDF, and then do the four things listed in the listing below one at a time, recompiling after each. Three of the four stop the run with a message that names a line and points at the truth. The fourth — a missing $ — is the dangerous one, because LaTeX sometimes patches the delimiter for you, prints a complaint you will not connect to the sentence, and hands back a page that looks almost right.

    After each failure, open the .log and find the line that starts with !. That is the only message that matters: TeX reports the first thing it could not make sense of and then keeps going, so the twenty lines underneath it are usually fallout from the one mistake you can already see. The number printed with it is where the engine noticed, not where you erred, and the gap is systematic — an unclosed brace sends TeX reading onward until something else ends the group, so it complains at the end of the file. When the number and your memory disagree, look upwards, fix one thing, compile again, and only then read what is left.

    Do this once and two things change permanently. You stop reading a failed compile as a verdict and start reading it as a trace, and you notice that the failures you will actually meet in your first month are not loud at all: a superscript with one character too many, a paragraph that became a list, a symbol from a package you never loaded, all of them compiling merrily into a wrong page. The full catalogue of what each message means, how the log is laid out and how to shrink a failure down to a minimal example is one of the early lessons of this course; what you need in the first hour is only the discipline of the first message.

    \documentclass{article}
    \usepackage{amsmath}                      % (3) misspell this as \userpackage
    
    \begin{document}
    
    \section*{Solve a quadratic}
    
    Solve $ax^2 + bx + c = 0$ and remember      % (2) delete one of these two $ signs
    \[
      x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}   % (1) delete one closing brace here
    \]
    
    The two roots are a pair, not a single number.
    
    \end{document}                             % (4) delete this line

    Lesson cheat sheet

    One key line of code from each section — jump to it or copy it straight from here.

    Section Key code
    \documentclass{article}
    x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
    \alpha \neq alpha \qquad \pi \neq pi
    x^{10} \neq x^10 \qquad a_{12} \neq a_12
    # 1. put the file in a folder, then make that folder the working directory

    Common mistakes

    • Forgetting \end{document}. The engine reads on to the end of the file looking for it, then aborts with ! Emergency stop. and *** (job aborted, no legal \end found). There is no half-built PDF to blame here: the fix is one line at the bottom of the file.
    • Trying to click inside the PDF and type in it. The PDF is output, regenerated from scratch on every run, and any change made to it is gone in a second. Every edit happens in the text source and nowhere else.
    • Putting \usepackage lines after \begin{document}. Packages are preamble-only: LaTeX refuses with ! LaTeX Error: Can be used only in preamble. and skips the line, so each command that package was supposed to provide then arrives as undefined, one complaint at a time.
    • Writing maths without delimiters. alpha outside $…$ is just the word alpha in upright prose, while $\alpha$ is the letter α — and nothing warns you, because the first version is not an error, it is only wrong.
    • Dropping the braces on ^ and _. x^10 and a_12 compile without a word and print an exponent of 1 followed by a stray digit. This is the single most common silent defect in a beginner's maths, and the only defence is reading your own output.
    • Compiling from a terminal that is looking at a different folder, or saving the file as test.txt with the extension hidden. The run starts, reports that the file cannot be found and writes nothing at all — so check the working directory and the real file name before you suspect the distribution.

    Questions about this lesson

    Is LaTeX free, or do I need a licence to publish with it?
    Free in both senses that matter. The TeX engine, the LaTeX macro layer, the class and package files and the distributions that bundle them — TeX Live, MacTeX, MiKTeX — are free software, and you can compile and publish any number of documents without paying anything or registering anywhere. Overleaf has a free tier that covers everything in this course; its paid plans buy collaboration and compile time, not the typesetting itself.
    Can I use LaTeX without installing anything?
    Yes, and for your first week that is usually the better choice. A browser editor runs the compiler on someone else's machine, so you get a real compiled document without a multi-gigabyte download; in exchange, large projects recompile more slowly and you cannot always reach the exact log line you need. The language is identical in both worlds, so starting online costs you nothing when you later move to a local setup — you bring the same .tex files with you.
    What is the real difference between TeX, LaTeX, TeX Live and MiKTeX?
    Four different things with similar names. TeX is the typesetting engine: the program that places boxes and glue on a page. LaTeX is a macro layer on top of TeX that supplies the document commands you actually write, such as \documentclass and \section. TeX Live and MiKTeX are distributions: installers that bundle the engine, the LaTeX layer, thousands of extra packages, the fonts and the command-line tools. So "install LaTeX" means installing a distribution — and the editor you type into is a fifth, unrelated piece of software.
    Do I need to memorise hundreds of LaTeX commands?
    No. A dozen or so carry an entire first document, this course teaches them in order, and everything else you look up — experienced users look things up constantly, which is exactly what the reference pages on this site are for. What is worth memorising is not commands but the shape of the language: mandatory braces, optional square brackets, matched \begin and \end pairs, and the boundary between text mode and maths mode. Once those four feel natural, any new command is a two-second lookup.
    How long does it take to learn LaTeX well enough to write a paper?
    A few hours to produce one page that does not embarrass you, a few weeks to stop looking up every construct, and a few months before you have opinions about spacing. The productive subset is genuinely small: the document class and package lines, sectioning, the two maths delimiters, fractions and sub- and superscripts with their braces, labels and references, and one command for including an image. Everything deeper is document-specific — you learn the bibliography when you have sources and the tables when you have data — and this course is ordered so that each step only needs what came before it.
    How should a complete beginner start learning LaTeX, and in what order?
    Get one page to compile first, and choose whichever route gets you there fastest today — a browser editor needs no installation. Then learn by converting something you already own, such as last week's problem set or a letter, because a real document forces every decision the tutorial can only describe: where the paragraphs break, which parts are maths, what belongs in the preamble. After that the productive order is the minimal document, the text-versus-maths boundary, sectioning with labels and a contents list, figures and tables, and the bibliography last, since it is the part with the most machinery behind it.

    Live playground: edit the LaTeX, see it render

    The preview updates as you type — break it on purpose to see how errors look.

    KaTeX error
    Try an example

    Keep reading

    Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.