0

I have a framework for creating exercises, where the content of the solution to each subproblem is fed into a \solution-macro, which only displays its content if a global boolean is set to true.

What I'm interested in is having unique equation/figure/table/listings numbers for both the version with and without the solution. In particular, I need to take into account if the counters are increased in the solution. The intended benefit of this is that equations numbers (and other things) that might be referred to by the students in their solutions don't change their number once the solution is published later.

My first attempt to do this was to try to typeset everything into a box that is not printed (e.g. by \sbox or by the lrbox-environment) - in the hope that this would actually increase the counters - but I already run into trouble with equation environments.

Below is an MWE, where Euler's formula should have equation number (3) for both settings of the toggle with_solution. For the solution-less version to work, the (content of the) third argument of \iftoggle in \solution has to be commented out.

Edit: One solution to circumvent the problem would be to subordinate all relevant counters to subproblem, because then the occurrences in the formulation are the first and therefore necessarily unique (as the formulation appears in both versions - with and without the solution). However, this is not an interesting solution for me, because in actuality (vs. the cut-down MWE), my equations already look like (5.3.2) for eq. 2 of prob. 3 of exercise 5, while the subproblem look like "5.3a)", "5.3b)", etc. (making a consistent notation - i.e. (5.3a.2) - unattractive to me). Subordinating the counters to problem doesn't help, because between the different formulations for each subproblem, the solutions are able to increment different counters that are not reset by calling \subproblem.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{amsmath}
\usepackage{xparse}

\newtoggle{with_solution}
\newsavebox\tempbox
\NewDocumentCommand{\solution}{+m}{
\iftoggle{with_solution}{
    \par\medskip\noindent\textbf{Solution:} #1
    }
    {
%   \sbox\tempbox{#1}
    \begin{lrbox}\tempbox
    #1
    \end{lrbox}
    }
}

% \problem and \subproblem cut to bare bones for simplicity
\newcommand{\problem}[1]{\section{#1}}
\newcounter{subproblem}
\newcommand{\subproblem}{\par\medskip\noindent\stepcounter{subproblem}{\bfseries\alph{subproblem})}\quad} 

\toggletrue{with_solution}          % Set to true to include solution
%\togglefalse{with_solution}        % Set to false to exclude solution

\begin{document}

\problem{A Problem}

\subproblem Problem Formulation
\solution{Solution}

\subproblem Problem Formulation with \eqref{eq:prb}
\begin{equation}\label{eq:prb}
a=b
\end{equation}
\solution{Solution with \eqref{eq:sol}

\begin{equation}\label{eq:sol}
x=y
\end{equation}
}

\subproblem Problem Formulation with \eqref{eq:prb2}, having the same number both with or without typesetting the solutions.

\begin{equation}\label{eq:prb2}
\mathrm{e}^{2\pi\mathrm{i}}=1
\end{equation}
\solution{The \texttt{$\backslash$solution}-macro should be able to handle \texttt{$\backslash$par}'s, figures, tables, listings (and also increase their counters accordingly, even when not typeset)...}

\end{document}
  • 1
    \setbox\tempbox=\vbox{#1} is a better candidate than the lrbox environment. – egreg Sep 23 '14 at 15:15
  • @egreg: This works for equations - I'll check it for the other things momentarily. Do you want to post it as an answer, or do you think the question should be deleted? Even though I tried to go through tex.stackexchange.com/a/83936/42225 before I asked the question, I obviously don't understand the differences between the \hbox of \sbox and \vboxenough... – Axel Sep 23 '14 at 15:19
  • I'm not following you. Your example is too small for describing the problem. I'll remove all my comment. – egreg Sep 23 '14 at 15:57
  • @egreg: ok, I'll delete my comments and try to clarify the question. – Axel Sep 23 '14 at 15:59
  • @egreg: while trying to expand the question to make it clearer, I found that your solution is now working in all cases (don't know why it didn't work before). I therefore consider the question answered (but I edited it a little nevertheless to address your points). Thanks again for the tip! – Axel Sep 23 '14 at 17:10
1

Another way to go

\NewDocumentCommand{\solution}{+m}{
    \iftoggle{with_solution}
        {\par\medskip\noindent\textbf{Solution:} #1\par}
        %{\setbox\tempbox=\vbox{#1}}} % this line comes from egreg's comment
        % The following line is my alternative
        {\par\vbox to0cm{\vspace*{\paperheight}\medskip\noindent\textbf{Solution:} #1\vspace*{-\paperheight}}\par}}

Notice that commands like \addtocontents are executed during output routine. Therefore in my answer the \listoffigure contains all figures, no matter they are shown or not. (In fact they are shown...) While egreg's answer produces \listofshownfigure.

For Spacing

Dealing with negative spaces is not fun. I would like to put it aside. Experiments show that \par\marginpar{}\par behaves as \par.

\NewDocumentCommand{\solution}{+m}{
    \iftoggle{with_solution}
        {\par\medskip\noindent\textbf{Solution:} #1\par}
        %{\setbox\tempbox=\vbox{#1}}} % this line comes from egreg's comment
        % The following line is my alternative
        {\par\marginpar{\moveright\paperwidth\vbox to0cm{\medskip\noindent\textbf{Solution:} #1}}\par}}
| improve this answer | |
  • I like this version even better, because even though I can't think of a particular use for this right now, it allows references to content within the solution to be resolved (in addition to your remark about \listoffigures). I'll accept this answer. – Axel Sep 24 '14 at 7:07
  • One more question, I'd like vertical spacing between the formulations to be the same as if the solution actually weren't there (i.e. {} as the third argument of \iftoggle). But with the invisible content of \vbox a new line is started and so the spacing increases. For \solution's directly following text, it works if there are no \par's between the formulation and the solution, resp. it seems substracting \baselineskip is almost correct (aside from the glue I guess) when \par's are present, but after equations etc. both these attempts don't produce the right spacing. – Axel Sep 24 '14 at 7:39
  • I just saw your edit, thanks! Unfortunately, the result is still not satisfactory after equations. I tried searching some more, and found that surrounding the \vbox (i.e. your first solution) with \@bsphack-\@esphack (see tex.ac.uk/ctan/macros/latex/contrib/sphack/sphack-doc.pdf) works as expected for text (edit: sadly, only if there's no \par in between), but also doesn't work well after equations. But it feels like a step closer, because the \@bsphack-\@esphack combination uses the actual skips and penalties that were present before. – Axel Sep 24 '14 at 18:40

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.