IT Services

Extending LaTeX

LaTeX lets you write new commands (or adapt old ones), create or adapt
environments, and write your own packages and even classes.
This document will
give detailed descriptions on how to do simple things, but won't cover
all the issues relating to class creation.

Use \newcommand to create a command and \renewcommand to
over-write an existing one. You can use only letters in the names of commands.

\newcommand -
A completely new command can be created using
\newcommand{\commandname}[number of arguments]{command text, using#1, #2etc to denote arguments}
For example,

\newcommand{\ve}[1]{\(#1_1 ... #1_n\)}
\ve{x}

produces as output
x1 ... xn.
A problem with this example is that it shouldn't change to math mode if
LaTeX is already in that mode. A better try would be

\newcommand{\ve}[1]{\ensuremath{#1_1 ... #1_n}}

which will only change to math mode if it's necessary.
The following weekday macro uses the ifthen
package. Note the use of '%' characters to suppress spaces in the final
output. weekday{3} should print out Wednesday.

\renewcommand -
Let's start with some short though useful examples.
Suppose you wanted to have References in a book
rather than Bibliography. If you look in book.cls you'll
see

\newcommand\bibname{Bibliography}

so adding

\renewcommand{\bibname}{References}

to your file should achieve what you want.
Counters (e.g.figure - a variable that stores the figure
number) have related commands
(e.g.thefigure) to control their appearance, so
they're easy to customise

\renewcommand\thefigure{\roman{figure}}

produces figure numbers in lower case roman numerals.
Longer commands can be adapted too. Remember however, that if the
command involves a @ character you have to enclose your changes in
\makeatletter ... \makeatother or put the code
into a package. Here's an example that
changes the appearance of section headings, making them into centred
small caps -

The following's adapted from the TeX Frequently Asked Questions -
Rather than overwriting an old command it's common to want to add some
code at the beginning or the end of it.
Suppose we want a version of a command that does some
small extension of its original definition: we might try:

\renewcommand{\splat}{addedcode\splat}

However, this would not work: a call to \splat would execute
addedcode, and then call the redefined \splat again; this is an infinite recursive loop.
Fortunately, the TeX primitive \let command comes to our rescue; it allows us to take a "snapshot" of the current state of a command, which we can then
use in the redefinition of the command. So:

\let\Oldsplat\splat
\renewcommand{\splat}{addedcode\Oldsplat}

effects the required patch, safely. Adding things at the end of a command
works similarly.