生成表行,其中行数取决于计数器

生成表行,其中行数取决于计数器

这个问题源于这个这是特定于exam文档类的,但我想,这里应该有更广泛的适用性,以及使用之外的实用性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}

在此处输入图片描述

我使用了这个问题对于行号,但似乎应该有一个简单的解决方法来实现我在这里尝试实现的功能。表格是我希望它看起来的样子,只是我希望生成的行数由类决定。\numquestionsexam代码中可以看出,所有\numquestions行(本例中为 3)都是类型

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

有什么方法可以使我使用\numquestions表格来生成\numquestions上述类型的行?

答案1

当环境结束时,的最后一个值question仍然是已知的questions,直到新的环境开始。

您可以据此建立一个表格。

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

在此处输入图片描述

将表格放在问题上方(需要两次运行才能同步):

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

在此处输入图片描述

答案2

table由于分组,在循环内构建行很“困难”。expl3这里有助于\prg_replicate:nn根据第一个参数复制相同的内容,即\number\value{numquestions}

另外,我还使用了可以自动计算行数的新列类型,但这也R可以转移到宏中。\displaytherows

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

在此处输入图片描述

相关内容