如何排版子問題及對應答案

如何排版子問題及對應答案

我想用子問題排版練習,以​​便在程式碼中每個子問題後立即有一個答案,但在輸出中所有答案都將在最後。我還希望練習和子問題都能自動編號。我知道有幾個用於排版練習的套件(例如“answers”、“exercise”和“probsoln”)。我還知道有一種稱為“考試”的文檔類型。

我的問題是:哪個包最適合這個以及什麼樣的程式碼會產生結果?

我想寫如下內容:

\begin{ExerciseList}
  \Exercise{}
    Calculate the following:
    \Question{$7+2$}
      \Answer{$9$}
    \Question{$9-9$}
      \Answer{$0$}
    \Question{$5+5+5$}
      \Answer{$15$}
  \Exercise{}
    Solve the following equations:
    \Question{$x+5=7$}
      \Answer{$x=2$}
    \Question{$x-5=9$}
      \Answer{$x=14$}
    \Question{$5x=20$}
      \Answer{$x=4$}
\end{ExerciseList}

我希望最終的文件看起來像這樣:

練習1
計算以下內容:
a) 7+2 b) 9-9 c) 5+5+5

練習2
解下列方程式:
a) x+5=7 b) x-5=9 c) 5x=20

解決方案:

練習1
a) 9 b) 0 c) 15

練習2
a) x+5=7 b) x=14 c) x=4

我上面的程式碼只是一個例子。只要結構相似,命令不必與我寫的完全相同。

答案1

使用該probsoln套件,可以在文件中定義問題,也可以在可以使用 或 等命令載入的外部文件中定義\loadallproblems問題\loadrandomproblems

這是一個簡單的範例,其中包含文件中定義的問題:

\documentclass{article}

\usepackage{probsoln}

\begin{defproblem}{prob1}% label
  \begin{onlyproblem}% question
    Calculate the following:%

    \begin{textenum}
    \item $7+2$
    \item $9-9$
    \item $5+5+5$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $9$
    \item $0$
    \item $15$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{defproblem}{prob2}% label
  \begin{onlyproblem}% question
    Solve the following equations:

    \begin{textenum}
    \item $x+5=7$
    \item $x-5=9$
    \item $5x=20$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $x=2$
    \item $x=14$
    \item $x=4$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{document}
\renewcommand{\labelenumii}{\theenumii)}

\begin{enumerate}
\foreachproblem{\item\thisproblem}
\end{enumerate}

\section*{Solutions}

\showanswers
\begin{enumerate}
\foreachsolution{\item\thisproblem}
\end{enumerate}

\end{document}

這會產生

顯示問題和解決方案的文件影像

格式可以更改。例如,使用enumitem包裹:

\documentclass{article}

\usepackage{probsoln}
\usepackage{enumitem}

\begin{defproblem}{prob1}% label
  \begin{onlyproblem}% question
    Calculate the following:%

    \begin{textenum}
    \item $7+2$
    \item $9-9$
    \item $5+5+5$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $9$
    \item $0$
    \item $15$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\begin{defproblem}{prob2}% label
  \begin{onlyproblem}% question
    Solve the following equations:

    \begin{textenum}
    \item $x+5=7$
    \item $x-5=9$
    \item $5x=20$
    \end{textenum}
  \end{onlyproblem}%
  \begin{onlysolution}% solution
    \begin{textenum}
    \item $x=2$
    \item $x=14$
    \item $x=4$
    \end{textenum}
  \end{onlysolution}
\end{defproblem}

\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
   ref={\arabic*},wide]
  \renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}

\newcommand{\exercise}{\item\mbox{}\par}

\begin{document}

\begin{ExerciseList}
\foreachproblem{\exercise\thisproblem}
\end{ExerciseList}

\section*{Solutions}

\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}

\end{document}

這會產生:

生成的文檔的圖像

編輯:

這是另一種選擇,您可以將答案寫在程式碼中的問題旁邊:

\documentclass{article}

\usepackage{probsoln}
\usepackage{enumitem}

\newcommand{\QA}[2]{%
  \begin{onlyproblem}#1\end{onlyproblem}%
  \begin{onlysolution}#2\end{onlysolution}}

\begin{defproblem}{prob1}% label
  \begin{onlyproblem}% question
    Calculate the following:%

  \end{onlyproblem}%
  \begin{textenum}
    \item \QA{$7+2$}{$9$}
    \item \QA{$9-9$}{$0$}
    \item \QA{$5+5+5$}{$15$}
  \end{textenum}
\end{defproblem}

\begin{defproblem}{prob2}% label
  \begin{onlyproblem}% question
    Solve the following equations:

  \end{onlyproblem}%
  \begin{textenum}
    \item \QA{$x+5=7$}{$x=2$}
    \item \QA{$x-5=9$}{$x=14$}
    \item \QA{$5x=20$}{$x=4$}
  \end{textenum}
\end{defproblem}

\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
   ref={\arabic*},wide]
  \renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}

