Exams & theses

Teachers who end up writing exams in LaTeX usually get there for one reason: keep the paper and the answer key in two files and sooner or later they will disagree. You fix a question and forget the solution; you change the points and forget the total. The exam class solves that structurally. The solution is written right next to the question, and the presence or absence of a single \printanswers produces two different PDFs from one source — the blank paper and the answer key. Measured on a real document: two pages with answers, three without. Same file, different documents. This page covers the exam class and the other genre with rigid structure, the thesis: how report, book and memoir differ, which classes leave \frontmatter undefined, and the single most useful piece of advice — do not fight your university's template.

The skeleton of the exam class: \question, parts, points

The structure of the exam class is a three-level nest: \question inside a questions environment, \part inside a parts environment below it, and \subpart inside subparts below that. Points go in square brackets — \question[10] — and the class option addpoints makes it total them for you. The counts are available in the body as \numquestions, \numpoints and \numpages, so the sentence "4 questions, 30 points, 3 pages" never has to be updated by hand. Multiple choice comes in four environments: choices stacks them vertically, oneparchoices runs them along one line, and checkboxes / oneparcheckboxes add a box to tick. The right option is written not with \choice but with \CorrectChoice, which is what makes the answer key below work.

Where the points are printed is finely adjustable. By default they follow the question number, but \pointsinmargin and \pointsinrightmargin push them into the margin, and \pointsdroppedatright turns the right margin into a column for recording points deducted. Replacing the word "points" with another word is a one-line redefinition of \pointname. Headers and footers, once you select \pagestyle{headandfoot}, are written in three parts — \header{left}{centre}{right} and \footer{left}{centre}{right} — and \firstpageheader / \runningheader let the first page differ from the rest. Printing "Page 2 of 3" is standard equipment here, and that alone lets you spot the script that is missing a sheet.

latex
\documentclass[addpoints,12pt]{exam}
\pagestyle{headandfoot}
\header{Calculus I --- Midterm}{}{Page \thepage\ of \numpages}
\begin{document}
\begin{center}\gradetable[v][questions]\end{center}

\begin{questions}
  \question[10] Differentiate $f(x)=x^3$.

  \question[5] Pick the correct answer.
  \begin{oneparchoices}
    \choice wrong \CorrectChoice right \choice wrong
  \end{oneparchoices}

  \question Answer both parts.
  \begin{parts}
    \part[3] State the definition.
    \part[7] Prove the claim.
  \end{parts}
\end{questions}
\end{document}

What \printanswers changes: one source, paper and answer key

\printanswers is the switch for whether solutions appear, but the interesting part is what takes their place when they are hidden. A solution environment simply prints or does not print. Write solutionorlines[2.5in] instead and you get the solution when answers are on, and 2.5 inches of ruled lines when they are off. The same idea gives solutionorbox (a frame), solutionordottedlines (dotted rules) and solutionorgrid (graph paper). Compiling one document — six questions, every one wrapped in solutionorlines[2.5in] — both ways gives two pages with \printanswers and three with \noprintanswers. The solution text appears six times in the first and not once in the second. The extra page is exactly the writing space the hidden solutions were replaced with.

latex
% One source, two documents.
%   \printanswers   -> the key   (measured: 2 pages, 6 solutions printed)
%   \noprintanswers -> the paper (measured: 3 pages, 0 solutions printed)
\documentclass[addpoints,12pt]{exam}
\printanswers          % comment this line out to get the blank paper
\begin{document}
\begin{questions}
  \question[10] State the definition and prove the claim.
  \begin{solutionorlines}[2.5in]
    $f'(x)=3x^2$ by the power rule.
  \end{solutionorlines}
\end{questions}
\end{document}

In practice it helps to know there are two ways to flip the switch: writing \printanswers in the source, or passing the class option [answers]. The output is identical — checked on the same document, both give two pages with six solutions. The option route leaves the body untouched, so a latexmk rule or a Makefile can emit both PDFs through \PassOptionsToClass without editing anything. There is also cancelspace, which prints neither the solutions nor the space reserved for them: it ignores the bracketed argument of the solutionor… environments entirely and gives you the questions alone, which is exactly what you want when handing out a past paper as a study sheet. Note that cancelspace has no effect at all while answers are being printed.

