Writing your own homework document class for LaTeX

The last couple of semesters I've normally wrapped up my homework in LaTeX. However, every time I started a new document, I would always go and copy the preamble from previous assignments. This lead to a lot of redundant code, and sometimes I found myself looking for a *.tex file with a specific functionality I've used previously that week.

Secondly, I noticed several redundancies in the document itself across the various homeworks I worked on, like section numbering, table styling, etc., which made the LaTeX code longer and less maintainable than it should be. These redundancies could easily have been removed by creating new commands and environments, which I never bothered to do.

Starting the new semester, I decided to fix these issues, as well as giving my homework documents a consistent look. I have therefore started working on my very own LaTeX document class called homework.

Creating the class

The first thing I did, was to create a file named homework.cls and put it in the directory of the homework I wanted to work on. To this file, I added the following lines:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{homework}  

%% Load base
\LoadClass{article}

\endinput

The new document class could now be used, and work exactly like an article. (must in the same directory as homework.cls)

\documentclass{homework}

Adding class number variable and modifying the title page

All of my classes have a class number, so I decided I wanted to include this as a variable in my homework class. A common way to create a LaTeX variable is to define it as a command, so in addition to the title, date and author, I added \hwClassNumber to my document's preamble, like this:

\author{Haakon Aamdal}
\date{February 2nd, 2015}
\title{Homework 1}
\newcommand{\hwClassNumber}{CSCI 5512}

I wanted the class number to appear on the title page, so in the homework.cls file, I addded the following definition:

\def\maketitle{
    \begin{center}
    \huge
    \textbf{\hwClassNumber\ - \@title} \\
    \Large
    \@author \\
    \@date
    \end{center}
}

Defining tasks and subtasks

One of the things that kept repeating in my previous documents was my following definition of tasks and subtasks:

\section*{Task 1}
\label{task:1}
Task 1 content goes here...

\subsection*{Subtask a}
\label{subtask:1a}

To avoid these repetitions, I decided to create a task environment, with the following definition:

%% Task environment
\newcounter{taskeqcounter} % Create new counter for equations
\newenvironment{task}[1]{

  % Subtask
  \newcommand{\subtask}[1]{
    \subsection*{Subtask ##1}
    \label{subtask:#1##1} % Use double # for nested arguments
  }

  \section*{Task #1} 
  \label{task:#1}
  \setcounter{taskeqcounter}{0} % Reset counter per task

  % Task equation environment
  \newenvironment{taskeq}{
    \refstepcounter{taskeqcounter} % Increment counter
    \renewcommand{\theequation}{#1.\thetaskeqcounter} % Add task to equation numbering
    \begin{equation} 
  }{
    \end{equation} 
  }
}{}

In addition to adding the sections and labels, task defines a sub-environment taskeq for equations. These equations will be numbered with the task name, for instance equation 3 in task 4 will have the number (4.3). Utilizing custom counters requires the etoolbox package which can be included with \RequirePackage{etoolbox}.

Document styling

In my opinion, one of the cons by using LaTeX, is that every document looks so damn LaTeX. There are probably many good reasons for keeping the fonts and margins at their default values, but I would like my documents to look a little "less LaTeX". First, I changed to the arev font by adding \RequirePackage{arev}. Secondly, I decreased the size of the margins with the following code:

%% Page appearance
\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in
\linespread{1.1} % Line spacing

Utility functions

Commands and text that I use often should become commands to save time, like the scientific notation in math. I therefore created a new command \e that sorts this out for me:

\providecommand{\e}[1]{\ensuremath \times 10^{#1}}

Conclusion

The homework document class will hopefully evolve during my semester. I intend using this class for all of my assignments, continiously adding new functionality.

I have not yet figured out where to put homework.cls to make it globally available so I won't have to copy it around, but I guess this is trivial.

The full homework.cls can be downloaded here.