Summary
This lesson teaches how to define commands and environments of one’s own and how to gather them in a preamble that can be reused. It explains why it pays to name the functions of the text instead of repeating their appearance; it presents \newcommand with mandatory arguments and with an optional argument that has a default value, \renewcommand and \providecommand, and the error that arises when a name already exists; it briefly shows \NewDocumentCommand, the modern interface built into the LaTeX kernel; it analyses the space that is lost after a command without arguments; it defines environments with \newenvironment, with their opening and closing code; and it teaches how to move the preamble to a file of its own, either a file read with \input or a .sty package, paying attention to the order in which packages are loaded and to the sobriety of the whole.
Learning objectives
- Justify defining a command of one’s own by the function it names, not by the typing it saves.
- Define commands with mandatory arguments and with an optional argument, and predict their output in each form of use.
- Distinguish
\newcommand,\renewcommandand\providecommand, and interpret the error produced by a name already in use. - Foresee and avoid the loss of the space after a command without arguments.
- Define environments with arguments and recognize which part of the definition can use them.
- Organize a preamble of one’s own in a separate file or in a package, decide which is preferable, and order the loading of packages.
Why define commands of one’s own
The lesson on meaningful formatting set out the central idea: every formatting mark should answer to a function of the text, and a command of one’s own names that function while its definition, written once in the preamble, fixes how it looks. This lesson studies the mechanism in detail. First, though, it is worth stating what is gained, because on that depends when a command is worth defining and when it is not.
Three things are gained. The first is meaning: \concept{being} says what the word is, whereas \textit{being} only says how it looks, and the source reads as an annotated text rather than as a list of typographic orders. The second is consistency: if vectors are always written with \vect, it cannot happen that some come out in bold and others with an arrow through carelessness. The third is that a convention is changed in a single place: if a committee, a journal or a supervisor asks for another notation, one line of the preamble is modified and the change reaches the whole document. From this follows the criterion: what fulfils a recognizable function and recurs deserves a command of its own; an isolated adjustment, which is written in its place and does not recur, does not.
Loading the content…
Completed
Sign in to save your progress