How to HIDE and SHOW answer?

Here I introduce \Calculate{} to provide the problem, and \Solution{} to provide the answer. Note that \Solution automatically restates the "problem" as the first line, and the optional argument of [] suppresses the line feed that normally would follow (shown in item 1).

\documentclass[10pt]{amsart}
\usepackage{tabstackengine,enumitem}
\newcommand\Calculate[1]{Calculate $#1$\def\Question{#1}}
\newcommand\Solution[2][\\]{Solution: \Longunderstack[l]{\Question#1#2}}
\stackMath
% TO PROVIDE BLANK SOLUTION
%\renewcommand\Solution[2][\\]{Solution: \setbox0=\hbox{%
%  \Longunderstack[l]{\Question#1#2}}\rule[-\dp0]{0pt}{\dimexpr\ht0+\dp0}}
% TO PROVIDE NO SOLUTION
%\renewcommand\Solution[2][\\]{}
\begin{document}
\sffamily
\begin{enumerate}[listparindent=0in,itemsep=5pt]
\item \Calculate{1 + 2}

  \Solution[]{=3}

\item \Calculate{1 + 2 + 3}

  \Solution{=3+3\\=6}

\item \Calculate{1+2+3+4}

  \Solution{=3+3+4\\=6+4\\=10}

\item \Calculate{1+2+3+4+5}

  \Solution{=3+3+4+5\\=6+4+5\\=10+5\\=15}

\item
\end{enumerate}
\end{document}

enter image description here

To get the solutions blanked out, just uncomment the lines to redefine \Solution as

\renewcommand\Solution[2][\\]{Solution: \setbox0=\hbox{%
  \Longunderstack[l]{\Question#1#2}}\rule[-\dp0]{0pt}{\dimexpr\ht0+\dp0}}

enter image description here

To get no solution, just uncomment the other line to redefine \Solution as

\renewcommand\Solution[2][\\]{}

enter image description here


ALTERNATE

If you would like to not have to type the = at the beginning of each line of the solution, that can be achieved but only by giving up the optional argument capability of suppressing the line feed after the problem restatement (compare item 1).

\documentclass[10pt]{amsart}
\usepackage{tabstackengine,enumitem}
\def\stackalignment{l}
\newcommand\Calculate[1]{Calculate $#1$\def\Question{#1}}
\newcommand\Solution[1]{Solution: \stackunder{\Question}
  {\tabbedLongunderstack[l]{#1}}}
\stackMath
\TABstackMath
\TABstackMathstyle{=}
% TO PROVIDE BLANK SOLUTION
%\renewcommand\Solution[1]{Solution: \setbox0=\hbox{\stackunder{\Question}%
%  {\tabbedLongunderstack[l]{#1}}}\rule[-\dp0]{0pt}{\dimexpr\ht0+\dp0}}
% TO PROVIDE NO SOLUTION
%\renewcommand\Solution[1]{}
\begin{document}
\sffamily
\begin{enumerate}[listparindent=0in,itemsep=5pt]
\item \Calculate{1 + 2}

  \Solution{3}

\item \Calculate{1 + 2 + 3}

  \Solution{3+3\\6}

\item \Calculate{1+2+3+4}

  \Solution{3+3+4\\6+4\\10}

\item \Calculate{1+2+3+4+5}

  \Solution{3+3+4+5\\6+4+5\\10+5\\15}

\item
\end{enumerate}
\end{document}

enter image description here


You could use the exam class, which would also allow you to leave space for the solution. It also allows you to assign points to questions or parts of questions (which I omit here).

\documentclass[
answers
]{exam}
\usepackage{mathtools}
\begin{document}
\begin{questions}
\question Calculate \(1 + 2\)
\begin{solution}
\(1 + 2 = 3\)
\end{solution}

\question Calculate \(1 + 2 + 3\)
\begin{solution}
\begin{align*}
& 1 + 2 + 3\\
& = 3 + 3\\
& = 6
\end{align*}
\end{solution}
\end{questions}
\end{document}

enter image description here


In addition to what erik said, I've found that for many classes I wanted the text of all the questions followed by each question with space to fill. I've extended the exam class to do this automatically, with just typing the questions once and a couple of passes of the compiler.

%\NeedsTeXFormat{Latex2e}[1996/06/01]
\ProvidesClass{examExt}[2016/11/09 v1.0]

\LoadClassWithOptions{exam}
\RequirePackage{etex}

\global\newwrite\examfile%

\newcommand\insertQuestions[1][plain]{
\begingroup
\IfFileExists{./\jobname.exm}{\thispagestyle{#1}\input{\jobname.exm}}{\typeout{Class exam Warning: Questions file created. Rerun to get expected output.}}
\endgroup
\immediate\openout\examfile=\jobname.exm
}



\AtEndDocument{
\immediate\closeout\examfile
}

\newcommand\create@environment[1]{
\newenvironment{my#1}{%
\begin{#1}
\immediate\write\examfile{\string\begin{#1}}
}{%
\immediate\write\examfile{\string\end{#1}}
\end{#1}
}
}



\newcommand\create@item[1]{
\expandafter\newcommand\csname my#1\endcsname[2]{%
\if\relax\detokenize{##1}\relax
\csname #1\endcsname ##2
\immediate\write\examfile{\string\csname \space #1\string\endcsname\relax \unexpanded{##2}}%
\else
 \csname #1\endcsname[##1] ##2
\immediate\write\examfile{\string\csname \space #1\string\endcsname[##1] \unexpanded{##2}}%
\fi
}
}

\newcommand\create@titleditem[1]{
\expandafter\newcommand\csname my#1\endcsname[3]{%
\if\relax\detokenize{##2}\relax
\csname #1\endcsname{##1} ##3
\immediate\write\examfile{\string\csname \space #1\string\endcsname\unexpanded{\begingroup ##1\endgroup}\relax \unexpanded{##3}}%
\else
 \csname #1\endcsname{##1}[##2] ##3
\immediate\write\examfile{\string\csname \space #1\string\endcsname\unexpanded{##1}[##2] \unexpanded{##3}}%
\fi
}
}

\create@environment{questions}
\create@environment{parts}
\create@environment{subparts}
\create@environment{subsubparts}
\create@item{question}
\create@item{part}
\create@item{subpart}
\create@item{subsubpart}
\create@titleditem{titledquestion}
\newcommand\insertClearPage{\immediate\write\examfile{\string\clearpage}}

Here's a MWE showing how to use it. It will create another file and on a second pass, copy the questions on the top.

\documentclass{examExt}
\usepackage{amsmath, amssymb}

\begin{document}

 \insertQuestions
 \clearpage
 \begin{myquestions}
  \myquestion{2}{This is a question $3\in\mathbb{R}$}


  \begin{solutionbox}{3in}
   This is a solution
  \end{solutionbox}

  \begin{myparts}
   \mypart{2}{This is a part}


   \begin{solutionbox}{3in}
   This is a solution
  \end{solutionbox}
  \end{myparts}
 \end{myquestions}


\end{document}

enter image description here

Tags:

Exam