Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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}
share|improve this answer
    
Many thanks. This works a treat! –  Geoff Jan 12 '13 at 19:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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