Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Writing LaTeX Classes

24 Sep 2009

LaTeX may look like dark magic. LaTeX classes even more so. In fact, it's straightforward and becomes instrumental when you write several similar documents.

1 Basics

The nice thing about creating classes is that it's fast, convenient and clean. Everything can be carried out as a normal user. All you need to do in the first place is to create e.g. yourclass.cls in ~/texmf/tex/latex/yourclass and run mktexlsr. The rest of it happens in yourclass.cls. Simpler still, you can even keep your class files alongside your TEX files.

First, introduce the name of your class, with an optional date and comments:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{yourclass}[2009/09/24 Your Class]

It's often convenient to build your class on top of an existing one and use existing packages:

\LoadClass[dvips]{article}
\RequirePackage[dvipsnames]{xcolor}
\RequirePackage{listings}
\RequirePackage{hyperref}

You can pass options like you normally do. It's even possible to call setup commands and assign variables:

\lstset{basicstyle=\tt\color{ForestGreen}, frame=single}
\overfullrule=5pt

2 Delayed Settings

Usually, everything you include in the class happens in the preamble. But sometimes, there are commands that need being called after the beginning of the document; this can be done with the AtBeginDocument command:

\AtBeginDocument{
 \pagestyle{empty}
}

3 Pseudo Variables

You may need to set variables in your class, like you set the title with \title or the date with \date. Here's a solution which works. Suppose we want to write an interface to let the user set the document number with the \docno command; we'll actually define a command which defines a hidden command which prints out the information we want:

\newcommand{\docno}[1]{
 \newcommand{\theno}{#1}
}

Behind the scenes, the idea will be to call the \theno command wherever we want the document number to show up, like, for instance, in the heading (we've used package fancyhdr, here):

 \pagestyle{fancy}
 \lhead{Document \theno}

In other words, the user will implicitly define the command which prints out the document number.

4 Error Handling

Here's an example on how to trigger errors when necessary commands have not been called by the user – like, with the above example, when the user failed to specify the document number. The idea is to trigger this error when we're about to use the command that ought be defined by the user calling the right command:

\ifthenelse{\isundefined{\theno}}{
 \ClassError{yourclass}
  {Lecture number is undefined}
  {Do something like \protect\cmno{CM3}}
}

The test is carried out with the \ifthenelse command from the ifthen package. The first argument is the test (we check if the required command has been defined, i.e. if the user called the right command) carried-out by the ifthen-supplied \isundefined command, the second one is the action to take when the test is true, the third one is the action to take if the test fails (you can leave it out if there's no action to take).

In the latter case (i.e. \theno is not defined), an error is triggered with the \ClassError command, whose first argument is the class the error is issued from, the second one is the error message and the third one is the help message to display if the user types h. Note that if you want to write verbatim contents in the messages, you need to call the \protect command beforehand.

5 References