Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

Your First LaTeX Document

Write and understand a complete LaTeX file line by line: commands, arguments, environments, comments, paragraphs, whitespace rules and how to compile it.

A LaTeX file is plain text with a little structure. Once you understand the handful of building blocks, every document you ever meet, from a one-page note to a doctoral thesis, will look familiar. This lesson builds a first document, explains each line, and teaches the rules about spaces, paragraphs, comments and reserved characters that trip up almost every beginner.

You can compile every listing in this lesson in your own editor (see installing LaTeX). The site cannot run the real compiler, so the listings are read-only; the editors that appear are small stand-ins that imitate a few commands so you can feel how source and output relate.

The smallest document

LATEX
\documentclass{article}

\begin{document}
Hello, world! This is my first LaTeX document.
\end{document}

Compile it and you get a single page with that sentence and a page number at the bottom. Three parts are present in every document:

  1. \documentclass{article} chooses the kind of document. The class decides page size, font size, heading style and numbering. The next lesson covers others (report, book, letter, beamer).
  2. The preamble is everything between \documentclass and \begin{document}. Here you load packages and set options. This tiny file has an empty preamble.
  3. The body sits between \begin{document} and \end{document}. Only text in the body is printed. Anything after \end{document} is ignored.

Commands, arguments and options

A command starts with a backslash followed by letters: \textbf, \section, \LaTeX. Command names are case sensitive (\Section is not \section). Information passed to a command goes in braces, and optional settings go in square brackets before it.

LATEX
\documentclass[12pt]{article}
%         ^^^^ option   ^^^^^^^ required argument

\textbf{important}   % argument in braces
\\[6pt]              % optional argument in square brackets
  • Required arguments use { }.
  • Optional arguments use [ ] and can usually be left out.
  • A command name ends at the first character that is not a letter. A space after a letter-command is swallowed, so \LaTeX is fun prints "LaTeXis fun". Write \LaTeX{} is fun or \LaTeX\ is fun to keep the space.

Environments

An environment applies something to a block of text. It opens with \begin{name} and closes with \end{name}, and the names must match.

LATEX
\begin{center}
  This line is centered.
\end{center}

\begin{quote}
  An indented quotation.
\end{quote}

Environments can be nested, but they must be closed in reverse order. The most important environment is document; later you will meet itemize and enumerate (lists), tabular (tables), figure, equation and many more.

Paragraphs, spaces and line breaks

LaTeX treats whitespace in its own way, and this is a frequent surprise.

  • Many spaces equal one space. Typing five spaces, a tab or a newline between words all produce a single space.
  • One line break in the source is just a space. Break your source lines wherever you like; the output will wrap by itself.
  • A blank line starts a new paragraph. Leave one or more empty lines to separate paragraphs. LaTeX indents the first line of each paragraph automatically.
  • Do not use blank lines for vertical space. More than one blank line is the same as one.
  • \\ forces a line break inside a paragraph, and \newline does the same. Use it sparingly, for addresses or poems, never to make paragraphs.
  • \par is a visible way to write a paragraph break.

Try these rules in the stand-in below. It understands only \section, \textbf, \emph, \\, comments and blank lines, but it follows the same whitespace rules: extra spaces collapse, single newlines are ignored, and blank lines make new paragraphs.

Whitespace and paragraphs (stand-in, not a real compiler)

Delete the blank line before "A blank line made..." and watch two paragraphs merge into one. The real LaTeX does the same.

Comments

A percent sign % starts a comment: everything from % to the end of the line is ignored. Use comments to explain the source, or to temporarily switch a line off while testing.

LATEX
Today we start. % this note is not printed
% \textbf{This whole line is switched off}

A comment also swallows the line break, which is occasionally useful to stop a stray space appearing at a line end inside a macro.

Special (reserved) characters

Ten characters have a meaning in LaTeX, so you cannot type them directly and expect them to print.

You want Type Why
% \% % starts a comment
$ \$ $ switches to math mode
& \& & separates table cells
# \# # marks macro parameters
_ \_ _ makes a subscript in math
{ } \{ \} braces group text
^ \^{} ^ makes a superscript in math
~ \~{} ~ is a non-breaking space
\ \textbackslash{} backslash starts a command

A frequent first error is "Missing $ inserted", caused by typing an underscore in a file name such as my_file outside math mode. Escape it: my\_file. The special characters lesson goes deeper.

Quotes and dashes

Typewriter quotes "like this" print as two closing quotes. LaTeX wants two backticks to open and two apostrophes to close: ``like this''. Single quotes work the same with one character each: `single'.

Dashes come in three lengths, written with one, two or three hyphens: - hyphen (well-known), -- en dash (pages 10--12), --- em dash (a pause---like this).

Compiling

Press the compile button in your editor, or run the engine in a terminal.

Terminal
pdflatex hello.tex

The engine reads hello.tex, prints a log to the terminal and writes hello.pdf, plus helper files hello.log and hello.aux. If the log ends with "Output written on hello.pdf", it worked. If it stops with a line starting !, it hit an error; the message names the problem and the line number. The debugging lesson in chapter 8 covers this in depth. For now, remember: fix the first error, then recompile, because later errors are often caused by the first.

A slightly bigger document

Here is a complete small article that uses commands from this lesson plus a title, built from the pieces you will learn in the next chapter.

LATEX
\documentclass[11pt]{article}

\title{My First Article}
\author{Ada Student}
\date{\today}

\begin{document}
\maketitle

\section{Introduction}
LaTeX makes \textbf{structure} easy. Write the content, and let
the compiler handle the layout.

\section{Conclusion}
Blank lines separate paragraphs; \emph{commands} start with a backslash.
\end{document}

\maketitle prints the title block from \title, \author and \date, and \section produces numbered headings automatically: add a section in the middle and every later number updates by itself.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do you load packages and set options?

    The preamble is everything between \documentclass and \begin{document}.
  2. 2. How do you start a new paragraph in the source?

    A blank line ends a paragraph. A single newline is only a space, and \ is a forced line break.
  3. 3. What does the % character do?

    Use % to print a percent sign.
  4. 4. How do you print the underscore in my_file?

    The underscore is reserved for subscripts, so you escape it with a backslash.

Summary

  • A document has a class, a preamble and a body between \begin{document} and \end{document}.
  • Commands start with \; required arguments use { }, optional ones use [ ].
  • Environments are \begin{name} ... \end{name} pairs that must match and nest.
  • Whitespace collapses; a blank line starts a new paragraph; \\ forces a line break.
  • % begins a comment; ten reserved characters need escaping.
  • Fix the first compile error first.

Next: Document Classes and the Preamble