自动创建答案的部分

自动创建答案的部分

我的问题与这个,但我认为需要采取稍微不同的方法。

我有一份包含大量练习的文档,我想在某个部分添加答案和提示。

我不想手动执行此操作,因为如果我添加练习,我必须确保答案和提示保持正确的顺序。此外,并非所有练习都有答案和提示,因此答案和提示的编号应完全取决于练习的编号。

例如

1. Some hint for exercice 1
2. Some hint for exercice 2
4. Some hint for exercice 4
99. Some hint for exercice 99

应该是可能的。

例如:

\documentclass[12pt,a4paper]{article}
\usepackage[dutch]{babel}
\usepackage{amsthm}

\newtheorem{ex}{Exercice}
\newtheorem{ans}{} % Abbreviation for answer. No text needed because I just want numbers with the answers next to them.
\newtheorem{hi}{} % Same here.

\begin{document}

\section{Algebra}
\begin{ex}What is $1+3$?\end{ex}
\begin{hi}First consider $1+0$, $1+1$, $1+2$ and try to find a pattern.\end{hi}
\begin{ans}It's $4$!\end{ans}

\section{Grammar}
\setcounter{ex}{0} % So the counter is set to zero when a new section begins.

\begin{ex}The first letter of the alphabet?\end{ex}
%No hint given here
\begin{ans}A\end{ans}

\section*{Answers}
\subsection{Section Algebra}
%Answer should be here, but of course it isn't.

\subsection{Section Grammar}
%Same.

\section*{Hints}
\subsection{Section Algebra}
%Hint here.

\subsection{Section Grammar}
% No hint because I didn't ask Latex to do so.

\end{document}

所以实际上我需要一些\magicCommandThatPutsAnswersAndHintsSomewhereElse。如果一个部分中有多个答案,我希望它们像定理一样被编号。

正如您可能在示例中注意到的那样,我想要在引用其部分的小节中找到答案和提示。

答案1

我和你面临同样的问题。我创建了两个功能:我可以打印答案(对于学生)或解决方案(对于我;在这种情况下,答案会消失)。

我将在这里向您展示我的部分代码(它太大了,相信我,不值得在这里全部粘贴)。那么,让我们开始吧...

\problem我创建了陈述问题和存储每个问题答案的命令\answer,以及solution存储问题解决方案的环境(使用参数,我可以设置列数来排列解决方案以使其更紧凑):

%-> Defining COUNTERS:
\newcounter{problem}

%-> Defining the numbered environment for QUESTIONS, TESTS, EXERCISES, PROBLEMS, and CHALLENGES:
\newcommand{\problem}{%
    \par\bigskip
    \refstepcounter{problem}
    \noindent\textbf{\theproblem}
    }

