1

I am trying to modify a question-solution capability that was answered brilliantly a while back here. After each question text, one can freely use \solution to keep the solution directly with the question text. The solutions are stored in a .sol file through the command \collectSolutions and can be accessed in the document through \printSolutions. The code below is thanks to David Carlisle and works a charm (perhaps it should be a package). This question is about the fine tuning.

The code below will return the numbering of the solutions as: 1: 2a: b: c: 3ai: ii: 3b: 4:

I would like it to return: 1: 2a: b: c: 3ai: ii: b: 4:

\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{. }% \textbf{\@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 Question text \solution{Ans} \item Question text \begin{enumerate} \item Part Q \solution{Ans} \item Part Q \solution{Ans} \item Part Q \solution{Ans} \end{enumerate} \item Another Q \begin{enumerate} \item Part Q \begin{enumerate} \item Part Q \solution{Ans} \item Part Q \solution{Ans} \end{enumerate} \item Part Q \solution{Ans} \end{enumerate} \item Question text \solution{Ans} \end{enumerate} \printSolutions \end{document} 
5
  • Do you know the exsheets package? Commented Feb 5, 2015 at 21:31
  • When I first looked at exsheets I wasn't able to find out how to keep solutions directly embedded next to the question text. Commented Feb 5, 2015 at 21:47
  • what do you mean by embedded next to the question text exactly? Commented Feb 5, 2015 at 22:20
  • Look at the (not so) MWE above: One simply writes text for the question and then you put in a \solution{answer text}. Personally I find it very elegant. Commented Feb 5, 2015 at 22:25
  • Do you know about the probsoln package? Or you can use a combination of datatool and datatooltk Commented Oct 13, 2017 at 11:44

1 Answer 1

2

The solution is to rewrite the macro \sol@enumiii to group the two higher prefixes separately. So

\newcommand{\sol@enumiii}{{\theenumi.}{\theenumii.}\theenumiii} 

with two internal groups instead of the one group {\theenumi.\theeunmii.}. The code below also fixes the extraneous space before : in the output of your MWE.

Sample output

\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{. }% \textbf{\@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 Question text \solution{Ans one} \item Question text \begin{enumerate} \item Part Q \solution{Ans two A} \item Part Q \solution{Ans two B} \item Part Q \solution{Ans two C} \end{enumerate} \item Another Q \begin{enumerate} \item Part Q \begin{enumerate} \item Part Q \solution{Ans three A one} \item Part Q \solution{Ans three A two} \end{enumerate} \item Part Q \solution{Ans three B} \item Part Q \begin{enumerate} \item Part Q \solution{Ans three C one} \item Part Q \solution{Ans three C two} \end{enumerate} \end{enumerate} \item Question text \solution{Ans four} \end{enumerate} \printSolutions \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.