Professional Documents
Culture Documents
Peter Flynn
Silmaril Consultants
peter (at) silmaril dot ie
http://silmaril.ie/cgi-bin/blog
Abstract
Document classes in LATEX provide automation to improve consistency, produc-
tivity, and accuracy in creating and maintaining documents, thereby avoiding the
inefficiencies of wordprocessors. However, users who want to package their macros
or applications as a document class are often put off by the apparent complexity
of the sample classes in the standard distribution. This paper describes what the
code in the article document class file does and suggests solutions to some of the
popular requirements for changes.
110 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
and the user who has examined the following sec- 2.1 Version and identification
tions should have no difficulty in identifying the dif- The first thing a document class or package must do
ferences. is identify itself by name, and specify the oldest ver-
The letter class, however, is a very different ani- sion of LATEX with which it will work (it is assumed
mal. It implements a vertically-centered format once that it will therefore work with all later versions).
common in typewritten letters but rarely seen nowa- article.cls
days, and has no provision for many of the features 54 \NeedsTeXFormat{LaTeX2e}[1995/12/01]
users expect to be able to find in a letter template. 55 \ProvidesClass{article}
For this reason I do not refer any further to this 56 [2004/02/16 v1.4f
format. 57 Standard LaTeX document class]
The ConTEXt system implements a different
and extensible set of document classes including In your new document class file you should set
letters in a radically different manner to LATEX the date and version to the earliest version you have
and has been discussed and presented extensively in tested your code with (probably your current ver-
recent years. The Eplain macros implement many sion). The name of the document class being pro-
of the features of the LATEX internal mechanisms, vided gets checked against the name requested in the
but without imposing any document format at all, \documentclass declaration, and LATEX will give a
leaving the plain TEX user free to write those herself. warning if there is a discrepancy. You may provide a
label for the class as well: this will appear in the log
1.3 More background
file. The linebreaks and indentation are for human
The essential documentation to read before you start readability only.
writing your own classes is LATEX 2 for class and
package writers [8] (available on all modern TEX \NeedsTeXFormat{LaTeX2e}[1995/12/01]
installations by typing texdoc clsguide, and The \ProvidesClass{ladingbill} [2006/07/01 v0.01 Bill of Lading
LATEX Companion [6, App : A.4]. These describe in specialist LaTeX document class]
detail the additional commands available to class
and package authors. There are also some spe- 2.2 Initial code and compatibility
cial declarations explained in Companion [6, p. 847].
The article by Hefferon [3] which I refer to later is a On a number of occasions, classes define values as
good example of how to build on an existing class. null or a default for later use, so that subsequent
If you have to deal with an obsolete LATEX 2.09 style code wont trip up as it would if they were undefined.
file, there is an older paper in TUGboat [1]. In most cases you will probably need to keep the
internal definitions (such as \@ptsize here) for use
2 Dissection of article.cls later on (see section 2.4.1 on p. 113).2
article.cls
In this example, we use the file from the TEX Live
58 \newcommand\@ptsize{}
2005 distribution (so the line numbers refer to that
59 \newif\if@restonecol
version only). Lines 153 are comments and are
60 \newif\if@titlepage
omitted here for brevity: they explain where the file
61 \@titlepagefalse
came from and how it can be used. This is auto-
generated because the document class and pack- The conditionals \if@restonecol (which flags
age files in the standard distributions of LATEX are the restoration of one-column layout after using
derived from master copies maintained in docTEX LATEXs built-in two-column usage, as distinct from
(.dtx) format [7], which combines documentation using the multicol package) and \if@titlepage
and LATEX code in a single file, much in the same (which flags use of the separate title-page layout
way that Knuths WEB system does for many pro- set to false in the following line) are used in the
gramming languages [9].1 A short explanation of default \maketitle command in section 2.4.4 on
the sources of the class files is in the TEX FAQ [2,
2 The use of the @ sign may be unfamiliar to newcomers:
label : ltxcmds].
in normal LATEX it is classified as an other character [4,
p. 37]. This means it cannot be used as part of a control
sequence (command) in your document. But in class and
1 If you intend making your document class available to package files, LATEX reclassifies it as a letter, and uses it in
the rest of the LATEX community (eg via CTAN), you should command definitions which are intended to be inaccessible
make it a docTEX document so that you can combine docu- to the normal user. Its use here indicates that the \@ptsize
mentation with your code. Actually, you should probably be command is going to be given a value that the end-user should
doing this anyway. . . not be able to interfere with, or even know exists.
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 111
Peter Flynn
p. 116. If youre planning to rewrite \maketitle to In some cases you may be writing for a highly
your own design you may need to take these condi- specific environment such as your own office or em-
tionals into account.3 ployer, where only one size is required. If so, just
If you are going to invoke additional pack- omit all the other declarations and add the one op-
ages to provide facilities needed by your options, tion to the \ExecuteOptions command (see sec-
use the \RequirePackage command here, before tion 2.3.4 on p. 113).
the options section. If the additional packages are
unconnected with your option definitions, use the 2.3.2 Type sizes and layout options
\RequirePackage command after the options are As mentioned above, the compatibility settings in
executed (see section 2.3.4 on p. 113). this block can be removed in your own class, because
modern class files use default option settings via the
2.3 Options \DeclareOption command instead.
In an ideal world we wouldnt have to support obso- article.cls
lete versions of software, but the LATEX defaults still 86 \if@compatibility
allow v2.09-type \documentstyle declarations to be 87 \renewcommand\@ptsize{0}
processed, with a warning. However, for a modern 88 \else
class file this is not necessary, so in your own class 89 \DeclareOption{10pt}{\renewcommand\@ptsize{0}}
you can omit all the tests for \@ifcompatibility 90 \fi
and their \else and terminating \fi commands, 91 \DeclareOption{11pt}{\renewcommand\@ptsize{1}}
here and throughout, leaving just the code that was 92 \DeclareOption{12pt}{\renewcommand\@ptsize{2}}
in the \else blocks. 93 \if@compatibility\else
94 \DeclareOption{oneside}{\@twosidefalse \@mparswitchfalse}
2.3.1 Paper sizes 95 \fi
How many paper size options you want to support 96 \DeclareOption{twoside}{\@twosidetrue \@mparswitchtrue}
in your class is entirely up to you. You should allow 97 \DeclareOption{draft}{\setlength\overfullrule{5pt}}
at least A4 and Letter for normal office work. 98 \if@compatibility\else
article.cls 99 \DeclareOption{final}{\setlength\overfullrule{0pt}}
62 \if@compatibility\else 100 \fi
63 \DeclareOption{a4paper}
64 {\setlength\paperheight {297mm}% 101 \DeclareOption{titlepage}{\@titlepagetrue}
65 \setlength\paperwidth {210mm}} 102 \if@compatibility\else
66 \DeclareOption{a5paper} 103 \DeclareOption{notitlepage}{\@titlepagefalse}
67 {\setlength\paperheight {210mm}% 104 \fi
68 \setlength\paperwidth {148mm}}
69 \DeclareOption{b5paper} 105 \if@compatibility\else
70 {\setlength\paperheight {250mm}% 106 \DeclareOption{onecolumn}{\@twocolumnfalse}
71 \setlength\paperwidth {176mm}} 107 \fi
72 \DeclareOption{letterpaper}
108 \DeclareOption{twocolumn}{\@twocolumntrue}
73 {\setlength\paperheight {11in}%
74 \setlength\paperwidth {8.5in}} 109 \DeclareOption{leqno}{\input{leqno.clo}}
75 \DeclareOption{legalpaper} 110 \DeclareOption{fleqn}{\input{fleqn.clo}}
76 {\setlength\paperheight {14in}% 111 \DeclareOption{openbib}{%
77 \setlength\paperwidth {8.5in}}
78 \DeclareOption{executivepaper}
112 \AtEndOfPackage{%
79 {\setlength\paperheight {10.5in}% 113 \renewcommand\@openbib@code{%
80 \setlength\paperwidth {7.25in}} 114 \advance\leftmargin\bibindent
81 \DeclareOption{landscape} 115 \itemindent -\bibindent
82 {\setlength\@tempdima {\paperheight}%
83 \setlength\paperheight {\paperwidth}%
116 \listparindent \itemindent
84 \setlength\paperwidth {\@tempdima}} 117 \parsep \z@
85 \fi 118 }%
119 \renewcommand\newblock{\par}}%
3
120 }
How much to cater for and how much to ignore will
depend on how much your class deviates from the default.
Many LATEX users will expect to be able to use options like The other options should probably be retained,
twocolumn and titlepage simply because they are available as users may expect them to work, bearing in mind
in the default classes. But if you are writing a much more
the comment about two-column and title-page set-
prescriptive format, you may want to remove these options
entirely, which means removing all references to conditional tings above. Note that the openbib declaration is
flags which depend on them. 10 lines long, and defers itself to end of the package
112 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 113
Peter Flynn
114 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 115
Peter Flynn
116 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
210 \global\let\and\relax You can also make up your own additional ele-
211 } ments, for example an optional subtitle:
212 \else
213 \newcommand\maketitle{\par
\def\@subtitle{\relax}
214 \begingroup
215 \renewcommand\thefootnote{\@fnsymbol\c@footnote}% \newcommand{\subtitle}[1]{\gdef\@subtitle{#1}}
216 \def\@makefnmark{\rlap{\@textsuperscript{\normalfont\@thefnmark}}}% \renewcommand{\maketitle}{
217 \long\def\@makefntext##1{\parindent 1em\noindent \begin{titlepage}
218 \hb@xt@1.8em{%
219 \hss\@textsuperscript{\normalfont\@thefnmark}}##1}%
\huge\@author\par
220 \if@twocolumn \Large\@title\par
221 \ifnum \col@number=\@ne \if\@subtitle\relax\else\large\@subtitle\par\fi
222 \@maketitle \normalsize\@date\par
223 \else
224 \twocolumn[\@maketitle]%
\end{titlepage}
225 \fi }
226 \else
227 \newpage This lets the phantom \@subtitle exist unused,
228 \global\@topnum\z@ % Prevents figures from going at top of page.
229 \@maketitle
set to \relax unless an author explicitly uses the
230 \fi \subtitle command, because the titling routine
231 \thispagestyle{plain}\@thanks can test whether it is still set to \relax, and if not,
232 \endgroup format it accordingly. This technique can be used
233 \setcounter{footnote}{0}%
234 \global\let\thanks\relax to add many of the items of metadata used by pub-
235 \global\let\maketitle\relax lishers, such as author affiliations, email and web
236 \global\let\@maketitle\relax addresses, and dates of submission.
237 \global\let\@thanks\@empty
238 \global\let\@author\@empty 2.5 Structure
239 \global\let\@date\@empty
240 \global\let\@title\@empty Unless you are doing a very rigid class for data-
241 \global\let\title\relax handling, you probably want to keep the basic sec-
242 \global\let\author\relax
tional structures for normal continuous text as they
243 \global\let\date\relax
244 \global\let\and\relax are, and only change the formatting.
245 } article.cls
246 \def\@maketitle{% 265 \setcounter{secnumdepth}{3}
247 \newpage 266 \newcounter {part}
248 \null 267 \newcounter {section}
249 \vskip 2em% 268 \newcounter {subsection}[section]
250 \begin{center}% 269 \newcounter {subsubsection}[subsection]
251 \let \footnote \thanks 270 \newcounter {paragraph}[subsubsection]
252 {\LARGE \@title \par}% 271 \newcounter {subparagraph}[paragraph]
253 \vskip 1.5em% 272 \renewcommand \thepart {\@Roman\c@part}
254 {\large 273 \renewcommand \thesection {\@arabic\c@section}
255 \lineskip .5em% 274 \renewcommand\thesubsection {\thesection.\@arabic\c@subsection}
256 \begin{tabular}[t]{c}% 275 \renewcommand\thesubsubsection{\thesubsection .\@arabic\c@subsubsection}
257 \@author 276 \renewcommand\theparagraph {\thesubsubsection.\@arabic\c@paragraph}
258 \end{tabular}\par}% 277 \renewcommand\thesubparagraph {\theparagraph.\@arabic\c@subparagraph}
259 \vskip 1em% 278 \newcommand\part{%
260 {\large \@date}% 279 \if@noskipsec \leavevmode \fi
261 \end{center}% 280 \par
262 \par 281 \addvspace{4ex}%
263 \vskip 1.5em} 282 \@afterindentfalse
264 \fi 283 \secdef\@part\@spart}
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 117
Peter Flynn
118 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 119
Peter Flynn
120 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 121
Peter Flynn
122 TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference
Rolling your own Document Class: Using LATEX to keep away from the Dark Side
TUGboat, Volume 28 (2007), No. 1 Proceedings of the Practical TEX 2006 Conference 123