5

Suppose I am using the exam class. There is an option \printanswers to print solutions, otherwise solutions aren't printed. I would like to suppress questions and just print the solutions. Is this possible?

Edit

I've added a basic work-around, but perhaps there is a better way to do this? Here's a demo using the modified exam class, found here.

% Document
\documentclass[12pt]{exam2}

\usepackage[margin=1.0in]{geometry}
\usepackage{fancyhdr}
\usepackage{amsmath}

\pagestyle{fancy}
\lhead{The author}
\rhead{The assignment}

\setcounter{section}{1}
\unframedsolutions

% Preamble
\printquestions
\printanswers

\begin{document}
\numberwithin{question}{section}

\begin{questions}

\begin{quest}
What is $\int_{0}^{5} x^2 dx$?

\end{quest}

\begin{solution}

This integral can be calculated as

\begin{align}
\int_{0}^{5} x^3 &= \Big[ \frac{x^3}{3} \Big|_{0}^{5} \notag \\
&= \frac{5^{3}}{3}
\end{align}

\end{solution}

\begin{quest} 
The next question would go here

\questp{a}{Part a of the question}
\questsp{i}{Subpart of a}
\questspnob{More information}

\end{quest}

\begin{solution}
The next answer would go here

\begin{parts}
\part Parts works in solution environment
\begin{subparts}
\subpart Here is a subpart
\end{subparts}
\end{parts}

\end{solution}

\end{questions}

\end{document}

Questions and answers

% Preamble: 
\printquestions
\printanswers

Questions and answers

Questions only

% Preamble:
\printquestions
%\printanswers

Questions

Answers only

% Preamble:
%\printquestions
\printanswers

Answers

3
  • Since the solution has to be specified within a question, dropping the question content is difficult -- in my point of view this is a design flaw of exam.cls, unless I've missed some crucial aspect
    – user31729
    Aug 3, 2015 at 6:31
  • The answers package is harder to use, but allows you to spit out the answers to a separate file to be \input at will.
    – vonbrand
    Aug 3, 2015 at 18:32
  • As @nathanesau suggests, the answers package will generate a separate file of answers. Then you can \input that file in a document for answers only. The package isn't hard to use for just this purpose. Aug 3, 2015 at 21:40

1 Answer 1

2

I made the following changes to exam.cls.

First I added a \printquestions option.

\newif\ifprintanswers
\printanswersfalse
\DeclareOption{answers}{\printanswerstrue}
\DeclareOption{noanswers}{\printanswersfalse}

% BEGIN EDIT %
\newif\ifprintquestions
\printquestionsfalse
\DeclareOption{yesquestions}{\printquestionstrue}
\DeclareOption{noquestions}{\printquestionsfalse}
% END EDIT %

Then later on

\def\printanswers{\printanswerstrue}
\def\noprintanswers{\printanswersfalse}

\def\printquestions{\printquestionstrue}

Next, I modify the solution environment. If I don't print the questions, then I wanted the solution environment to act as a \question.

\newenvironment{solution}[1][0pt]{%
  \ifprintquestions % act as a solution
    \@insolutiontrue % cancelled by the end of the environment
    \@addpointsfalse % cancelled by the end of the environment
    \ifprintanswers
      \begingroup
      \Solution@Emphasis
      \begin{TheSolution}%
    \else
      \ifcancelspace
        % Do nothing
      \else
        \par
        \penalty 0
        \vspace*{#1}%
      \fi
      \setbox\z@\vbox\bgroup
    \fi
  \else %act as a question
    \question
  \fi
  }{%
  \ifprintquestions %act as a solution
    \ifprintanswers
      \end{TheSolution}%
      \endgroup
    \else
      \egroup
    \fi
    \fi
  }%

Next, I add a quest environment which can be hidden. However, this would need to be used instead of \question.

\newenvironment{quest}[1][0pt]%
  {%
    \ifprintquestions % act as a question
        \question
    \else % don't show the question
        %\par
        %\vspace*{-9mm} %
      \setbox\z@\vbox\bgroup
    \fi
  }{%
    \ifprintquestions
        % don't do anything
    \else % hide the group
      \egroup
    \fi
  }%

One annoying thing is that itemize lists, parts and subparts won't work in the environment I defined (I believe this is due to the \question call initializing some variables which I don't initialize. Therefore, I created some basic itemize list commands using the tabular environment.

% PART
\newcommand{\questp}[2]{%
\begin{tabular}{R{0.25cm} p{14.75cm}}
(#1) & #2 \\ 
\end{tabular}

{}
}

\usepackage{array,booktabs,ragged2e}
\newcolumntype{R}[1]{>{\RaggedLeft\arraybackslash}p{#1}}
\newcommand{\questsp}[2]{%
 \hspace*{2em}\begin{tabular}{R{1cm} p{13.15cm}}
(#1) & #2 \\
\end{tabular}

{}
}

% SUBPART
\newcommand{\questssp}[1]{%
\hspace*{8em}\begin{tabular}{p{10.75cm}}
#1 \\ 
\end{tabular}

{}
}

Some more part commands could be defined like this.

\newcommand{\questpnob}[1]{%
\begin{tabular}{R{0.25cm} p{13.15cm}}
 & #1 \\
\end{tabular}

{}
}

\newcommand{\questspnob}[1]{%
 \hspace*{2em}\begin{tabular}{R{1cm} p{13.15cm}}
& #1 \\
\end{tabular}

{}
}
2
  • 1
    I did not test this, but I hope, you didn't edit exam.cls directly?
    – user31729
    Aug 3, 2015 at 19:25
  • No of course not.
    – nathanesau
    Aug 3, 2015 at 20:02

You must log in to answer this question.

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