# Switching between environments

I teach at the university and I write exams in Tex. I'd like to write an exam with a number of questions, where for each question I include either empty lines and empty figures with axes (for the actual exam - students to fill) OR the solution, including complete figures. For example:

Q. Illustrate what happens to the demand of housing when interest rates go down.

\begin{nosolution}
A FEW HORIZONTAL LINES
\begin{figure}
\includegraphics[scale=1]{EmptyGraph_withAxes}
\end{figure}
\end{nosolution}

\begin{solution}
It will increase....
\begin{figure}
\includegraphics[scale=1]{HousingDemand}
\end{figure}
\end{solution}


Then set a paramater at the beginning of the exam such as \printsolution, which says to print what is within the \begin{solution}...\end{solution}, and otherwise print what is within the \begin{nosolution} ... \end{nosolution}. I defined a solution and nosolution environment:

\newenvironment{solution}
{
\vspace{0.5cm}
}{}

\newenvironment{nosolution}
{
\hrulefill
\hrulefill
\hrulefill
}{}


Is there a way to switch between one and the other depending on an initial parameter like \printsolution? I have been trying the exam class (exam.cls) but it is not perfect for what I need to do because it cannot take floats in the solutions: I get a lost float message. I also checked the documentation which confirmed that floats are not supported because it uses frames. thanks, Mario

• If the problem are floats a possible solution is not using them. I include graphics in my exams with just \includegraphics (without figure) where I need them. floats mean this, graphics will appear where LaTeX considers. They are good for documents, but I don't think the same for short exams. – Ignasi Apr 2 '14 at 9:13
• The comment package should allow you to switch one or other of the environments to be a "comment" – David Carlisle Apr 2 '14 at 9:38
• Besides David's comment, see also: How to have upside down text in body? – user11232 Apr 2 '14 at 10:03
• You can make conditionals through the etoolbox package and implement an if-then-else branch. – remus Apr 2 '14 at 10:33
• Also see this answer where they hide custom environments: tex.stackexchange.com/questions/15509/… – remus Apr 2 '14 at 10:43

Here is a MWE based on etoolbox:

\documentclass{article}
\usepackage{etoolbox}

%\edef\printsolution{} % Uncomment to print solutions.

\newcommand{\nosolution}[1]{%
\ifdefmacro{\printsolution}{}{#1}%
}

\newcommand{\solution}[1]{%
\ifdefmacro{\printsolution}{#1}{}%
}{}

\begin{document}

\nosolution{
No solution.
}

\solution{
Solution.
}

\end{document}

• Thanks all for the suggestions and sorry if it took so long, but I was busy with teaching and then trying to play with all the proposed solutions. In the end I think this solution is the simplest and works great for me. – mariodrumblue Apr 7 '14 at 4:41

The exsheets package has ready made solutions for this. The following example

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{exsheets}

runin           = true ,
title-post-code = \space ,
attach          = { main[l,vc]points[l,vc](\linewidth+\marginparsep,0pt) } ,
join            = { main[r,vc]title[r,vc](0pt,0pt) }
}

% setup:
\SetupExSheets{
question/name  = Q. ,
solution/name  = A. ,
% solution/print = true
solution/print = false
}

\begin{document}

\begin{question}
Illustrate what happens to the demand of housing when interest rates go down.
\end{question}

% if solutions are not printed:
\PrintSolutionsF{%
\blank[width=4.8\linewidth,linespread=1.5]{}% 4.8 lines to write on
\begin{center}
\includegraphics[scale=1]{EmptyGraph_withAxes}% demo picture
\end{center}
}

% if solutions are pinted:
\begin{solution}
It will increase\ldots
\begin{center}
\includegraphics[scale=1]{HousingDemand}
\end{center}
\end{solution}

\end{document}


gives

with solution/print = false and

with solution/print = true.

You could do something like below, setting \showanswerstrue or \showanswersfalse determines what is shown or what is hidden. It provides two ways to add an answer:

Use the answer/noanswer environment, which are defined with the comment package. Or you can just enter the answer below the question and use \onlyanswer to hide parts. You probably want to add to this an optional argument to offer replacement text or a \hrule, this isn't built in right now. This can be used to hide parts of an answer for fill in questions or remove a plot from a graph. Note that if you have no plots in the graph you have to set up the axis by hand.

Finally there is a \showlines{<n>} command that draws <n> lines.

\documentclass{article}

%% Define the switch to control which is shown
\usepackage{comment,forloop}
\showanswersfalse   % Comment out one of these to show

\ifshowanswers% Hide either of the two environments defined above
\else
\fi

%% Define an environment to markup the questions
\newcounter{questionnumber}
\setcounter{questionnumber}{0}
\newenvironment{question}%
{
\stepcounter{questionnumber}
\bf \arabic{questionnumber}.
}
{\par\vspace{1em}}

%% Some extra commands that might be helpful
\newcommand\showlines[1]{%
\newcounter{numlines}
\forloop{numlines}{0}{\value{numlines} < #1}{\hrulefill\par}%
}

%% Only necessary for the plot in the second example question
\usepackage{pgfplots}

\begin{document}
Introduction to exam

\begin{question}
Some question
\end{question}
The true answer to the question
\showlines{3}

\begin{question}
A second question with a plot to be filled in

\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=10,
ymin=0, ymax=100,
]
\end{axis}
\end{tikzpicture}
\end{question}

\end{document}


Update: added some whitespace and a counter to number the questions

Thanks all for the suggestions. In the end I took a bit from everyone and made my own customised commands. Hope this can be useful to others

\documentclass{article}
\usepackage{forloop,etoolbox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solutions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define an new command to switch between solution and nosolution with
% Command showlines defines the number of lines.

\newcommand{\nosolution}[1]{%
\ifdefmacro{\printsolution}{}{#1}%
}

\newcommand{\solution}[1]{%
}

\newcounter{numlines}
\newcommand\showlines[1]{%
\setcounter{numlines}{0}%
\forloop{numlines}{0}{\value{numlines} < #1}{\vskip0.3cm \hrulefill}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\edef\printsolution{} % Uncomment to print solutions.

\begin{document}

THIS IS THE QUESTION