9

I'm a comp sci prof working on an exam for my students in LaTeX, using the exam class. I like to throw in a few in-jokes here and there. Unfortunately, the class breaks if you try to use 0-indexing on the questions (i.e. set the \thequestion counter to an initial value of -1). So...I thought I'd bypass the problem by making a custom counter display, like \alph,\roman, etc., that just outputs the arabic number one less than the counter.

However, this requires some heavy-duty variable evaluation. I'd like to do something like this:

\renewcommand{\thequestion}{\arabic{question - 1}}

But I don't understand function evaluation in LaTeX very well. Does anybody have an idea, or can at least point me where to start?

3
  • Use \setcounter{question}{-1} at the start and inside the questions environment, and things should be fine. Don't tinker with \thequestion.
    – Werner
    Jul 22, 2013 at 5:25
  • No go. That numbers the questions themselves properly, but breaks the \gradetable. (The author has said that it's a known issue that he should fix, but he hasn't had the time.) The resulting table is missing Question #0, and has an additional Question ?? at the end. Good thought, though.
    – Adam Smith
    Jul 22, 2013 at 6:41
  • I do not quite understand, do you only need the value of the counter displayed? Jul 22, 2013 at 10:51

2 Answers 2

13

All counter representation commands like \arabic or \alph call an associated internal command (\@arabic or \@alph) that takes the number represented by the counter as input. For a counter <counter> this is always a TeX \count named \c@<ounter>. The scheme is always as follows:

\def\cntformat#1{\expandafter\@cntformat\csname c@#1\endcsname}
\def\@cntformat#1{<do something with integer #1 that formats the number>}

where \cntformat stands for a command like \arabic.


Our user command now could look like this (using \newcommand* instead of \def):

\newcommand*\arabicminusone[1]{\expandafter\@arabicminusone\csname c@#1\endcsname}

This command takes an argument (#1, the counter name) from which the associated count is built (\csname c@#1\endcsname). This is then the argument to our internal command \@arabicminusone. In order to build the \c@<counter> before is is parsed by \@arabicminusone the latter needs to be preceded by \expandafter.

The internal command should take an integer as argument and simply substract one. There are a few possibilities to do this, one of them is e-TeX's \numexpr ... \relax. The \relax is not mandatory, strictly speaking, but will do no harm. It prevents \numexpr from scanning further ahead until the number expression ends and is removed from the input stream.

\newcommand*\@arabicminusone[1]{\the\numexpr(#1)-1\relax}

With these two definitions you can now use it to format a counter:

\newcounter{test}
\setcounter{test}{8}
\arabicminusone{test}% will print `7'

A complete example:

\documentclass{exam}

% make @ a letter so we can use it in names of command sequences:
\makeatletter
% call the internal \c@<counter> command and apply number formatting:
\newcommand*\arabicminusone[1]{\expandafter\@arabicminusone\csname c@#1\endcsname}
% number formatting:
\newcommand*\@arabicminusone[1]{\the\numexpr(#1)-1\relax}
% make @ other again:
\makeatother

\renewcommand*\thequestion{\arabicminusone{question}}

\begin{document}
\begin{questions}
  \addpoints
  \question[5] foo
  \question[4] bar
\end{questions}

\gradetable

\end{document}

enter image description here

2
  • I think this one does exactly what I want. Thank you! Could you explain the lines between \makeatletter and \makeatother? It's obvious that you're defining a counter-printing style called arabicminusone, but I'm not sure what the two \def commands are doing. Thanks again.
    – Adam Smith
    Jul 22, 2013 at 19:22
  • @AdamSmith I added some explanation :)
    – cgnieder
    Jul 22, 2013 at 19:59
1

Here is another Idea, if you only want to throw in fake questions, that shall not appear in the \gradetable.

\documentclass{exam}
\newcounter{display}

\begin{document}
\begin{questions}
  \addpoints
  \setcounter{display}{\value{question}}
  \item[\arabic{display}.] (10 points) Fake question...
  \question[10] A foo question
  \question[10] A bar question
  \setcounter{display}{\value{question}}
  \item[\arabic{display}.] (10 points) Annutha fake question...
\end{questions}

\gradetable
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .