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}
\newwrite\answerkeyfile
\AtBeginDocument{%
\immediate\openout\answerkeyfile=\jobname.keys
}
\begingroup
\catcode`\#=12
\gdef\hashchar{#}%
\endgroup
\makeatletter
\newcommand{\answerkey}[1]{%
\begingroup
\let\#\hashchar
\immediate\write\answerkeyfile{\@currentlabel.\ }
\immediate\write\answerkeyfile{\unexpanded{#1}}%
\immediate\write\answerkeyfile{ }
\endgroup
}
\makeatother
\AtEndDocument{%
\immediate\closeout\answerkeyfile
}
\begin{document}
Answer these questions:
%% 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 1 \answerkey{Answer 1}
\item Question 2 \answerkey{$\frac{1}{2}$}
\item Question 3 \answerkey{\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}}
\end{enumerate}
Answer Key:
% 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
\immediate\write\answerkeyfile{\expandafter\unexpanded
\expandafter{\@currentlabel}#1\ }
\immediate\write\answerkeyfile{\unexpanded{#2}}%
\immediate\write\answerkeyfile{ }
\endgroup
}
In actual use, it looks like this:
If the default item labels are used:
\begin{enumerate}
\item Question 1 \answerkey{Answer 1}
If enumitem label formatting is used:
\begin{enumerate}[label={\arabic*.}]
\item Question 1 \answerkey[]{Answer 1}


\protected@write{\answerkeyfile}{}{\@currentlabel.\ }rather.\noexpandwill result in\@currentlabelbeing written to the file which is probably not wanted. – Dec 01 '17 at 20:32\@currentlabelseems to be deeply unexpandable here – Dec 01 '17 at 20:46