\newcommand{\exercise}{\item\mbox{}\par}

\begin{document}

\begin{ExerciseList}
\foreachproblem{\exercise\thisproblem}
\end{ExerciseList}

\section*{Solutions}

\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}

\end{document}

結果與前面的範例相同。

編輯2:

這是定義和顯示問題的方法。標籤是自動產生的:

\documentclass{article}

\usepackage{probsoln}
\usepackage{enumitem}

\newcommand{\QA}[2]{%
  \begin{onlyproblem}#1\end{onlyproblem}%
  \begin{onlysolution}#2\end{onlysolution}}

\newenvironment{ExerciseList}
{\begin{enumerate}[label={\emph{Exercise \arabic*}},%
   ref={\arabic*},wide]
  \renewcommand{\labelenumii}{\theenumii)}%
}
{\end{enumerate}}

\newcommand{\exercise}{\item\mbox{}\par}

\newcommand{\Exercise}[2]{\exercise 
 #1\par
 \begin{defproblem}{prob\arabic{enumi}}%
  \begin{textenum}%
  #2%
  \end{textenum}%
 \end{defproblem}%
 \useproblem{prob\arabic{enumi}}%
}

\begin{document}

\begin{ExerciseList}
 \Exercise{Calculate the following:}%
 {%
    \item \QA{$7+2$}{$9$}
    \item \QA{$9-9$}{$0$}
    \item \QA{$5+5+5$}{$15$}
 }%

 \Exercise{Solve the following equations:}
 {%
    \item \QA{$x+5=7$}{$x=2$}
    \item \QA{$x-5=9$}{$x=14$}
    \item \QA{$5x=20$}{$x=4$}
 }
\end{ExerciseList}

\section*{Solutions}

\showanswers
\begin{ExerciseList}
\foreachsolution{\exercise\thisproblem}
\end{ExerciseList}

\end{document}

答案2

這是使用該answers套件的解決方案。您可以修改答案的格式。

\documentclass[pdftex,12pt]{article}

\usepackage{amsmath}
\usepackage{answers}

\newcommand{\answerFileName}{anexamanswers}
\Newassociation{sol}{Solution}{\answerFileName}
\Opensolutionfile{\answerFileName}

\newenvironment{Exercise}[1]
{\item #1
\begin{enumerate}
}
{
\end{enumerate}
}

\newcommand{\Question}[1]{%
\item #1
}


\begin{document}
\begin{enumerate}

\begin{Exercise}{Calculate the following:}
    \Question{$7+2$}
     \begin{sol}
         $9$
     \end{sol}
    \Question{$9-9$}
      \begin{sol}
          $0$
      \end{sol}
\end{Exercise}

\begin{Exercise}{Solve the following equations:}
    \Question{$x+5=7$}
      \begin{sol}
        $x=2$
     \end{sol}
    \Question{$x-5=9$}
%      \Answer{$x=14$}
    \Question{$5x=20$}
%      \Answer{$x=4$}
\end{Exercise}

\end{enumerate}

\Closesolutionfile{\answerFileName}
\newpage
Answers to Exercises:

\input{\answerFileName}
\end{document}

在此輸入影像描述

答案3

exsheets以下是使用和包的建議tasks

\documentclass{article}
\usepackage{exsheets,tasks}
\SetupExSheets{
  solution/name=Exercise ,
  headings-format=\itshape
}
\begin{document}

\begin{question}
  Calculate the following:
  \begin{tasks}(3)
    \task $7+2$
    \task $9-9$
    \task $5+5+5$
  \end{tasks}
\end{question}
\begin{solution}
  \begin{tasks}(3)
    \task $9$
    \task $0$
    \task $15$
  \end{tasks}
\end{solution}

\begin{question}
  Solve the following equations:
  \begin{tasks}(3)
    \task $x+5=7$
    \task $x-5=9$
    \task $5x=20$
  \end{tasks}
\end{question}
\begin{solution}
  \begin{tasks}(3)
    \task $x=2$
    \task $x=14$
    \task $x=4$
  \end{tasks}
\end{solution}

\section*{Solutions}
\printsolutions

\end{document}

在此輸入影像描述

相關內容