[Tex/LaTex] How to HIDE and SHOW answer

exam

I want to make a handout, which contains a lot of questions.

I want a output of this:

enter image description here

Another output is like this:

enter image description here

The 3rd output is like this:

  1. Calculate 1+2

  2. Calculate 1+2+3

  3. Calculate 1+2+3+4

  4. Calculate 1+2+3+4+5

  5. Calculate 1+2+3+4+5+6

The problem here is just my example, I have thousands of middle school examination questions. Who can teach me to use only one TEX file output of three kinds of PDF files, thank you very much!

Best Answer

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

Related Question