I'm trying to refactor some exam questions to use the exam class. Most of the time I have questions that are broken down into parts within a parts environment. E.g.:

% Some styling stuff

\titledquestion{A really tricky topic}
\part[4] How many wibbles are in a wobbit?
\part[1] Did you just make that up?
\part[2] Are you lying?
\part[1] Sure?
\part[1] Would you like to have a degree?

% ... more questions ...

For such questions it's pretty standard to have a statement near the start of the question along the lines of:

This question consists of \underline{five} (5) parts ((a)---(e))

Where the number ("five") is the count of the parts in the current question's parts environment and the letters ('a' and 'e') refer to the first and last parts in that environment, respectively.

This seems ripe for automating, but I'm not sufficiently acquainted with (La)TeX to do it at present. Any suggestions?

  • We'll need a bit more than this to help you. Without knowing anything about how you're formulating the parts, it will be hard to advise you on integrating with the exam package. – A.Ellett May 7 '15 at 1:34
  • Sorry, wasn't aware there was more than one way. Example provided. – beldaz May 7 '15 at 1:42

The exam class doesn't make this available, but here's some quick code that does what you ask. Since you seem to want to state the number of parts several times, I created a command \setcountertopartsofq{number} that sets the counter numpofq to the number of parts in question number number.

For every part of every question, the exam class defines a control sequence that expands to the number of the page on which that part appears. For example, for part 2 of question 3 it creates \csname Pg@part@3@2\endcsname, and so to find the largest part number for a question we only need to find the largest part number for which such a control sequence has been defined.

Here's a complete LaTeX file that demonstrates this:



% The argument to \numpartsofquestion should be an arabic number that
% is the number of a question.  We set the counter numpofq to the
% number of parts of question number argument.
  % We go to \numpofqrelay to increment numpofq until we find
  % a part number that doesn't exist:
}% setcountertopartsofq

  \expandafter\ifx\csname Pg@part@\qnotemp
    % This part number doesn't exist; back up one and exit:
    % This part number exists; try the next number:
}% numpofqrelay

\def\wordnum#1{\expandafter\word\csname c@#1\endcsname}
  \ifcase #1 zero\or one\or two\or three\or four\or five\or six\or
  seven\or eight\or nine\or ten\or eleven\or twelve\or thirteen\or
  fourteen\or fifteen\or sixteen\or seventeen\or eighteen\or
  nineteen\or twenty\else too many\fi
}% word


  \question This question has\setcountertopartsofq{\arabic{question}}
  \underline{\wordnum{numpofq}} (\arabic{numpofq}) parts,
    \part Why are we here?

    \part Where should we be?

    \part Why aren't they here?

  \question This question, on the other hand,
  \underline{\wordnum{numpofq}} (\arabic{numpofq}) parts,
    \part Where have all the flowers gone?

    \part Why do fools fall in love?

  \question This question has\setcountertopartsofq{\arabic{question}}
  \underline{\wordnum{numpofq}} (\arabic{numpofq}) parts,
    \part Why?

    \part Why not?

    \part Who am I to say?

    \part What did I say?

The command \wordnum{countername} produces the word for the value of the counter countername. It works for up to a value of 20, but you can easily extend that. enter image description here

  • Magnificent! And a beautifully created answer explaining how it works. Many thanks. – beldaz May 7 '15 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.