2

For a multiple choice exam, I'm using the mcexam package. There does not seem to be a command that allows me to print the total number of questions. Is there a way I can define such a command? E.g.:

The total number of questions in this exam is: \numquestions

This command is defined for the regular exam document class. I tried adding this to my preamble: \newcommand{\numquestions}{\mc@totalNumberOfQuestions}

but this gives me an undefined control sequence. Here's a small example. It won't compile due to the \numquestions command. Notice that this example is a trimmed version of the example.tex file shipped with mcexam:

\documentclass[a4paper]{article}

\usepackage[output=exam
           ,numberofversions=4
           ,version=1
           ,seed=1
           ,randomizequestions=true
           ,randomizeanswers=true
           ,writeRfile=false
           ]{mcexam}

% Packages used for special things
\usepackage{tikz,framed} 

% Set headers and footers
\usepackage{fancyhdr,lastpage}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt} 
\renewcommand{\footrulewidth}{1pt}
\lfoot{\mctheversion}
\rfoot{Page \thepage\ of \pageref{LastPage}}

% Ensure each question+answers is printed entirely on the same page.
\usepackage{calc}
\renewenvironment{setmcquestion}{\begin{minipage}[t]{\linewidth-\labelwidth}}{\end{minipage}\par}  

\makeatletter
\newcommand{\numquestions}{\mc@totalNumberOfQuestions} %DOES NOT WORK
\makeatother

\begin{document}


\begin{center}
  \bfseries\LARGE Midterm Exam 
\end{center}

\begin{framed}
  \centering\bfseries\Large\MakeUppercase{\mctheversion}
\end{framed}

\mcifoutput{concept,exam}{ 

  \bigskip

  \noindent Name: \dotfill\\[.5\baselineskip]
  \noindent Student Number: \dotfill\\[.5\baselineskip]
  \noindent Program: \dotfill  

  \vspace{2\baselineskip}


  \noindent\textbf{Instructions:}
  \begin{itemize}[nosep]
         \item This exam has \numquestions questions. %THIS THROWS AN UNDEFINED CONTROL SEQUENCE ERROR
  \end{itemize}  
}


\begin{mcquestions}

\question What is the color of the sky?

          \begin{mcanswerslist}[fixlast]
          \answer[correct] blue
          \answer green
          \answer red
          \answer yellow
          \answer none of the above
          \end{mcanswerslist}        

          \begin{mcexplanation}
          If you look up to the sky and there are no clouds, you'll see it is blue.
          \end{mcexplanation}

          \begin{mcnotes}
          This question had a large proportion of good answers last year.
          \end{mcnotes}

\question Which figure is a square?
          \begin{mcanswers}
          \begin{tabular}{@{}cccc}
          \answer{1}{\tikz{\draw (-0.66,0)--(0.66,0)--(0,1)--cycle;}}& 
          \answer{2}{\tikz{\draw (0,0)--(2,0)--(2,1)--(0,1)--cycle;}}& 
          \answer[correct]{3}{\tikz{\draw (0,0)--(1,0)--(1,1)--(0,1)--cycle;}}& 
          \answer{4}{\tikz{\draw (0,0) circle[radius=0.5];}}\\
          \answernum{1}&\answernum{2}&\answernum{3}&\answernum{4}\\
          \end{tabular} 
          \end{mcanswers}


\end{mcquestions}

\end{document}

1 Answers1

3

That mcexam package is interesting. By the way, it has no internal for collecting the total number of questions. We can add one.

\documentclass[a4paper]{article}

\usepackage[output=exam
           ,numberofversions=4
           ,version=1
           ,seed=1
           ,randomizequestions=true
           ,randomizeanswers=true
           ,writeRfile=false
           ]{mcexam}

% Packages used for special things
\usepackage{tikz,framed} 
\usepackage{fancyhdr,lastpage,totcount,xpatch}
\usepackage{calc}

% Set headers and footers
\pagestyle{fancy}

\fancyhf{}
\renewcommand{\headrulewidth}{0pt} 
\renewcommand{\footrulewidth}{1pt}
\lfoot{\mctheversion}
\rfoot{Page \thepage\ of \pageref{LastPage}}

% Ensure each question+answers is printed entirely on the same page.
\renewenvironment{setmcquestion}
  {\begin{minipage}[t]{\linewidth-\labelwidth}}
  {\end{minipage}\par}

\newtoggle{questionscounted}
\global\togglefalse{questionscounted}
\makeatletter
\newcommand{\patch@mcquestions}{%
  \iftoggle{questionscounted}
    {}
    {%
     \setcounter{numquestions}{\value{mc@counter}}%
     \global\toggletrue{questionscounted}
    }%
}
\newcounter{numquestions}
\regtotcounter{numquestions}
\xpatchcmd{\env@mcquestions@process}
 {\mc@setQuestionAndAnswerNumbers}
 {\mc@setQuestionAndAnswerNumbers\patch@mcquestions}
 {}{}
\makeatother


\begin{document}


\begin{center}
  \bfseries\LARGE Midterm Exam 
\end{center}

\begin{framed}
  \centering\bfseries\Large\MakeUppercase{\mctheversion}
\end{framed}