Command / environmentWith printanswersWith noprintanswers
solutionprints the solutionprints nothing
solutionorlinesprints the solutionruled lines of the given height
solutionorboxprints the solutionan empty frame of the given height
solutionorgridprints the solutiongraph paper of the given height
\fillwithlinesprints ruled lines regardless of the switchthe same
cancelspaceno effectignores the bracketed argument, leaving no space

Printing a grading table: \gradetable and \pointsonpage

One line, \gradetable[v][questions], puts a grading table of question numbers, points and blank score boxes on the cover page. The first argument is the orientation (v vertical, h horizontal) and the second the axis: questions gives one row per question, pages one row per page. The page-indexed table earns its keep when several people mark together, because it maps directly onto "you take page 3". The arithmetic is available too: \pointsofquestion{3} returns the points for question 3 and \pointsonpage{2} the total on page 2. Used with the addpoints option, this means changing a point value updates the table and the totals at the same time. Not adding numbers up by hand is the whole value of the feature. Bonus questions get \bonuspoints and their own bonus tables.

How seriously the exam class is built shows in the thickness of its manual. The examdoc.pdf in TeX Live 2024 runs to 135 pages — a 135-page user's guide for a class whose entire purpose is setting exams. It is written by Philip Hirschhorn of the Department of Mathematics at Wellesley College, a topologist known for Model Categories and Their Localizations. The copyright line on the manual lists the revision years 1994, 1997, 2000, 2004, 2008, 2011, 2015, 2017, 2021, 2023: this is a class one mathematician has been maintaining for nearly thirty years, for his own teaching. The version bundled with TeX Live 2024 is v2.704, dated 9 July 2023.

Thesis templates: do not fight the one your university hands you

The conclusion first: if your university distributes a class or a template, use it, and modify it as little as you can. The reason is not typographic but procedural. Margins, leading, the wording of the title page, the numbering style of figures and tables are, at most institutions, written into the submission requirements, and a thesis that departs from them is sent back regardless of its content — usually on the day of the deadline. TeX Live 2024 alone contains 64 directories whose names include thesis or diss and that ship a .cls: mitthesis, ucbthesis, uiucthesis, thuthesis, njuthesis, ut-thesis, york-thesis and so on. The local templates that never reach CTAN are far more numerous still, and most of them circulate as "the .cls I inherited from the student before me".

So what is such a template, underneath? In almost every case, a class that puts a prescribed title page and page geometry on top of book or report. That is why \chapter and \tableofcontents behave normally in it — and conversely, why a template that appears broken usually has something wrong in its foundation. If nothing is mandated and you are choosing for yourself, a thesis with chapters wants report or book; for finer control over the layout, memoir (v3.8.2 in TeX Live 2024, dated 26 January 2024, which describes itself as a "configurable book, report, article document class") or KOMA-Script's scrbook / scrreprt. The next section looks at how they differ in terms of what is actually defined in each.

The difference between report and book: sides, and where a chapter starts

The difference is spelled out in the default options. book.cls says \ExecuteOptions{letterpaper,10pt,twoside,onecolumn,final,openright}; report.cls has the same line with oneside and openany. So book is set for two-sided printing and every chapter starts on an odd (right-hand) page, which sometimes means an inserted blank page. report is one-sided and a chapter simply starts on the next page. For a thesis that will be bound, book's behaviour is the correct one; for a PDF submission that nobody will print double-sided, report is the more straightforward choice. article has no \chapter at all — whether you need \chapter is the first fork in choosing a class.

ClassDefault sidesChapter openingfrontmatter
articleonesideno \chapter at allundefined
reportonesideopenany (the next page)undefined
booktwosideopenright (an odd page)defined
memoiras book (configurable)openright (configurable)defined
scrbooktwosideopenrightdefined
scrreprtonesideopenanyundefined

