2

For a study book that I and others are writing, we want to include some exercises. Eventually we want all the exercises to have a solution, but for now, due to limited time, we are able to only include solutions to some exercises. However, we still want to include the exercises for students to try and find answers. For those environments that do not have a solution, we would like to display a text or something else inside the exercise environment to make clear that the solution is not yet available.

The exercises are implemented with an exercise environment, and the solutions are within a solution environment that is created with the answers packages. These solution environments are placed within the specific exercise they belong to.

Now my question; How, if at all, can we detect if a solution environment is present in the exercise environment? And if it is present, how to detect whether it is empty or not?

I think it should be something like this (although this is not proper LaTeX syntax):

\documentclass{scrbook}

\usepackage{etoolbox}
\usepackage{answers}
\Newassociation{sol}{Solution}{solu}    

\newenvironment{exercise}{\textit{Exercise}
\begin{small}
}{
    \if %exercise environment does contain sol environment
        \if sol environment is empty 
             no solution available %display this text in document
        \fi
    \else %exercise environment does not contain sol environment
         no solution available %display this text in document
    \fi
\end{small}
}

\begin{document}

\Opensolutionfile{solu}[SolutionChapter]

\begin{exercise}
    Question?
    \begin{sol}
        Here is a solution
    \end{sol}
\end{exercise}

\begin{exercise}
    Question?
    \begin{sol}
    \end{sol}
    \textit{No solution available} %should be automatically detected
\end{exercise}

\begin{exercise}
    Question?
    \textit{No solution available} %should be automatically detected
\end{exercise}

\Closesolutionfile{solu}

\end{document}

If the solutions are available they should be handled as default by the answers package, so that they are put in a separate file. I am not very familiar with conditionals in TeX, so I hope this is possible.

  • Welcome to TeX.SX! Please complete your code to a minimal working example (MWE), that illustrates your problem. It will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob Aug 1 '17 at 11:41
  • @Bobyandbob Thanks. I hope this suffices as MWE. – P. de Jong Aug 1 '17 at 11:51
3

Testing for a missing sol-environment is rather easy, see below. Testing for an empty environment is more difficult as empty is not the same as "doesn't contain anything". You can have comments, or tests and other material in the environment that doesn't print anything. And there is also the problem of empty lines: the test here https://tex.stackexchange.com/a/333783/2388 wouldn't work if there is an empty line inside the environment. So one would probably to make a trial typesetting in some box and then check the size of this box.

\documentclass{scrbook}

\usepackage{etoolbox}
\usepackage{answers}
\Newassociation{sol}{Solution}{solu}
\newbool{withsol}
\newcommand{\presol}{\global\booltrue{withsol}}
\newenvironment{exercise}{\textit{Exercise}
\begin{small}\global\boolfalse{withsol}%
}{
    \ifbool{withsol} %exercise environment does contain sol environment
    {\fbox{\textit{with solution}}}{\fbox{\textit{no solution}}}
\end{small}
}

\begin{document}

\Opensolutionfile{solu}[SolutionChapter]

\begin{exercise}
    Question?
    \begin{sol}
        Here is a solution
    \end{sol}
\end{exercise}

\begin{exercise}
    Question?
    \begin{sol}
    \end{sol}
    \textit{empty sol: No solution available} %should be automatically detected
\end{exercise}

\begin{exercise}
    Question?
    \textit{No solution available} %should be automatically detected
\end{exercise}

\begin{exercise}
    Question?
    \begin{sol}
        Here is a solution
    \end{sol}
\end{exercise}

\Closesolutionfile{solu}

\end{document}

enter image description here

  • For something similar I tried the "typeset in a box and measure the box" approach. Works mostly, but not if say the only contents is something that writes in the margin (say fxnote with marginnote setting) – daleif Aug 1 '17 at 12:49
  • @daleif Imho there are a lot of possible pitfalls, e.g. if the solution is long, or contains a longtable, or tikz overlays, or changes counters. And there is the combination with the answers code which wants to write the environment verbatim.... I would only do it if there where a lot to gain as it is so much easier and safer to use a "nosol"-environment and rename it when needed. – Ulrike Fischer Aug 1 '17 at 13:04
  • Thank you. This works like a charm when there is no solution environment in the exercise environment. And on second thoughts, leaving out or renaming empty solution environments is probably better anyway, as then also no ugly empty solutions appear in the solutions chapter either. – P. de Jong Aug 2 '17 at 7:15
  • Well if one could reliably (and without side effects) test if a solution is empty in the exercise part one could also do it in the solution part ;-). But imho a clear markup is better. Beside the technical problems: It is so easy e.g. to overlook a lonely period or a ~ ;-) – Ulrike Fischer Aug 2 '17 at 7:20

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.