# How can you make custom counter display types?

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?

-

## migrated from stackoverflow.comJul 22 '13 at 10:41

This question came from our site for professional and enthusiast programmers.

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 '13 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 '13 at 6:41
I do not quite understand, do you only need the value of the counter displayed? – Martin - マーチン Jul 22 '13 at 10:51

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}
\question[5] foo
\question[4] bar
\end{questions}

\end{document}


-
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 '13 at 19:22
@AdamSmith I added some explanation :) – clemens Jul 22 '13 at 19:59

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}
`