2

This question arises from this one that was specific to the exam document class, but there should, I imagine, be wider applicability here as well as usefulness outside of using exam.

\documentclass[10pt,addpoints]{exam}

\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}

\begin{document}

\begin{questions}
\question Question one.

\question Question two.

\question Question three.
\end{questions}

\bgroup
\def\arraystretch{2.5}
{\setlength{\tabcolsep}{2em}
\begin{tabular}{|c|c|c|c|}
\hline
Problem & Understood & Confused & \phantom{someemptytext}Note\phantom{someemptytext} \\
\hline
\rownumber & {} & {} & {}\\
\hline
\rownumber & {} & {} & {}\\
\hline
\rownumber & {} & {} & {}\\
\hline
\end{tabular}}
\egroup\\

Total number of questions: \numquestions. 

\end{document}

enter image description here

I used help from this question for the row numbers, but it seems like there should be an easy workaround for what I am trying to accomplish here. The table is how I would like it to look except I want the number of rows generated to be dictated by \numquestions from the exam class. As can be seen from the code, all \numquestions rows (3 in this case) are of the type

\rownumber & {} & {} & {}\\
\hline

Is there a way I can use \numquestions to make a table generate \numquestions rows of the type above?

2 Answers2

2

The last value of question is still known when the questions environment ends, until a new one starts.

You can build a table based on this.

\documentclass[10pt,addpoints]{exam}
\usepackage{xparse,tabularx}

\ExplSyntaxOn
\tl_new:N \l__farlow_grading_tl
\NewDocumentCommand{\gradingtable}{}
 {
  \tl_clear:N \l__farlow_grading_tl
  \int_step_inline:nnnn { 1 } { 1 } { \value{question} }
   {
    \tl_put_right:Nn \l__farlow_grading_tl
     {
      ##1 \vphantom{$\bigg|$} & & & \\ \hline
     }
   }
  \par\noindent
  \begin{tabularx}{\textwidth}
   {
    | c |
    >{\hsize=0.5\hsize} X |
    >{\hsize=0.5\hsize} X |
    >{\hsize=2.0\hsize} X |
   }
  \hline
  Problem &
  \multicolumn{1}{c|}{Understood} &
  \multicolumn{1}{c|}{Confused} &
  \multicolumn{1}{c|}{Note} \\ \hline
  \tl_use:N \l__farlow_grading_tl
  \multicolumn{4}{@{}l}{{\small Number~of~problems:~\thequestion}}
  \end{tabularx}
 }
\ExplSyntaxOff

\begin{document}

\begin{questions}

\question Question one.

\question Question two.

\question Question three.

\end{questions}

\gradingtable

\end{document}

enter image description here

For placing the table above the questions (requires two runs to synchronize):

\documentclass[10pt,addpoints]{exam}
\usepackage{xparse,tabularx,refcount,etoolbox}

\newcounter{grading}
\BeforeBeginEnvironment{questions}{%
  \stepcounter{grading}%
  \gradingtable
}
\AfterEndEnvironment{questions}{%
  \addtocounter{question}{-1}%
  \refstepcounter{question}%
  \label{grading\thegrading @label}%
}

\ExplSyntaxOn
\tl_new:N \l__farlow_grading_tl
\NewDocumentCommand{\gradingtable}{}
 {
  \tl_clear:N \l__farlow_grading_tl
  \int_step_inline:nnnn { 1 } { 1 } { \getrefnumber{grading\thegrading @label} }
   {
    \tl_put_right:Nn \l__farlow_grading_tl
     {
      ##1 \vphantom{$\bigg|$} & & & \\ \hline
     }
   }
  \par\noindent
  \begin{tabularx}{\textwidth}
   {
    | c |
    >{\hsize=0.5\hsize} X |
    >{\hsize=0.5\hsize} X |
    >{\hsize=2.0\hsize} X |
   }
  \hline
  Problem &
  \multicolumn{1}{c|}{Understood} &
  \multicolumn{1}{c|}{Confused} &
  \multicolumn{1}{c|}{Note} \\ \hline
  \tl_use:N \l__farlow_grading_tl
  \multicolumn{4}{@{}l}{{\small Number~of~problems:~\getrefnumber{grading\thegrading @label}}}
  \end{tabularx}
 }
\ExplSyntaxOff

\begin{document}

\begin{questions}

\question Question one.

\question Question two.

\question Question three.

\end{questions}

\begin{questions}

\question Question one again.

