# Modify the stored value of enumeration in output file

This question is in reference to a previous question: Write the value of enumerate to output file.

I would like to take the solution one step further, but I couldn't get it to work.

\documentclass{article}

\makeatletter
\newwrite\solutions@file
\newcommand{\collectSolutions}{\immediate\openout\solutions@file=\jobname.sol}
\newcommand{\sol@enumi}{\theenumi}
\newcommand{\sol@enumii}{\theenumi.\theenumii}
\newcommand{\sol@enumiii}{\theenumi.\theenumii.\theenumiii}
\newcommand{\solution}[1]{%
\immediate\write\solutions@file{%
\csname sol@\@enumctr\endcsname: \unexpanded{#1}%
}%
}

\newcommand{\printSolutions}{%
\immediate\closeout\solutions@file
\noindent\input{\jobname.sol}
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\collectSolutions

\begin{enumerate}
\item Text
\begin{enumerate}
\item Part Q 1 \solution{Ans}
\item Part Q 1 \solution{Ans}
\item Part Q 1 \solution{Ans}
\end{enumerate}
\item Another Q \solution{Ans}
\end{enumerate}

\printSolutions
\end{document}


This results in a solutions being typeset as: 1.a: Ans 1.b: Ans 1.c: Ans 2: Ans

Ideally I would like to have 1.a: Ans, b: Ans, c: Ans. 2: Ans.

There are two differences: 1: the leading enumi number is only mentioned once; 2: there are commas between parts an a fullstop at the last one.

So it seems that I need to redefine \sol@enumi, \sol@enumii and \sol@enumiii macros. I suppose what I want is that the macro takes one value at a certain situation (first for 1: and last for 2:)

I tried the following using the ifthen package:

\renewcommand{\sol@enumi}{\theenumi.\let\OLDtheenumi\theenumi}
\renewcommand{\sol@enumii}{\ifthenelse{\equal{\theenumi}{\OLDtheenumi}}{theenumii}{\theenumi(\theenumii)}.}


but I get errors; probably due to what gets expanded where. Is there a way to achieve this?

-

\documentclass{article}

\makeatletter
\newwrite\solutions@file
\newcommand{\collectSolutions}{\immediate\openout\solutions@file=\jobname.sol}
\newcommand{\sol@enumi}{{\theenumi}}
\newcommand{\sol@enumii}{{\theenumi.}\theenumii}
\newcommand{\sol@enumiii}{{\theenumi.\theenumii.}\theenumiii}
\newcommand{\solution}[1]{%
\immediate\write\solutions@file{%
\noexpand\solsep\csname sol@\@enumctr\endcsname: \unexpanded{#1\ignorespaces}%
}%
}

\def\solsep{\afterassignment\@solsep\def\@tempa}

\def\@solsep{%
\ifx\@tempa\sol@lastsec,
\else
\global\let\sol@lastsec\@tempa
\sol@stop\gdef\sol@stop{. }%
\@tempa
\fi
}

\newcommand{\printSolutions}{%
\let\sol@stop\@empty
\gdef\sol@lastsec{0.}%
\immediate\closeout\solutions@file
\noindent\input{\jobname.sol}\sol@stop
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\collectSolutions

\begin{enumerate}
\item Text
\begin{enumerate}
\item Part Q 1 \solution{Ans}
\item Part Q 1 \solution{Ans}
\item Part Q 1 \solution{Ans}
\end{enumerate}
\item Another Q \solution{Ans}
\end{enumerate}

\printSolutions
\end{document}

-
Many thanks. This works a treat! – Geoff Jan 12 '13 at 19:03
It's taken me two years to ask: but how can your code above be modified so that \theenumi part (and only this part) is in bold face? I simply tried wrapping \textbf but this didn't work. – Geoff Feb 5 '15 at 19:13
you mean the 1. wherever it occurs? Depends exactly what you want but \renewcomamnd\theenumi{\textbf{\arabic{enumi}.}} might be easiest – David Carlisle Feb 5 '15 at 19:28
Sorry, I didn't mean throughout, I meant when we call \printSolutions. So it is somewhere within the definition of your \solsep. – Geoff Feb 5 '15 at 19:42
\textbf{\@tempa} in \@solsep ? – David Carlisle Feb 5 '15 at 19:50