# Writing enumitem's to a file

I am a teacher and often need to make worksheets and reviews that have answer keys. With the help of TeX StackExchange, I have a fairly robust system at this point with the notable exception that it does not work with enumitem. The error message that I am getting is beyond my LaTeX wisdom. Is there anyone who can help me out?

MWE:

\documentclass[10pt]{article}

\usepackage{pgfplots}
\usepackage{enumitem}

\AtBeginDocument{%
}

\begingroup
\catcode\#=12
\gdef\hashchar{#}%
\endgroup

\makeatletter
\begingroup
\let\#\hashchar
\endgroup
}
\makeatother

\AtEndDocument{%
}

\begin{document}

%% With \begin{enumerate}, this code works.  Switch to
%% \begin{enumerate}[label=\emph{\alph*)}] and it fails.
%\begin{enumerate}
\begin{enumerate}[label=\emph{\alph*)}]
\item Question 2 \answerkey{$\frac{1}{2}$}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}}
\end{enumerate}

% Copy the mwe.keys to mwe.ans, then use this to show the answers.
%\input{mwe.ans}

\end{document}


As noted in the comments, switch the enumerate line to the one that uses enumitem features and this will fail. What can I do to not compromise the system I have and yet still use enumitem?

Here is the error that I am getting:

! Use of \@item doesn't match its definition. \text@command #1->\def \reserved@a {#1}\ifx \reserved@a \@empty \let \check@... l.37 \item Question 1 \answerkey{Answer 1}

UPDATE:

The comment below that it is not actually the enumitem code but rather the \emph (or similarly \textbf, \textit, etc) in the label that is the issue was interesting. The solution below that creates a "fake" label is the least invasive/complicated, while adding the functionality that label formatting makes it through to the answer key document. The code I started with did not put the answers into a true enumerate environment, so the solution proposed below which does this is overkill for my situation. In the end, based on the help/guidance below, I rewrote the \answerkey command as follows:

\newcommand{\answerkey}[2][.]{%
\begingroup
\let\#\hashchar
\expandafter{\@currentlabel}#1\ }
\endgroup
}


In actual use, it looks like this:

If the default item labels are used:

\begin{enumerate}


If enumitem label formatting is used:

\begin{enumerate}[label={\arabic*.}]

• @StevenB.Segletes: I suggest to use \protected@write{\answerkeyfile}{}{\@currentlabel.\ } rather. \noexpand will result in \@currentlabel being written to the file which is probably not wanted. – user31729 Dec 1 '17 at 20:32
• @ChristianHupfer You are correct in noting that my approach does not output the label properly. I will delete the earlier comment. – Steven B. Segletes Dec 1 '17 at 20:34
• @StevenB.Segletes: Unfortunately my suggestion does only provide compilation but not the desired content in the output file. \@currentlabel seems to be deeply unexpandable here – user31729 Dec 1 '17 at 20:46
• Is there a way to explain to a non-power-user what it is about enumitem that requires the special handling? Why is it that I can put a whole tikz picture into my answer file without troubles, yet I can't have the enumerant written there without special processing? – Christopher Donham Dec 2 '17 at 1:11

I suggest to expand \@currentlabel and write that content to the file with \unexpanded, however, this will place 'fake' item labels and not really \item layout in the file.

A better strategy would be to 'replicate' the enumerate environment in the output file.

\documentclass[10pt]{article}

\usepackage{pgfplots}
\usepackage{enumitem}

\AtBeginDocument{%
}

\begingroup
\catcode\#=12
\gdef\hashchar{#}%
\endgroup

\makeatletter
\begingroup
\let\#\hashchar
\endgroup
}
\makeatother

\AtEndDocument{%
}

\begin{document}

%% With \begin{enumerate}, this code works.  Switch to
%% \begin{enumerate}[label=\emph{\alph*)}] and it fails.
%\begin{enumerate}
\begin{enumerate}[label=\emph{\alph*)}]
\item Question 2 \answerkey{$\frac{1}{2}$}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}}
\end{enumerate}

% Copy the mwe.keys to mwe.ans, then use this to show the answers.
\input{\jobname.keys}

\end{document}


Another approach by writing \begin{answerenumerate} to the file, which is a copy of enumerate, basically:

\documentclass[10pt]{article}

\usepackage{pgfplots}
\usepackage{enumitem}

\AtBeginDocument{%
}

\begingroup
\catcode\#=12
\gdef\hashchar{#}%
\endgroup

\makeatletter
\begingroup
\let\#\hashchar
\endgroup
}
\makeatother

\AtEndDocument{%
}

\begin{document}

\setlist[enumerate,1]{label=\emph{\alph*)},
}

\begin{enumerate}
\item Question 2 \answerkey{$\frac{1}{2}$}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}}
\end{enumerate}

% Copy the mwe.keys to mwe.ans, then use this to show the answers.
\input{\jobname.keys}

\end{document}


• Very nice. (+1) – Steven B. Segletes Dec 1 '17 at 21:01
• @StevenB.Segletes: Thanks ;-) I've updated with another (better?) way – user31729 Dec 1 '17 at 21:12
• I have been playing around with your "fake" solution. At first glance, the "Answer" formatting appears to be very similar to the "Question" formatting. Can you suggest what might reveal the problems with this solution (or when I might encounter difficulties with it)? – Christopher Donham Dec 2 '17 at 3:38
• The only way I have been able to reveal it so far is by using the default item labels in which case the "Answer" version does not have a period after the label. – Christopher Donham Dec 2 '17 at 3:59
• Actually, I believe I have an answer to my own comment here. The formatting in the Answer portion does not maintain things like the "itemsep" or "topsep" settings from enumitem. With respect to the labels, it appears to be the same except for the default label which puts a period after the label. – Christopher Donham Dec 2 '17 at 4:30

You should use the ref feature of enumitem, also exploiting the fact that the package braces the \@currentlabel.

Define \answeritem as you like.

\documentclass[10pt]{article}

\usepackage{pgfplots}
\usepackage{enumitem}

\AtBeginDocument{%
}

\edef\hashchar{\string#}

\makeatletter
\begingroup
\let\#\hashchar
\endgroup
}
\makeatother

\begin{document}

%% With \begin{enumerate}, this code works.  Switch to
%% \begin{enumerate}[label=\emph{\alph*)}] and it fails.
%\begin{enumerate}
\begin{enumerate}[label=\emph{\alph*}),ref=\alph*]
\item Question 2 \answerkey{$\frac{1}{2}$}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}}
\end{enumerate}

\begin{enumerate}
\input{\jobname.keys}
\end{enumerate}

\end{document}


Here's the output in \jobname.keys:

\answeritem {a}

$\frac {1}{2}$


The key ref defines what's stored in \@currentlabel (typically written in the .aux file, but you're using it yourself). Writing in the .aux file is safe, because \protected@write is used by LaTeX; however you're using \write and \emph is not a command TeX likes to see there. You could use \protected@iwrite (you find how to define it on the site), but the trick I suggest above is even better because you can then decide what \answeritem gets expanded to. It's important its definition is \protected, so it will remain untouched during the \write operation.
• @ChristopherDonham The error message you get is misleading; it was due to \@currentlabel containing \emph, which is not a command that TeX likes to see in the argument to \write`. – egreg Dec 2 '17 at 1:31