%-> Defining the environment for the ANSWERS to QUESTIONS
\newcommand{\answer}[1]{%
    \let\thefootnote\relax %... suppresses the footnote numbering
    \footnote{\textbf{\theproblem-} #1}%
}

\newif\ifcols

%-> Defining the environment for the SOLUTIONS:
\newenvironment{solution}[1]{%
        \setlength{\parindent}{0cm}
        \vspace{-2.5mm}
        \textbf{\footnotesize Solution:}\par
        \vspace{-3mm}
        \footnotesize
        \ifnum #1 > 1
            \colstrue
            \begin{multicols}{#1}
            \fi
    }{%
    \ifcols
        \end{multicols}
        \fi
    \colsfalse
    }

接下来,我创建了一个布尔值\ifshowans\showanstrue每当我想隐藏解决方案并显示答案时,都可以将其注释掉(以防我想为学生打印材料)。否则,它将打印解决方案并隐藏答案:

%-> Defining the MATERIAL TYPE:
\newif\ifshowans
\showanstrue
    \ifshowans
        \renewcommand\answer[1]{}
    \else
        \let\solution\comment
        \let\endsolution\endcomment
    \fi

到这里,您可以发现,就我而言,我更喜欢在脚注中显示解决方案。由于您想将它们显示在文档末尾,因此您应该将命令修改\answer为如下内容:

%-> Defining the environment for the ANSWERS to QUESTIONS
\newcommand{\answer}[1]{%
    \let\theendnote\relax %... suppresses the endnote numbering
    \endnote{\textbf{\ptype\ \mnumber.\theproblem-} #1}%
}

我希望这可以帮助你。

编辑:

这是一个使用示例(我的学生是巴西人,所以问题是用葡萄牙语陈述的......很抱歉):

\problem
O dia 21 de dezembro marca o solstício de verão no hemisfério sul. Nesse dia do ano, as sombras das pessoas em São Paulo (SP) têm comprimento nulo. Em uma viagem a Brasília nesse dia, a sombra projetada no chão de uma pessoa em pé ao meio-dia local tem um comprimento de 23,7 cm. Sabendo que a altura da pessoa é de 1,73 m e que uma volta à Terra tem comprimento $C = 40\ \v{mil\ km}$, calcule a distância entre São Paulo e Brasília.
\answer{870 km}

%---------- SOLUTION ----------%
\begin{solution}{2}
Do triângulo formado pela pessoa, sua sombra e o raio de luz do Sol, tem-se
$$
    \tan\theta = \frac{23,7}{173} = 0,137
    \ \ \ \Rightarrow \ \ \ 
    \theta = 7,80\deg
$$

Por regra de três simples,
$$
    \begin{array}{rcl}
        360\deg     & \rightarrow & 40 000\ \v{km}
        \\
        7,80\deg    & \rightarrow & d
    \end{array}
    \ \ \ \Rightarrow \ \ \ 
    d = 870\ \v{km}
$$
\end{solution}

编辑 2:我刚刚删除了一些未定义(且无意义)的命令

答案2

这是我所做的最简化版本。为了简洁起见,它不包括以下附加内容:解决方案部分的页码、指向练习的链接、练习旁边是否有书面解决方案的指示等。

在下面的代码中,我假设有一个计数器exercise,在您的情况下可能是theorem。(请参阅底部的 MWE。)

我使用该包environ来定义一个我可以访问solution的环境\BODY。它的主体逐字写入文件solutions.txt,该文件稍后将包含在文档中。请注意每个反斜杠都必须转义。(来源

\usepackage{environ}
\newwrite\SolutionWriter
\makeatletter
\NewEnviron{solution}{%
   \immediate\write\SolutionWriter{%
      \@backslashchar par\@backslashchar addvspace\@backslashchar topsep%
      \@backslashchar noindent%
      \@backslashchar textbf{Solution \theexercise}.{ }%
      \detokenize\expandafter{\BODY}%
      \@backslashchar par\@backslashchar addvspace\@backslashchar topsep%
      }%
}
\makeatother

然后放在\immediate\openout\SolutionWriter=solutions.txt后面\begin{document}。你现在可以做类似的事情

\begin{exercise}What is $1+3$?\end{exercise}
\begin{solution}It's $4$!\end{solution}

只要你写

\immediate\closeout\SolutionWriter
\section{Solutions}
\input{solutions.txt}

在文档的后面部分。(当没有更多练习时)。

要在解决方案部分中添加部分标题,可以使用etoolboxapptocmd但一旦您开始自定义部分,使用 可能是一个好主意titlesec。由于可能存在您不想在解决方案部分中出现的部分,因此我创建了一个\ifexercises,您可以将特定部分设置为 false。(请参阅这里以近似于默认部分格式。

\newif\ifexercises
\exercisestrue
\usepackage[explicit]{titlesec}
\makeatletter
\titleformat{\section}
   {\normalfont\Large\bfseries}
   {\thesection}
   {1em}
   {#1}
   [\ifnum\value{section}=0\else% exclude contents
   \ifexercises\immediate\write\SolutionWriter{%
   \@backslashchar subsection*{\thesection\@backslashchar quad #1}}%
   \fi\fi%
   \global\exercisestrue]
\makeatother

然后将其放在\exercisesfalse您不想出现在解决方案之间的部分之前。

平均能量损失

\documentclass{article}

\usepackage{environ}
\newwrite\SolutionWriter
\makeatletter
\NewEnviron{solution}{%
   \immediate\write\SolutionWriter{%
      \@backslashchar par\@backslashchar addvspace\@backslashchar topsep%
      \@backslashchar noindent%
      \@backslashchar textbf{Solution \theexercise}.{ }%
      \detokenize\expandafter{\BODY}%
      \@backslashchar par\@backslashchar addvspace\@backslashchar topsep%
      }%
}

\newif\ifexercises
\exercisestrue
\usepackage[explicit]{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1}
  [\ifnum\value{section}=0\else\ifexercises\immediate\write\SolutionWriter{\@backslashchar subsection*{\thesection\@backslashchar quad #1}}\fi\fi\global\exercisestrue]
\makeatother

\usepackage{amsthm}
\newtheorem{exercise}{Exercise}

\begin{document}
\immediate\openout\SolutionWriter=solutions.txt

\section{Algebra}

\begin{exercise}What is $1+3$?\end{exercise}
\begin{solution}It's $4$.\end{solution}

\section{Grammar}

\begin{exercise}What is the first letter of the alphabet?\end{exercise}
\begin{solution}A and this is a solution which is made very long on purpose so that it spans multiple lines.\end{solution}

\begin{exercise}What is the second letter of the alphabet?\end{exercise}
\begin{solution}B\end{solution}

\exercisesfalse %alternatively, close the writer before the solutions section
\section{Solutions}
\immediate\closeout\SolutionWriter
\input{solutions.txt}

\end{document}

解决方案周围的间距并不完美。我尝试模仿amsthm包装中的间距。

相关内容