\mcifoutput{concept,exam}{ 

  \bigskip

  \noindent Name: \dotfill\\[.5\baselineskip]
  \noindent Student Number: \dotfill\\[.5\baselineskip]
  \noindent Program: \dotfill  

  \vspace{2\baselineskip}


  \noindent\textbf{Instructions:}
  \begin{itemize}[nosep]
         \item This exam has \total{numquestions} questions. %THIS THROWS AN UNDEFINED CONTROL SEQUENCE ERROR
  \end{itemize}  
}


\begin{mcquestions}

\question What is the color of the sky?

          \begin{mcanswerslist}[fixlast]
          \answer[correct] blue
          \answer green
          \answer red
          \answer yellow
          \answer none of the above
          \end{mcanswerslist}        

          \begin{mcexplanation}
          If you look up to the sky and there are no clouds, you'll see it is blue.
          \end{mcexplanation}

          \begin{mcnotes}
          This question had a large proportion of good answers last year.
          \end{mcnotes}

\question Which figure is a square?
          \begin{mcanswers}
          \begin{tabular}{@{}cccc}
          \answer{1}{\tikz{\draw (-0.66,0)--(0.66,0)--(0,1)--cycle;}}& 
          \answer{2}{\tikz{\draw (0,0)--(2,0)--(2,1)--(0,1)--cycle;}}& 
          \answer[correct]{3}{\tikz{\draw (0,0)--(1,0)--(1,1)--(0,1)--cycle;}}& 
          \answer{4}{\tikz{\draw (0,0) circle[radius=0.5];}}\\
          \answernum{1}&\answernum{2}&\answernum{3}&\answernum{4}\\
          \end{tabular} 
          \end{mcanswers}

\end{mcquestions}

\end{document}

enter image description here

An alternative solution (but I like better the other one).

\documentclass[a4paper]{article}

\usepackage[output=exam
           ,numberofversions=4
           ,version=1
           ,seed=1
           ,randomizequestions=true
           ,randomizeanswers=true
           ,writeRfile=false
           ]{mcexam}

% Packages used for special things
\usepackage{tikz,framed} 
\usepackage{fancyhdr,lastpage}
\usepackage{calc}

% Set headers and footers
\pagestyle{fancy}

\fancyhf{}
\renewcommand{\headrulewidth}{0pt} 
\renewcommand{\footrulewidth}{1pt}
\lfoot{\mctheversion}
\rfoot{Page \thepage\ of \pageref{LastPage}}

% Ensure each question+answers is printed entirely on the same page.
\renewenvironment{setmcquestion}
  {\begin{minipage}[t]{\linewidth-\labelwidth}}
  {\end{minipage}\par}

\makeatletter
\AtEndDocument{%
  \immediate\write\@auxout{\xdef\string\number@of@questions{\mc@totalNumberOfQuestions}}%
}
\def\numberofquestions{%
  \@ifundefined{number@of@questions}{??}{\number@of@questions}%
}
\makeatother


\begin{document}


\begin{center}
  \bfseries\LARGE Midterm Exam 
\end{center}

\begin{framed}
  \centering\bfseries\Large\MakeUppercase{\mctheversion}
\end{framed}

\mcifoutput{concept,exam}{ 

  \bigskip

  \noindent Name: \dotfill\\[.5\baselineskip]
  \noindent Student Number: \dotfill\\[.5\baselineskip]
  \noindent Program: \dotfill  

  \vspace{2\baselineskip}


  \noindent\textbf{Instructions:}
  \begin{itemize}[nosep]
         \item This exam has \numberofquestions\ questions. %THIS THROWS AN UNDEFINED CONTROL SEQUENCE ERROR
  \end{itemize}  
}


\begin{mcquestions}

\question What is the color of the sky?

          \begin{mcanswerslist}[fixlast]
          \answer[correct] blue
          \answer green
          \answer red
          \answer yellow
          \answer none of the above
          \end{mcanswerslist}        

          \begin{mcexplanation}
          If you look up to the sky and there are no clouds, you'll see it is blue.
          \end{mcexplanation}

          \begin{mcnotes}
          This question had a large proportion of good answers last year.
          \end{mcnotes}

\question Which figure is a square?
          \begin{mcanswers}
          \begin{tabular}{@{}cccc}
          \answer{1}{\tikz{\draw (-0.66,0)--(0.66,0)--(0,1)--cycle;}}& 
          \answer{2}{\tikz{\draw (0,0)--(2,0)--(2,1)--(0,1)--cycle;}}& 
          \answer[correct]{3}{\tikz{\draw (0,0)--(1,0)--(1,1)--(0,1)--cycle;}}& 
          \answer{4}{\tikz{\draw (0,0) circle[radius=0.5];}}\\
          \answernum{1}&\answernum{2}&\answernum{3}&\answernum{4}\\
          \end{tabular} 
          \end{mcanswers}

\end{mcquestions}

\end{document}
egreg
  • 1,121,712
  • This works great, thanks! I unfortunately don't really understand how your code achieves this result. Moreover, in the mcexam.sty I found the following: % Store the number of questions in \mc@totalNumberOfQuestions so I'm not sure why I couldn't just print this variable. – Joris Kinable May 19 '18 at 18:41
  • @JorisKinable There is indeed \mc@totalNumberOfQuestions, but it is only available after the mcquestions environment. I'll provide an alternative solution to this effect. – egreg May 19 '18 at 19:50