2009-04-06

Creating your own LaTeX document class

I've been using LaTeX for a few years, and every time I make a new document, I always start by copying a similar document I've made in the past. So over time, the preamble to each document—the header, where formatting and package includes go—has kept getting bigger. And since I frequently create new commands to simplify things while I'm writing a document, those document-specific commands end up in unrelated documents.

For example, I wrote my IB Extended Essay last year, and the formatting I used (1-inch margins, ruled headers and footers with my IB candidate number, 3-point paragraph skips, etc.) became my standard formatting for all IB documents. So whenever I was starting a new IB assignment, I copied my Extended Essay, often neglecting to delete the unnecessary document-specific commands like the following:
\newcommand{\class}[1]{\texttt{#1}}
\newcommand{\method}[2]{\texttt{#1.#2}}
\newcommand{\note}[1]{\textit{#1}}
\newcommand{\coderef}[2]{\ref{#1}, page \pageref{#1:#2}, line \ref{#1:#2}}
So I finally decided to package the common document types I use into custom LaTeX classes, just like the built-in article.cls class. So far, I have two classes, interlake-assignment.cls and ib-assignment.cls. Both of those let you define several fields like candidatenum and wordcount that then get printed out into an appropriately formatted title section.

That makes it possible for me to start a fully-formatted new assignment with just a few lines:
\documentclass{ib-assignment}
\title{Modeling the Course of a Viral Illness and Its Treatment}
\subtitle{IB Math HL Type II Portfolio}
\author{Ankur Dave}
\candidatenum{0844-028}
\updateheaders % a bit of a kludge to get the title working properly

... % document-specific packages and macros

\begin{document}
\maketitle

... % document body

\end{document}
Here's the source for the two classes, as well as a screenshot of each one being used:
ib-assignment.cls
interlake-assignment.cls
(Update 2009-04-20: I fixed the links; thanks to Lincoln Berkley from New Zealand for pointing out that they were broken.)

6 comments:

Mildawg said...

Ok, I recently decided to type up Linguistics and CSE homework, not only math. I agree about the header growing bigger and bigger with time. Hence, this content is Relative To My Life. tyvm.

Anonymous said...

Ankur,

I am a very new LaTeX user who was looking for a good way to implement a class for writing essays for schoolwork etc. on LyX. Your IB class served as an excellent reference as I wrote this class, and I would like to thank you for posting it. Keep up the great work!

Best,
Phil

easy said...

Ankur, thank you, your code helped me to create class that i had needed.(sorry for my bad english :) )

Star Girl said...
This comment has been removed by the author.
Anonymous said...

I’d passion to canary that too!

Anonymous said...

The links are broken again. Please upload these classes again :)