Why \frontmatter gives ! Undefined control sequence, and what the three really do

\frontmatter, \mainmatter and \backmatter exist only in the book-derived classes. Write \frontmatter under \documentclass{report} and you get ! Undefined control sequence. with the offending line shown as l.3 \frontmatter. Searching the class files bears this out: the string frontmatter occurs in book.cls, memoir.cls and scrbook.cls, and does not occur in article.cls, report.cls or scrreprt.cls. Most reports of "\frontmatter doesn't work in my university template" come down to this — the template was built on report. If you do not need the chapter-numbering behaviour, writing \pagenumbering{roman} and \pagenumbering{arabic} by hand gives the same result, because that is very nearly all the three commands contain.

What do they contain, exactly? In book.cls: \frontmatter does a \cleardoublepage, sets \@mainmatterfalse and \pagenumbering{roman}. \mainmatter does a \cleardoublepage, sets \@mainmattertrue and \pagenumbering{arabic}. And \backmatter clears the page and sets \@mainmatterfalse — it does not touch the page numbering at all. This is widely misreported; any explanation claiming that the back matter changes the numbering is wrong. What \@mainmatter being false actually changes is the behaviour of \@chapter: the chapter counter is not stepped, no number is printed in the heading, and the table-of-contents entry is added without a \numberline. So in the front and back matter chapters come out unnumbered but still appear in the contents — acknowledgements and bibliography carry no number yet remain reachable from the ToC. That whole convention of thesis writing rides on this one switch.

latex
% Measured page labels from this exact file (book, 12pt):
%   front matter -> i, ii, iii, iv   (roman, chapters unnumbered but in the ToC)
%   main matter  -> 1, 2, 3, 4       (arabic, "Chapter 1", "Chapter 2")
%   back matter  -> 5                (still arabic; chapter unnumbered)
\documentclass[12pt]{book}   % or your university's class
\begin{document}
\frontmatter
\chapter{Acknowledgements}
Thanks.
\tableofcontents

\mainmatter
\include{chapters/intro}
\include{chapters/method}

\backmatter
\chapter{Bibliography}
Refs.
\end{document}

In practice: split the chapters, and do not break it the night before

The first thing to do with a long thesis is to split it into one file per chapter and pull them in with \include. \include starts a new page before and after each file, which suits chapter boundaries, and \includeonly{chapters/method} then lets you typeset a single chapter without the others while keeping page numbers and cross-references correct. On a 300-page thesis where only chapter 7 needs a fix, having that or not changes the wait by an order of magnitude. Splitting up a project and handling large documents each have their own page here; follow the related links. One more habit that pays off near a deadline: branch before you touch the layout. The urge to shave a millimetre off the margin arrives, almost without exception, on the last night.

  • Put the abstract in an abstract environment — but many mandated templates want \chapter*{Abstract} in book-derived classes, so check the distributed style guide first.
  • For an unnumbered chapter that should still appear in the contents, follow \chapter*{...} with \addcontentsline{toc}{chapter}{...}. Inside \frontmatter, plain \chapter already does this for you.
  • Files pulled in with \include must not contain \documentclass or \begin{document}; putting one there gives ! LaTeX Error: Can be used only in preamble.
  • Always do one clean build before you submit. A stale .aux will happily keep a cross-reference you have already deleted, and the run will succeed.
  • If you did modify the university class, leave a comment saying what you changed. Both you in three months and the student after you will end up reading it.

One last thing the two genres share. An exam and a thesis are both documents whose content and presentation are governed by different people — the setter and the marker in one case, the author and the examining institution in the other. LaTeX is strong here not because the typesetting is beautiful but because that separation can be expressed directly in the structure of the source. One \printanswers divides the paper from the key; one \frontmatter divides the numbered chapters from the unnumbered ones. Whether a fork can be folded down into a single switch is what you feel on the night before the deadline.