# Versions package producing unexpected behavior

I want to create a package for formatting problem sets. Also, I want an easy way to produce versions with the problem statement and the solutions and versions with only the problem statements. I have something almost like what I want (see below). The only thing going wrong is that I don't get the "Begin Solution ... End Solution" when I include the solutions (I do get the "solution text goes here"). Note that I want the problem environment to change based on whether the solutions are included or not, which is why I had my own \IncludeSolutions command wrapped around the \includeversion command. Any suggestions on how to make this work? I'm also not particularly attached to this approach, so let me know if there is a superior way of approaching the problem. I myself thought of a better way to do this would be to use kvoptions to pass an argument to my package, and then define my problem/solution environments based on the argument, rather than using a separate command to redefine the problem environment, but I couldn't get this to work, either.

I define the commands in MyProblemSet as follows.

\ProvidesPackage{MyProblemSet}

\RequirePackage{versions}

\newenvironment{problem}
{
\textbf{Begin Problem (without solution)}:\\
}
{
\\\textbf{End Problem (without solution)}\\
}

\newenvironment{solution}
{
\textbf{Begin Solution}:\\
}
{
\\\textbf{End Solution}\\
}
\excludeversion{solution}

\newcommand{\IncludeSolutions}
{
\renewenvironment{problem}
{
\textbf{Begin Problem (with solution)}:\\
}
{
\\\textbf{End Problem (with solution)}\\
}
\includeversion{solution}
}

Here is an example of how I would like to use them.

\documentclass{article}
\usepackage{MyProblemSet}
\IncludeSolutions

\begin{document}

\begin{problem}
problem text goes here
\end{problem}
\begin{solution}
solution text goes here
\end{solution}

\end{document}
-
Here's an idea. \usepackage{etoolbox} to create \newtoggle{includesolutions}. Create your solution environment with \NewEnviron from \usepackage{environ} and put the toggle around your text and the \BODY. –  Ethan Bolker Jun 23 '13 at 1:20
There are a few packages that do this kind of thing- the answers is my favourite. Of course, that's not to say that you shouldn't create your own.... One thing to be careful of- you need to use % in some of your environment definitions to avoid extra vertical space –  cmhughes Jun 23 '13 at 1:35
You could also borrow some code from the exam package/class. The \[no]printanswers macro does what you need. –  Sean Allred Jun 23 '13 at 1:48

## 1 Answer

Thanks for the replies: I had been using the exam package, but I wanted more control than I thought I could get without writing my own package (this wasn't reflected in my minimal working example). Anyway, I tried using kvoptions.sty again, and got it to work this time (I'm not sure what I messed up before). Here is what I did.

This is my testkv.sty file

\ProvidesPackage{testkv}

\RequirePackage{verbatim}
\RequirePackage{kvoptions}
\SetupKeyvalOptions
{
family = TKV,
prefix = TKV@
}

\DeclareBoolOption[false]{solutions}

\ProcessKeyvalOptions*

\ifTKV@solutions
\newcommand{\status}{yes solutions}
\newenvironment{solution}
{
\textbf{Begin Solution}\\
}
{
\\\textbf{End Solution}\\
}
\newenvironment{problem}
{
\textbf{Begin Problem}\\
}
{
\\\textbf{End Problem}\\
}
\else
\newcommand{\status}{no solutions}
\newenvironment{solution}
{
\comment
}
{
\endcomment
}
\newenvironment{problem}
{
\textit{Begin Problem}\\
}
{
\\\textit{End Problem}\\
}
\fi

and this is my main file

\documentclass{article}
% \usepackage[solutions = false]{testkv}
\usepackage[solutions = true]{testkv}

\begin{document}

\begin{problem}
problem text goes here
\end{problem}
\begin{solution}
solution text goes here
\end{solution}

\end{document}
-