3

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}
\textbf{Solution.} \quad \itshape
}{}

\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

7
  • 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
    Commented Apr 2, 2014 at 9:13
  • The comment package should allow you to switch one or other of the environments to be a "comment" Commented Apr 2, 2014 at 9:38
  • Besides David's comment, see also: How to have upside down text in body?
    – user11232
    Commented Apr 2, 2014 at 10:03
  • You can make conditionals through the etoolbox package and implement an if-then-else branch.
    – remus
    Commented Apr 2, 2014 at 10:33
  • Also see this answer where they hide custom environments: tex.stackexchange.com/questions/15509/…
    – remus
    Commented Apr 2, 2014 at 10:43

4 Answers 4

2

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

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

% custom headings:
\DeclareInstance{exsheets-heading}{myheading}{default}{
  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{
  headings       = myheading ,
  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

enter image description here

with solution/print = false and

enter image description here

with solution/print = true.

1

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}
1
  • 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. Commented Apr 7, 2014 at 4:41
1

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 
% empty lines instead
% Command showlines defines the number of lines. 

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

\newcommand{\solution}[1]{%
    \ifdefmacro{\printsolution}{\vskip0.1cm \noindent \textbf{Answer:} \textit{#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

\solution{THIS IS THE ANSWER. 
}
\nosolution{
\showlines{6}
}

\end{document}
0

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}
\newif\ifshowanswers
\showanswersfalse   % Comment out one of these to show
%\showanswerstrue    % or hide the answers

%% Define the answer/noanswer environments and hide when necessary
\specialcomment{answer}{}{\vspace{2em}}     % Add some space between
\specialcomment{noanswer}{}{\vspace{2em}}   % the environments
\ifshowanswers% Hide either of the two environments defined above
  \excludecomment{noanswer}
\else
  \excludecomment{answer}
\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\onlyanswer[1]{\ifshowanswers{#1}\else{}\fi} % Discard contents if not needed
\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}
\begin{answer}
The true answer to the question
\end{answer}
\begin{noanswer}
\showlines{3}
\end{noanswer}

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

\begin{tikzpicture}
  \begin{axis}[
      xmin=0, xmax=10,
      ymin=0, ymax=100,
    ]
    \onlyanswer{\addplot[domain=0:10,no marks] {x^2};}
  \end{axis}
\end{tikzpicture}
\end{question}

\end{document}

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .