Hide/Show Solution for Problems

There are too many options for "Solution/Answer" environments which we can use and I am adopting this one for my "big" project that I am doing.

At the moment, it looks like the easiest one for me to implement, but there is just one function to be explored. How can I show/hide the solution environment, so that I can have a teachers' version and a students' version?

When the solutions are hidden, preferably that the space I used for these content are "reserved."

Thanks.

• Hope it's okay that I'm sneeaking in here that I have made a package called exercisebank which is pretty light weight and has an easy \DisplaySolutions command that you toggle it with :) – Andreas Storvik Strauman May 30 '18 at 0:15
• What do you mean by "reserved" space? – Andreas Storvik Strauman May 30 '18 at 0:24
• @AndreasStorvikStrauman Sorry if I wasn't clear. What I meant is that simply in the students version, the solutions are "invisible" and replaced using white space. So if a solution goes on across two pages, in the students version, they simply see white space carried over to the "next page" with more white space. – Chen Stats Yu May 30 '18 at 0:55
• Updated my answer, this should work :) – Andreas Storvik Strauman May 30 '18 at 1:32
• OP has posted a MWE example, which I've encouraged to be edited into this question. – Teepeemm May 30 '18 at 13:36

tcolorbox offers lowerbox option to make visible or not their lower parts. This is a little example to show how it works. This option could be combined with recording mechanisms to store and later on show solutions (an example in this answer).

\documentclass{article}

\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\NewTColorBox[auto counter]{exercise}{+O{}}{%
enhanced, breakable,
coltitle=black,
fonttitle=\bfseries,
title={Exercise~\thetcbcounter:},
#1}

\tcbset{student/.style={lowerbox=invisible}}

\begin{document}

\begin{exercise}
Type some nice problem, not too long.
\tcblower
An this is the solution
\end{exercise}

\begin{exercise}[student]
\lipsum[1]
\tcblower
\lipsum[1]
\end{exercise}

\begin{exercise}[student]
\lipsum[2]
\tcblower
\lipsum[3]
\end{exercise}
\end{document}

• Tried this version with whole document, and got this error: ! pdfTeX error (setup): \pdfminorversion cannot be changed after data is written to the PDF file. – Chen Stats Yu Jun 5 '18 at 7:42
• Further, how do I control [student] as a general global option? I don't want to add/delete [student] every time when I compile the PDF. Thanks. – Chen Stats Yu Jun 5 '18 at 7:56
• @ChenStatsYu About the error, I have no idea. – Ignasi Jun 5 '18 at 9:09
• @ChenStatsYu The easiest way for printing or not answer would be to forget student option and include lowerbox=invisible declaration inside exercise definition. This way all answer will be hidden. Whan you want to show them, comment out lowerbox line (or change it to lowerbox=visible) and compile again. If you want to decide from command line look at tex.stackexchange.com/questions/239411/… or similar questions. – Ignasi Jun 5 '18 at 9:14
• @ChenStatsYu You should look for bicolor and colbacklower at tcolorbox documentation. – Ignasi Jun 5 '18 at 12:13

Doing this is a lot of work, and that is why there are, as you say, so many options for "Solution/Answer" packages. You might actually find a package that fits your need pretty easily; maybe exercisebank, xsim, or some of the other alternatives in the exercise and exam topics on CTAN.

That being said, you can as @Levy says, use the comment package to hide your environment. Maybe the \vspace workaround that @Levy suggests could be sufficient, but I couldn't help myself in regards to the automatic height question.

You could use boxes to make sure the height is appropriate. That is, the actual height of the content. Almost.

By defining the \ProcessCutFile to be empty then the comment package won't do anything (it's all in the docs). Further more, we can use the \CommentCutFile to put the content of the environment into a \box, which we can get the height of. Then we just apply \vskip to push this height.

However, if we want it for multiple pages, I came up with a solution that puts \vskip 10pt until it reaches the height of the box. A little hacky, I know.

Only drawbacks are that this height thing would probably not work with verbatim environments like e.g. listings.

\documentclass{article}
\usepackage{comment}
\usepackage{fp}
\usepackage{lipsum}

% Make ourselves a new conditional
\newif\ifDisplaySolutions
% Use \DisplaySolutionstrue to "activate" it
\makeatletter
% Make a solution environment
\generalcomment{solution}{%
\begingroup
\ifDisplaySolutions\else%
% if \DisplaySolutionstrue is not called, then we remove the contents
\def\ProcessCutFile{}\fi%
}{%
\ifDisplaySolutions\else%
% aand,now (also when it's not called), we make a box
% and then we \input the \CommentCutFile.
\setbox1=\vbox{\input{\CommentCutFile}}%
\edef\boxheight{\strip@pt\ht1}
% Get the height from \ht1 and use \vskip to make appropriate space
\newcount\Scount
\Scount=0
\FPdiv\boxPartHeight{\boxheight}{10}
\fi
\endgroup%
}
\makeatother

%Uncomment below to hide solution
\DisplaySolutionstrue
\begin{document}
Some problem text goes here \lipsum[1]

\begin{solution}
\textbf{SOLUTION}\lipsum
$\forall\varepsilon>0\exists\delta>0:\dots$
\end{solution}
Some text after solution.

\end{document}

Edit: Made it work across pages

You can put the teacher's section between a block of comment using the package comment.

\begin{comment}
...
text
...
\end{comment}

Then you can compile a version for students. If you want to compile the teacher's version just delete the \begin{comment} and \end{comment}.

You can do exactly what you want using \includecomment{teacher} or \excludecomment{teacher} with the text inside the block teacher.

\documentclass[10pt,a4paper]{report}
\usepackage{comment}

\begin{document}

Text not commented

\includecomment{teacher}
\begin{teacher}
text
\end{teacher}

Text not commented

\end{document}

Now for the reserved space you desire one simple workaround is use the vspace{} command.

\documentclass[10pt,a4paper]{report}
\usepackage{comment}

\begin{document}

Text not commented

\excludecomment{teacher}
\begin{teacher}
text
\end{teacher}
\vspace{3cm}

Text not commented

\end{document}

The reserved space left between the problems are 3cm.

• The OP writes 'preferably that the space I used for these content are "reserved." ' I don't really know what this means tbh, but I interpret it as the space would be white? – Andreas Storvik Strauman May 30 '18 at 0:24
• Oh I got it. Let me see something. – Levy May 30 '18 at 0:25

I've had good results taking your example and adding \usepackage{xcolor}. Then when I want the student version, I can add the option bodyfont=\color{white} to \declaretheoremstyle{solstyle}. The text still exists (so it's probably not safe to give students the electronic pdf), but it is invisible in the output.

• Just had a problem with figure enviro. They still appear. But well, not a big deal then. lol – Chen Stats Yu May 31 '18 at 3:24
• Also, if I have anything like ref{XXX}, there will still be COLOR links. – Chen Stats Yu May 31 '18 at 3:24
• In hyperref, I believe you can use \ref*{XXX} to get the appropriate reference without a link (it's also possible to pass the hidelinks option so that the links aren't colored. – Teepeemm May 31 '18 at 16:04