Summary
This lesson takes a LaTeX source apart. It distinguishes the preamble from the body and explains what may go in each and why; it describes commands, with their two kinds of names, their mandatory and optional arguments and their starred forms, and pins down where the name of a command ends; it shows how groups limit the scope of declarations; it presents environments and the error that improper nesting produces; it goes through the ten reserved characters, what each one does and how to print it; it explains comments as a means of disabling code; and it briefly introduces packages and the document class with its options, which the unit on structure will develop.
Learning objectives
- Distinguish the preamble from the body and decide where a command belongs according to what it does.
- Recognize in a command its name, its mandatory and optional arguments and its starred form, and predict where its name ends.
- Predict the scope of a declaration within groups and environments, and write it so that it affects only the intended text.
- Nest environments correctly and interpret the error that improper nesting produces.
- Print each of the ten reserved characters and explain what each one does when it is not protected.
The two parts of a source
Every LaTeX source has the same shape. It begins with \documentclass, which declares the document class. What follows, up to \begin{document}, is the preamble: there the packages are loaded and the decisions that hold for the whole document are made, but nothing is typeset. Between \begin{document} and \end{document} lies the body, the only place where text is typeset. Anything written after \end{document} is ignored altogether.
The division is not a matter of style but a consequence of how LaTeX works. On reaching \begin{document}, LaTeX closes the setup: it now knows which packages are present and which typeface, language and page size are in force, and from that moment on it typesets. Two rules follow, and two errors. First: a package can be loaded only in the preamble, because it might change decisions already applied; a \usepackage in the body produces the error “Can be used only in preamble”. Second: nothing is typeset in the preamble; a piece of text, a section heading or a table of contents written before \begin{document} produces the error “Missing \begin{document}”.
Between the two extremes there are commands that only define or store something, without typesetting it: \newcommand, which defines a new command, or \title, which stores the title until \maketitle prints it. These work in either part, but they belong in the preamble, because there they take effect from the start and whoever reads the source finds them gathered together.
The comments, which begin with %, mark the parts; the engine ignores them. The last line, written after \end{document}, does not appear in the output either.
\documentclass{article} % the class\usepackage{fontspec} % preamble: packages\usepackage[english]{babel}\title{Reading Report} % preamble: data that are stored\author{Alex Morgan}\date{}\begin{document} % the body begins\maketitle\section{Statement of the Problem}The body is the only place where text is typeset.\end{document} % end of the bodyThis line does not appear in the output.The command \date{}, with an empty argument, makes \maketitle print no date, which would otherwise be the date of compilation.
Loading the content…
Completed
Sign in to save your progress