0

In a previous question I asked how to count the total number of questions in an exam. Now I am curious how to count the number of questions types - e.g. multiple choice and short answer.

I borrowed from @samcarter's nice solution and defined unique counters for multiple choice, short answer and all questions using etoolbox. However, they do not work as planned. There are 5 total questions. The total question counter (thenumquestions) is correct, and so is the short answer question counter (thesa), but the multiple choice question counter (\themc) is counting all questions.

The trouble is that all questions are defined by \begin{question}, and I am trying to count those instances separately within each question type (\begin{multiplechoice} and \begin{shortanswer}).

I also tried to borrow from this solution to unique counting in the exam class by defining different environments for different questions, but it throws an error.

What am I doing wrong?

\documentclass[10pt]{examdesign}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\SectionFont{\large\bfseries\ttfamily}
\ContinuousNumbering
\Fullpages
\NoKey
\NumberOfVersions{1}

\let\namedata\relax

\usepackage{etoolbox}

\newcounter{numquestions}
\setcounter{numquestions}{0}
\AtBeginEnvironment{question}{\addtocounter{numquestions}{1}}

\newcounter{mc}
\setcounter{mc}{0}
\AtBeginEnvironment{multiplechoice}{\AtBeginEnvironment{question}{\addtocounter{mc}{1}}}
\AtEndEnvironment{multiplechoice}{\setcounter{mc}{\value{mc}}}

\newcounter{sa}
\setcounter{sa}{0}
\AtBeginEnvironment{shortanswer}{\AtBeginEnvironment{question}{\addtocounter{sa}{1}}}


\usepackage{totcount}

\newtotcounter{s}
\setcounter{s}{0}
\newenvironment{mcquestion}[1]{
    \stepcounter{s}
    \begin{question} 
    #1 
    \end{question}
   }


\begin{document}

\begin{examtop}
This exam is worth all the marbles. It contains  \themc\ MC questions and \thesa\ short answer questions. There are a total of \thenumquestions\ questions.\\
\end{examtop}

\begin{multiplechoice}[]

\begin{mcquestion}
    Yes?
        \choice[!]{Yes.}
        \choice{No.}
\end{mcquestion}


\begin{question}
  True?
    \choice[!]{True.}
    \choice{False.}
    \choice{Maybe.}
    \choice{Dunno.}
\end{question}

\begin{question}
  False?
    \choice{True.}
    \choice[!]{False.}
    \choice{Maybe.}
    \choice{Dunno.}
\end{question}
\end{multiplechoice}

\begin{shortanswer}[]
\begin{question}
What is love?
\begin{answer}
Baby don't hurt me. 
\end{answer}
\end{question}

\begin{question}
What is the meaning of life?
\begin{answer}
42. 
\end{answer}
\end{question}

\begin{question}
Are you sure?
\begin{answer}
Yes. 
\end{answer}
\end{question}
\end{shortanswer}

\end{document}
1

enter image description here

\documentclass[10pt]{examdesign}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\SectionFont{\large\bfseries\ttfamily}
\ContinuousNumbering
\Fullpages
\NoKey
\NumberOfVersions{1}

\let\namedata\relax

\usepackage{etoolbox}

\newcounter{qcount}
\setcounter{qcount}{0}
\AtBeginEnvironment{question}{\addtocounter{qcount}{1}}

\newcounter{mcount}
\setcounter{mcount}{0}
\AtBeginEnvironment{question}{\ifbool{insidemc}{\addtocounter{mcount}{1}}{}}

\newbool{insidemc}
\makeatletter
\renewenvironment{multiplechoice}[1][]
  {\setbool{insidemc}{true}
  \begin{section}
   \def\@currentsectionname{multiplechoice}
   \exam@definesectiontype{multiplechoice}
   \exam@SetDefaultRearrangeBehavior
   \exam@SetDefaultSectionTitle{\exam@notitle}
   \exam@SetDefaultRecountState{\exam@DefaultNumberingBehavior}
   \setkeys{section}{#1}%
   \get@instructions \relax}
  {\end{section}
  \setbool{insidemc}{false}}
\makeatother  

\newcounter{scount}
\setcounter{scount}{0}
\AtBeginEnvironment{question}{\ifbool{insides}{\addtocounter{scount}{1}}{}}

\newbool{insides}
\makeatletter
\renewenvironment{shortanswer}[1][]
  {\setbool{insides}{true}
  \begin{section}
   \def\@currentsectionname{shortanswer}
   \exam@definesectiontype{shortanswer}
   \exam@SetDefaultRearrangeBehavior
   \exam@SetDefaultSectionTitle{\exam@notitle}
   \exam@SetDefaultRecountState{\exam@DefaultNumberingBehavior}
   \setkeys{section}{#1}%
   \get@instructions \relax}
  {\end{section}
  \setbool{insides}{false}}
\makeatother


\begin{document}

\begin{examtop}
This exam is worth all the marbles. It contains  
\themcount\ 
MC questions and 
\thescount\ 
short answer questions. There are a total of 
\theqcount\ 
questions.\\
\end{examtop}

\begin{multiplechoice}[]

\begin{question}
    Yes?
        \choice[!]{Yes.}
        \choice{No.}
\end{question}


\begin{question}
  True?
    \choice[!]{True.}
    \choice{False.}
    \choice{Maybe.}
    \choice{Dunno.}
\end{question}

\begin{question}
  False?
    \choice{True.}
    \choice[!]{False.}
    \choice{Maybe.}
    \choice{Dunno.}
\end{question}
\end{multiplechoice}

\begin{shortanswer}[]
\begin{question}
What is love?
\begin{answer}
Baby don't hurt me. 
\end{answer}
\end{question}

\begin{question}
What is the meaning of life?
\begin{answer}
42. 
\end{answer}
\end{question}

\begin{question}
Are you sure?
\begin{answer}
Yes. 
\end{answer}
\end{question}
\end{shortanswer}

\end{document}
  • Wow! Could you explain what is going on? – invictus Sep 26 '17 at 20:08
  • 1
    @invictus I use a bool to find out if the question is inside a MC or short answer and increment the counter based on this. – user36296 Sep 26 '17 at 20:10
  • Cool - but why can't I do arithmetic with these counters? For example, if I define a counter called points, then I should be able to do something like \setcounter{points}{1*\value{mcount} + 2*\value{scount}} (using the calc package) and display it with thepoints. But this returns zero where it should return 9. – invictus Sep 26 '17 at 20:39
  • 1
    @invictus You could look into \numexpr to do such calculations, but actually its not necessary, just use \newcounter{points} \addtocounter{points}{\themcount} \addtocounter{points}{\thescount} \addtocounter{points}{\thescount} \thepoints – user36296 Sep 26 '17 at 21:12

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.