2

My exam template includes a table listing the point values for each problem in the exam. Is there a way to declare the point values of each problem as variables within latex and write code to automatically generate this table?

\documentclass[letterpaper]{article}

\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{problem}{Problem}

\begin{document}

\begin{center}
\begin{tabular}{|c|c|c|}
\hline
Problem &   Points  &   Score   \\ \hline
1       &   12      &           \\ \hline
2       &   17      &           \\ \hline
Total   &   29      &           \\ \hline
\end{tabular}
\end{center}

\begin{problem}[12 Points]
Problem statement.
\end{problem}

\begin{problem}[17 Points]
Problem statement.
\end{problem}

\end{document}
1

3 Answers 3

2

Faced with anything to do with lists, I reach for my etoolbox. The following has, perhaps, elements of overkill (mostly in the interests of customization); the core of the actual list-handling material is in \addqpoint and \printqlist; the rest is really designed to make the user-interface workable.

In order to keep things together, I've commented the code rather extensively.

\documentclass{article}
\usepackage{etoolbox}% for list handlig
\usepackage{booktabs}% for nice tables
% We define a list. Questions and points
% will be placed into the list as \qpoint{question}{points}
\newcommand{\qplist}{}
% A counter for question numbers
\newcounter{qnum}
% And a counter for total points
\newcounter{totalpoints}
% And one for the points of a particular question
\newcounter{thisqpoints}
% And (for ease of customization) the name for a question, points, score,
% so these could be changed to "marks" or whatever
\newcommand{\questionname}{Question}
\newcommand{\spoint}{point}% singular
\newcommand{\ppoint}{points}% plural
\newcommand{\Score}{Score}
\newcommand{\Points}{Points}
\newcommand{\Pointstotal}{Total}
% And a command that prints the points at the end of a qustion
\newcommand{\printpoints}{%
  \ifnumcomp{\value{thisqpoints}}{=}{1}
    {\arabic{thisqpoints}~\spoint}
    {\arabic{thisqpoints}~\ppoint}}

% We define a command to allow resetting of the list
\newcommand{\resetqplist}{\renewcommand{\qplist}{}}

% ... and a command to add a something to the list
% \addqpoint{question}{list}
\newcommand{\addqpoint}[2]{%
  \listxadd{\qplist}{{#1}{#2}}}

% To print the list we simply iterate over it expanding as we go
\newcommand{\printqplist}{%
  \gdef\do##1{\qpoint ##1 \\}%
  \setcounter{totalpoints}{0}%
  \dolistloop{\qplist}}

% This is the main internal command: it is designed for use in a 
% table, and updates the totalpoints counter
\newcommand{\qpoint}[2]{%
  \addtocounter{totalpoints}{#2}%
  & #1 & #2}

% So far all the commands are "internal". Now the user interface
\newenvironment{question}[1][]
  {\refstepcounter{qnum}%
   \ifblank{#1}% If no points are given, nothing is added to the list
      {\setcounter{thisqpoints}{0}}
      {\setcounter{thisqpoints}{#1}%
       \addqpoint{\arabic{qnum}}{#1}}%
       \subsection*{\questionname~\arabic{qnum}}}
  {\ifnumcomp{\value{thisqpoints}}{>}{0}% print the points only if there are some!
    {\marginpar{\printpoints}}
    {}%
    \par\bigskip}%

\newcommand{\printpointstable}{%
  \subsection*{\Points}
  \setcounter{totalpoints}{0}%
  \par\noindent
  \begin{tabular}{lcrl}
  \toprule
  & \questionname & \Points & \Score \\
  \midrule
  \printqplist
  \midrule
  & \bfseries\Pointstotal & \bfseries\arabic{totalpoints} \\
  \bottomrule
  \end{tabular}}


\begin{document}

\begin{question}[2]
Is this a question?\label{q:1}
\end{question}

\begin{question}[3]
How well do you consider you answered question \ref{q:1}?
\end{question}

\begin{question}
And how many points for this?
\end{question}


\printpointstable

\end{document}

output from MWE

(All that being said, I'd sooner use some else's specially developed class than roll my own. Proof in point: the first version of this I posted had a hard-to-find expansion error, which I hope I've now squashed!)

0
2

As I mentioned in my comment, the exam document class can do this out of the box. In addition, it has many other features you may find useful when writing exams and has customization options if you do not like the default layout/style.

Code

For your basic MWE, the equivalent code is as follows:

\documentclass[addpoints]{exam}

\begin{document}

\begin{center}
  \vqword{Problem} % change table heading text from the default ``Question''
  \gradetable % print a grade table (much customization possible here)
\end{center}

\begin{questions}

  \question[12] % optional argument gives point value
  Problem statement.

  \question[17]
  Problem statement.

\end{questions}

\end{document}

Output

enter image description here

1

In TeX (as in M4) data structures are represented as non-evaluated treatments and looping is realised by binding the non-evaluated treatment to the appropriate procedure and evaluating the data structure. For instance, we define a list as in

\def\kinglist{\\{Louis IX}\\{Louis X, dit le hutin}\\{Louis XI}}

To process the three items of \kinglist we bind \\ to the appropriate treatment, as in

\def\paraking#1{#1\par}
\def\lineking#1{ #1}

\let\\=\paraking
\kinglist

\let\\=\lineking
\kinglist

\let\\=\undefined

If we were about to define a true data structure, we surely would add book-keeping macros, such as macros allowing to add items to a list.

In your case, you can define macros \problemtable and \problempop so that you can type your original table

\begin{center}
\begin{tabular}{|c|c|c|}
\hline
Problem &   Points  &   Score   \\ \hline
1       &   12      &           \\ \hline
2       &   17      &           \\ \hline
Total   &   29      &           \\ \hline
\end{tabular}
\end{center}

as

\newcommand\problemlist{\\{{1}{12}}\\{{2}{17}}}
\problemtable

were we expect \problemtable to expand to the table you wrote. The \problempop should be used in your problem environment to prepare the text (with problem number and points) and pop the first element of the \problemlist.

If you want to avoid the fun of doing this yourself, you can probably use a package as suggested by others.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .