# Create custom tags for questions in Exam Class

I've been trying to figure out how to create a tagging system for the questions I have and display the results in a summary table (see image attached).

I have a sample .tex file that looks the following.

\documentclass{exam}

\begin{document}

\begin{questions}
\question[1] \tag{Algebra}
This is Question 1. This is tagged against Algebra.

\question[2] \tag{Equations}
This is Question 2.This is tagged against Equations.

\question[2] \tag{Data}
This is Question 3.This is tagged against Data.

\question[1] \tag{Algebra}
This is Question 4. This is tagged against Algebra.

\question[2] \tag{Algebra}
This is Question 5.This is tagged against Algebra.

\question[2] \tag{Data}
This is Question 6.This is tagged against Data.

\end{questions}

\end{document}



I would like to use the tag system to produce a summary table as follows. My desired results are:

Any help or direction is greatly appreciated.

• Basically you need to create your own summary code. The good news is that exam class put a lot of information into the aux file. Check out \protected@write{\@auxout}{}{...} Apr 14 at 2:03
• Hi @JohnKormylo, sorry i'm not familiar with the aux file. I've tried to make a new counter but that takes too long and is not dynamic. Apr 14 at 2:09
• If you look in your directory you will find a file with the same name as your .tex file only with an .aux extension. It is also executable TeX code, and in fact is read during \begin{document}. Apr 14 at 2:12
• I see this  \relax \gdef\pointsofq@i{0} \gdef\bonuspointsofq@i{0} \gdef\pointsofq@ii{0} \gdef\bonuspointsofq@ii{0} \gdef\pointsofq@iii{0}  Apr 14 at 2:17
• It would probably be easier to do than explain. I'll give it a shot in the morning. Apr 15 at 3:45

## 2 Answers

This solution requires you to provide a list of tags in the order to be displayed. Questions without tags or not on the list will be ignored (except for the total points, which are already computed).

This version will only work after the questions. If you want a summary before, you need to also put the tags into the aux file.

\documentclass[addpoints]{exam}
\usepackage{tikz}% foreach and pgfmath

\newcommand{\tag}[1]{\expandafter\gdef\csname tagofq@\roman{question}\endcsname{#1}}

\makeatletter
\newcommand{\taglist}[1]{% #1 = comma delimmited list of tags (in order) for summary
\@ifundefined{exam@numquestions}{}{%
\bgroup% use local definitions
\lineskip=0pt
\noindent
\fbox{\parbox[t][1cm][t]{4cm}{\strut Tag}}\hspace{-\fboxrule}%
\fbox{\parbox[t][1cm][t]{4cm}{\strut Question}}\hspace{-\fboxrule}%
\fbox{\parbox[t][1cm][t]{4cm}{\strut Points}}\newline
\foreach \x in {#1} {\count1=0
\let\qlist=\empty
\edef\pointsum{0}%
\loop\ifnum\count1<\exam@numquestions\relax
\advance\count1 by 1
\@ifundefined{tagofq@\@roman\count1}{}{%
\edef\thistag{\csname tagofq@\@roman\count1\endcsname}%
\ifx\x\thistag\relax
\ifx\empty\qlist\relax
\edef\qlist{\the\count1}%
\else
\edef\qlist{\qlist, \the\count1}%
\fi
\edef\thispoints{\csname pointsofq@\@roman\count1\endcsname}%
\pgfmathparse{int(\pointsum+\thispoints)}%
\let\pointsum=\pgfmathresult
\fi}%
\repeat
\vskip-\fboxrule\noindent
\fbox{\parbox[t][1cm][t]{4cm}{\strut\x}}\hspace{-\fboxrule}%
\fbox{\parbox[t][1cm][t]{4cm}{\strut\qlist}}\hspace{-\fboxrule}%
\fbox{\parbox[t][1cm][t]{4cm}{\strut\pointsum}}\newline}%
\vskip-\fboxrule\noindent
\fbox{\parbox[t][1cm][t]{4cm}{\strut Total}}\hspace{-\fboxrule}%
\fbox{\parbox[t][1cm][t]{4cm}{\strut}}\hspace{-\fboxrule}%
\fbox{\parbox[t][1cm][t]{4cm}{\strut\exam@numpoints}}
\egroup}}
\makeatother

\begin{document}

\begin{questions}
\question[1] \tag{Algebra}
This is Question 1. This is tagged against Algebra.

\question[2] \tag{Equations}
This is Question 2.This is tagged against Equations.

\question[2] \tag{Data}
This is Question 3.This is tagged against Data.

\question[1] \tag{Algebra}
This is Question 4. This is tagged against Algebra.

\question[2] \tag{Algebra}
This is Question 5.This is tagged against Algebra.

\question[2] \tag{Data}
This is Question 6.This is tagged against Data.

\end{questions}

\taglist{Algebra,Data,Equations}

\end{document}


• Thank you, this also works with subparts and subsubparts. I appreciate the fact its very dynamic. Now I just need to sit and understand the logic behind your code. Quick Question: Is it possible to say introduce a \part[1] \tag{Algebra} or \subpart[2]{Algebra} as part of a question? So that the summary page will include that particular part or subpart in the summary? Apr 15 at 22:02
• Currently I loop over every question for each tag. If a question has parts or subparts, one would need to loop over them as well. Note that each \loop requires another group layer. Apr 16 at 1:50
• Thanks once again John, I can't say I fully understand your code but I tried to modify your code to incorporate another loop/group layer and failed. I will be post another question on stackexchange that expands on this question to include parts and subparts. Apr 16 at 5:32

You most likely will succeed when switching to package xsim, which is the continuation of exsheets by the same author: https://ctan.org/pkg/xsim . It seems to be the most suited one from https://ctan.org/topic/exam .

Though the documentation is overwhelming, I suggest to go through it front to back, skipping all the details and getting the overall ideas. It's intended to provide exercise-solution pairs (if needed), including tagging. It's also a good idea to download the github code and have a look ad /doc/examples.

A few remarks before I comment the code below:

• if you want to rename "Exercise" into "Question", refer to ch. 6 "New Exercise Types"; you can probably skip many code presented there
• don't be afraid of "Templates", which is just a different way to create your own layout and content; as shown in the manual they use a lot of basic Latex commands
• it provides some looping-commands, which lets you access each exercise-object you created, and extract infromation as neede (see my first example below)

In the code below I rephrased your questions into xsim-speak, where the first one shows some features you may or may not want to use. I end showing an exercise-solution pair from xsim.

The first \gradingtable statement prints horizontally, while the easier one prints vertically. This is something you may want to modify, i.e. rewrite as an xsim-template to fit your needs.

The final lines show you my so far failing attempts to approach your desired output. Mainly the information in the manual was insufficient to me, the examples did not really cover it and an internet search didn't help much so far. But perhaps some member more experienced in using xsim can help.

So, to me it sounds like xsim does provide all you need to create your result step by step ... only I wasn't used enough to this package ;-)

\documentclass[12pt]{article}
\usepackage{xsim}

\xsimsetup{
solution/print = true
}

\begin{document}
% your examples
\begin{exercise}[ subtitle={About algebra}, % showing some features
points=1,
bonus-points=1,
tags={Algebra}]
This is Question 1. This is tagged against Algebra.
Tag set is: \GetExerciseProperty{tags}
\end{exercise}
%
\begin{exercise}[points=2]%,tags={Equations}]
This is Question 2. This is tagged against Equations.
\end{exercise}
%
\begin{exercise}[points=2, tags={Data}]
This is Question 3. This is tagged against Data.
\end{exercise}
%
\begin{exercise}[points=1, tags={Algebra}]
This is Question 4. This is tagged against Algebra.
\end{exercise}
%
\begin{exercise}[points=2, tags={Algebra}]
This is Question 5. This is tagged against Algebra.
\end{exercise}
%
\begin{exercise}[points=2, tags={Data}]
This is Question 6. This is tagged against Data.
\end{exercise}

% ~~~ an exercise with solution
\begin{exercise}
A first example for an exercise.
\end{exercise}
%
\begin{solution}
A first example for a solution.
\end{solution}

% this is standard (hor)
\gradingtable[template=default*]

% this is simpler (vert)
\gradingtable

> > > >
% here I failed so far
% \begin{tabular}
\ForEachExerciseTag{Data}{\ListExerciseTags{Data}{,}}

%\ListExerciseTags{Data}{&}
% \end{tabular}

> > > >

\end{document}


Result:

P.S.: Here I'll append new insights which may arise e.g. from further comments.

A. Suggestion: read in parallel the pdf-manual and xsim-source code; code to find relevant parts, manual to look up all those xsim-commands. On my system 4 source files can be found in C:..\AppData\Local\Programs\MiKTeX\tex\latex\xsim. Use an Editor like Notepad++, open these 4 and find search terms of you rinterest in all open files (makes it quick and easy). // Make sure not to modify these files (!)

B. \gradingtable: It's defined as a \NewDocumentCommandin xsim.sty. This place is only to have a look. To modify see ch. 12 "Grading Tables". It's intended to write your own template and specify it to this command, like \gradingtable[type=myTable]. How to create and insert your own template myTable (your desired summary table) consult ch. 13 "Styling the Exercise Templates".

C. Template "mechanics": Referring to Notepad++ or similar search for DeclareExerciseEnvironmentTemplate. You can find all templates mentioned in the manual in xsim.sty, xsim.style.layouts.code.tex and xsim-manual.cls. Again, it's just to see how all those xsim-commands are or can be applied. Refer to A. above. This should give you ideas, how to design your intended myTable, see B..

D. xsim as a toolbox: Just like Latex xsim is intended to be a toolbox. I.e. it provides these Exercise- and Solution-objects, with all their internal information. You access their content via xsim-commands ... AND draft your table with this information, as you would do in your example. And it's quite powerful.

E. Varying the examples: It's a good idea to copy and vary examples, found in /doc/examples from github (see above), like changing order of exercise-solution pairs, deleting parts, adding silly text-markers, introducing tags or xsim-commands. This way you get a better idea, how things work, and what requirements are to be met.

• I'm happy to try a new package, but how does this produce my desired result (the summary table) ? I know exam class allows me to modify the grade table as you suggested i.e per question/exercise but I need to group my questions according to custom tags/references. Apr 15 at 7:28
• As I wrote that's the part to rewrite. All you need is available in xsim, like tags and sum of points. Your turn :-) // Evaluating the .aux file, as proposed, will work certainly, too. Depends on which route you will make progress. Apr 15 at 7:48
• I understand, I will look into the documentation for xsim. I just couldn't find anything to customize the grade table in the exam class documentation. Apr 15 at 9:06
• @AlanJones, please see my statements after P.S. I think this way we can follow this places rules to avoid discussions, AND compile information for newcomers to xsim. Apr 15 at 11:33