如何隱藏和顯示答案?

如何隱藏和顯示答案?

我想做一份講義,裡面有很多問題。

我想要這樣的輸出:

在此輸入影像描述

另一個輸出是這樣的:

在此輸入影像描述

第三個輸出是這樣的:

  1. 計算1+2

  2. 計算1+2+3

  3. 計算1+2+3+4

  4. 計算1+2+3+4+5

  5. 計算1+2+3+4+5+6

這裡的問題只是我的例子,我有數千道中學考試題。誰能教我只用一種TEX文件輸出三種PDF文件,非常感謝!

答案1

除了艾瑞克所說的之外,我發現對於許多課程,我想要所有問題的文本,每個問題後面都留有空白。我已經擴展了考試課程以自動執行此操作,只需輸入一次問題並通過編譯器幾次即可。

%\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}}

這是一個 MWE,展示瞭如何使用它。它將創建另一個文件,並在第二遍時複製頂部的問題。

\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}

在此輸入影像描述

答案2

我在這裡介紹是\Calculate{}為了提供問題,也是\Solution{}為了提供答案。請注意,\Solution會自動將「問題」重述為第一行,並且可選參數 會[]抑制通常後面的換行符號(如第 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}

在此輸入影像描述

若要消除解決方案,只需取消註解行即可重新定義\Solution

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

在此輸入影像描述

要得到解決方案,只需取消註解另一行即可重新定義\Solution

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

在此輸入影像描述


備用

如果您不想=在解決方案的每一行開頭鍵入 ,則可以實現這一點,但只能透過放棄在問題重述後抑制換行的可選參數功能(比較第 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}

在此輸入影像描述

答案3

您可以使用該類exam,這也可以讓您為解決方案留出空間。它還允許您為問題或部分問題分配分數(我在此處省略)。

\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}

在此輸入影像描述

相關內容