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?




Problem &   Points  &   Score   \\ \hline
1       &   12      &           \\ \hline
2       &   17      &           \\ \hline
Total   &   29      &           \\ \hline

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

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


3 Answers 3


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.

\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}
% A counter for question numbers
% And a counter for total points
% And one for the points of a particular question
% And (for ease of customization) the name for a question, points, score,
% so these could be changed to "marks" or whatever
\newcommand{\spoint}{point}% singular
\newcommand{\ppoint}{points}% plural
% And a command that prints the points at the end of a qustion

% We define a command to allow resetting of the list

% ... and a command to add a something to the list
% \addqpoint{question}{list}

% To print the list we simply iterate over it expanding as we go
  \gdef\do##1{\qpoint ##1 \\}%

% This is the main internal command: it is designed for use in a 
% table, and updates the totalpoints counter
  & #1 & #2}

% So far all the commands are "internal". Now the user interface
   \ifblank{#1}% If no points are given, nothing is added to the list
  {\ifnumcomp{\value{thisqpoints}}{>}{0}% print the points only if there are some!

  & \questionname & \Points & \Score \\
  & \bfseries\Pointstotal & \bfseries\arabic{totalpoints} \\


Is this a question?\label{q:1}

How well do you consider you answered question \ref{q:1}?

And how many points for this?



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!)


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.


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



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


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

  Problem statement.




enter image description here


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\lineking#1{ #1}




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

Problem &   Points  &   Score   \\ \hline
1       &   12      &           \\ \hline
2       &   17      &           \\ \hline
Total   &   29      &           \\ \hline



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 .