\question Question two again.

\end{questions}

\end{document}

enter image description here

egreg
  • 1,121,712
  • @DanielW.Farlow The main part is to define a suitable preamble for the table; tabularx is used only for convenience; instead of changing \arraystretch, I insert a big strut. The body of the table is generated with \int_step_inline:nnnn, not that difficult. ;-) – egreg Aug 06 '17 at 20:54
  • You are the LaTeX deus ex machina to be sure. I will keep on my journey though that someday maybe I'll be able to look at something like that and see it as "not that difficult." :-) – Daniel W. Farlow Aug 06 '17 at 20:57
  • Is it at all possible with a minor change to have the modified grade table before the questions environment or does that ruin the structural integrity of your answer (I know you said you could build a table based on the value of question when the questions environment ends). I ask because that was originally my main intention (with using numquestions somewhere) but your answer is rather beyond me in terms of whether or not that would be a minor change or a complete restructuring. – Daniel W. Farlow Aug 06 '17 at 21:25
  • @DanielW.Farlow I can think to a two-pass procedure. Stay tuned. – egreg Aug 06 '17 at 21:30
  • This is fantastic. Thanks for the awesome work and saving me many hours of frustration (I was even able to moderately understand your code and modify one or two things to get exact placement of the table how I wanted it!). :-) – Daniel W. Farlow Aug 07 '17 at 19:41
  • Is it possible to modify your code while maintaining the integrity of your answer to preserve hyperlinking of questions in the exam document class when hyperref is loaded? I've posted a question about this here, but thought I would ask you on this original thread. – Daniel W. Farlow Jan 23 '18 at 05:01
1

Constructing table rows within loops is 'difficult' due to grouping. expl3 helps here with \prg_replicate:nn which replicates the same content according to the first argument, which is \number\value{numquestions}.

I additionally used the new R columntype that automatically counts the lines, but this can be shifted to the macro \displaytherows as well.

\documentclass[10pt,addpoints]{exam}

\usepackage{expl3}
\usepackage{array}

\newcolumntype{R}{>{\stepcounter{magicrownumbers}\themagicrownumbers}c}

\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}

\ExplSyntaxOn
\makeatletter
\newcommand{\displaytherows}{%
  \prg_replicate:nn {\exam@numquestions}{ & {} & {} & {} \tabularnewline \hline}
}
\makeatother
\ExplSyntaxOff

\begin{document}


\begin{questions}
\question Question one.

\question Question two.

\question Question three.
\end{questions}

\bgroup
\def\arraystretch{2.5}
{\setlength{\tabcolsep}{2em}
  \begin{tabular}{|R|c|c|c|}
    \hline
    \multicolumn{1}{|c}{Problem} & Understood & Confused & \phantom{someemptytext}Note\phantom{someemptytext} \tabularnewline
    \hline
    \displaytherows
  \end{tabular}
}
\egroup

Total number of questions: \numquestions. 

\end{document}

enter image description here

  • I get an undefined control sequence error when running your code above for \displaytherows. Does that mean something I am running is outdated? Oops maybe the issue is \exam@numquestions. Whatever it is, there's an undefined control sequence issue. Trying to resolve. – Daniel W. Farlow Aug 06 '17 at 20:34
  • @DanielW.Farlow: It works for me -- do you have copied my code as is, with the \makeatletter...\makeatother pair as well? –  Aug 06 '17 at 20:35
  • Yes (complete copy and paste), and it's still throwing the error. I'm using MiKTeX if that's relevant and pdfLaTeX to compile. Here is the error message. – Daniel W. Farlow Aug 06 '17 at 20:40
  • @DanielW.Farlow: Sigh (MikTeX), well I use TeXLive 2017, with daily updates on Linux, but the last expl3 update should have entered MikTeX already... –  Aug 06 '17 at 20:41
  • @DanielW.Farlow: what does \meaning\numquestions provide for you, e.g. right after \begin{document}? –  Aug 06 '17 at 20:42
  • Nothing is provided. There's no error. It's only on the line with \displaytherows. I can't see any effect that \meaning\numquestions has. – Daniel W. Farlow Aug 06 '17 at 20:43
  • @DanielW.Farlow: Strange, but I changed my answer regarding that line –  Aug 06 '17 at 20:47
  • Yes, it must be an issue with needing to update my system (can't thin of anything else that could explain it). Thank you for your help though. – Daniel W. Farlow Aug 06 '17 at 20:53