Your support for our advertisers helps cover the cost of hosting, research, and maintenance of this document

Formatting Information — An introduction to typesetting with LATEX

Chapter 7: Programmability

In this chapter…

  1. Simple replacement macros
  2. Macros using information gathered previously
  3. Macros with arguments
  4. Nested macros
  5. Macros and environments
  6. Reprogramming LATEX’s internals

We’ve touched several times on the ability of LATEX to be reprogrammed. This is one of its central features, and one that still, after nearly a quarter of a century, puts it well above many other typesetting systems, even those with programming systems of their own. It’s also the one that needs most foreknowledge, which is why this chapter is in this position.

TEX is basically a programming language for typesetting. As such, it allows you to define macros, which are little (or large) program-like sequences of commands with a name which can be used as a command itself. This in effect makes a macro a shorthand for a sequence of operation you wish to perform more than once. LATEX is in fact just a large collection of such macros.

Macros can be arbitrarily complex. Many of the ones used in the standard LATEX packages are several pages long, but as we will see, even short one-liners can very simply automate otherwise tedious chores and allow the author to concentrate on the most important thing; writing.

  1. If you move all this Preamble into a package (.sty) file of your own, you don’t need these commands: the use of @ signs in command names is allowed in package and class files. 

  2. Don’t try this at home alone, folks! This one is safe enough, but you should strictly avoid \def for a couple of years. Stick to \newcommand for now.