3

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?

3

enter image description here

\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}
7
  • 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 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 ? Feb 5 '15 at 19:50
  • Aha, the third instance works. Now I see another problem: If you need enumiii, the numbering becomes say: 1,2a,b,c,2di,ii,iii,2e,3 instead of 1, 2a,b,c,di,ii,iii,3. Somehow the test of which enum level works from enumi to enumii (in that the leading number is surpressed) but not when we get to enumiii.
    – Geoff
    Feb 5 '15 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.