8

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.

4 Answers4

3

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.

Levy
  • 1,167
3

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}
    {\nullfont\loop\vskip 10pt\relax\advance\Scount by 1 \ifnum\Scount<\boxPartHeight\relax\repeat}
  \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

2

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.

Teepeemm
  • 6,708
  • 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 03:24
  • Also, if I have anything like ref{XXX}, there will still be COLOR links. – Chen Stats Yu May 31 '18 at 03: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
1

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:},
        attach title to upper=\quad,
        before lower={\textbf{Solution~\thetcbcounter:\quad}},
        #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}

enter image description here

Ignasi
  • 136,588
  • 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 05 '18 at 07: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 05 '18 at 07:56
  • @ChenStatsYu About the error, I have no idea. – Ignasi Jun 05 '18 at 09:09
  • 1
    @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 https://tex.stackexchange.com/questions/239411/switch-between-different-output-versions-compiled-from-the-same-latex-source?noredirect=1&lq=1 or similar questions. – Ignasi Jun 05 '18 at 09:14
  • Thanks. This looks like the best option for me to adapt and move forward. – Chen Stats Yu Jun 05 '18 at 09:26
  • One more question, where would I find the options for color? I have been looking at the codes, but it does not seem to be there. Can't see grey or anything similar. Is it possible to have different color for lowerbox too? – Chen Stats Yu Jun 05 '18 at 10:14
  • colback=white changes all color, is it possible to do different color for lowerbox only?? – Chen Stats Yu Jun 05 '18 at 10:25
  • 1
    @ChenStatsYu You should look for bicolor and colbacklower at tcolorbox documentation. – Ignasi Jun 05 '18 at 12:13