From \maketitle to \end{problem}: Making a Custom LaTeX Template for Problem Sheets

As an undergraduate studying mathematics and computer science, I have to do a lot of problem sheets. I used to write them all out by hand (often working in rough and writing it up again neatly afterwards), but my handwriting isn't very neat which occasionally caused headaches for the people marking my work! Given that I am writing my extended essay this year in {% m %} \LaTeX {% em %}, I decided it would be a good idea for legibility and to get some practice if I started typing up my work in {% m %} \LaTeX {% em %}.

After a few weeks of copying and pasting common bits of code between my problem sheet solutions, I decided to move them into a separate .sty file which I could then use to format my work and keep the .tex files a bit neater.

First steps: headers, footers, and title

Before making my own template, I was using one I found here. It was a good start, but by the time I had finished modifying it, very little remained. One thing that did remain, however, is the code for headers and footers. These make use of the fancyhdr package, which is really easy to use and produces some nice results. Here's all the code to put the problem number and page number in the header, and the course name and problem sheet number in the footer:

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{Problem \arabic{probctr}} 
\chead{} 
\rhead{\thepage}
\lfoot{\small\scshape \CourseTitle} 
\cfoot{} 
\rfoot{\footnotesize PS \# \SheetNumber} 
\renewcommand{\headrulewidth}{.3pt} 
\renewcommand{\footrulewidth}{.3pt}
\setlength\voffset{-0.25in}
\setlength\textheight{648pt}

Most of this is straight-forward. lhead, chead, and rhead are used to set the content of the left, centre, and right of the header respectively, and the footer is set up similarly.

From here, I started deviating from the example template I linked to above. For one thing, I wanted to modify maketitle: the standard article class title looks fine, but I wanted to include my class details and marker, and I didn't need to include the date. I grabbed the source from article.cls, and modified it to fit my needs. (See the .sty file itself for the modified code).

This was a really easy but useful modification from the standard article. If you are getting to grips with {% m %} \LaTeX {% em %} and would like to start customising it a bit, this is a great place to start.

Custom environments

Problem sheets are basically just lists of questions and subquestions. It would be tedious to write formatting for each question individually, but a little bit goes a long way towards making the page look less like a block of text and a bit more readable. Following the lead of the template I mentioned earlier, I set up a custom problem environment, which would automatically count and number the problems, and provide a space and some dots after each problem to separate it from the next one. For this, I introduced a counter to count my problems (presumably Jay-Z uses one of these in his LaTeX files too to help him keep track):

\newcounter{probctr}

Then I set up the problem environment to increment the counter and display the correct number with each problem. I also gave it an optional argument for the title of a problem, and it attaches these after the problem number when present. It also sets the spacing for problem solutions to 1.5 spacing (using the setspace package), so the marker has space to write comments and the like:

\newenvironment{problem}[1]{
    \pagebreak[3] % starts a new page if there's not much room on this one
    \refstepcounter{probctr}
    \ifthenelse{\equal{#1}{}}{
    {\large\bf\noindent{Problem \arabic{probctr}}} }{ % Section title without :
    {\large\bf\noindent{Problem \arabic{probctr}: #1}}} % Section title with :
    \vskip .5em
    \onehalfspacing
}
{\singlespacing
\begin{center}\large\bf \ldots\ldots\ldots\end{center}}

Simple as that!

Subproblems tend to be incosistently labelled (with roman numerals and alphabetic characters and so on), and sometimes has sub-subproblems of their own, so I decided to continue labelling them manually (and have them formatted by the subproblem environment). However, at Julie's suggestion I also set up subproblems to be slightly indented from their labels. I think it looks pretty neat, and it wasn't too hard to achieve using minipages. Minipages basically allow you to set up independent parts of the page that can then be repositioned and filled with texts, and can be used to align multiple figures or blocks of texts on the same line.

\newenvironment{subproblem}[1]{
    \pagebreak[2]
    \begin{minipage}[t]{\dimexpr 1cm}{\bf\noindent{(#1.) }}\end{minipage}
    \hfill\begin{minipage}[t]{\dimexpr\textwidth-2cm}
}{
    \xdef\tpd{\the\prevdepth}
    \end{minipage}
    \vskip 1em
}

Easy!

The results

That's pretty much it! Here is an example of it in action on ShareLaTeX (just click 'recompile' to see the PDF output).

You can download the .sty file here.

It's a pretty simple template which does the job for now, and I'll probably keep tinkering with it when I have the time and inclination.