在 LaTeX 文档末尾添加解决方案关键字

在 LaTeX 文档末尾添加解决方案关键字

我写了一些练习,如下图所示。我想在 .tex 文档中写出每个练习的解决方案,并直接在下方写上。然后,我希望这些解决方案显示在文档末尾(见下图)。我希望实现下图的输出。我很难做到这一点。我研究并尝试了许多软件包,但似乎没有一个完全符合我的格式。

在此处输入图片描述

在此处输入图片描述

这是第一张图片的代码。内联注释表明了我想要如何指定每个解决方案。我希望第二张图片(解决方案)能够自动生成。

\documentclass{article}

%%%%%%% Packages %%%%%%%
\usepackage[margin=0.75in]{geometry}

\usepackage{enumitem}
    % Custom bolded enumerate environment.
    \newenvironment{boenumerate}{\begin{enumerate}[label=\textbf{\arabic*.}]}{\end{enumerate}}
    
    % A clearer method of resuming a numbered/lettered list (\seti and \conti).
    \newcounter{saveenumi}
    \newcommand{\seti}{\setcounter{saveenumi}{\value{enumi}}}
    \newcommand{\conti}{\setcounter{enumi}{\value{saveenumi}}}
    
\usepackage{tasks}  % Make horizontal lists.
    \settasks{label={(\alph*)}, 
        label-align=left, 
        label-offset={0mm}, 
        label-width=25pt, 
        item-indent=25pt, 
        label-format={\bfseries}, 
        after-item-skip=2mm, 
        after-skip={3mm}}

\usepackage{amsmath} % Math mode.


\begin{document}
    \subsection*{Exercises}\addcontentsline{toc}{subsection}{Exercises}
    \subsubsection*{The Tangent Line}
    \begin{boenumerate}
        \item Find the slope of the tangent line at the given point.
        \begin{tasks}(2)
            \task $f(x)=2x^3$,\quad $x=\tfrac{1}{3}$
                %\solution{$f^\prime(x)=6x^2 \implies f^\prime(\tfrac{1}{3})=\tfrac{2}{3}$}
            \task $y=(1-2x)(3x-4)$,\quad $x=2$
                %\solution{$-13$}
        \end{tasks}
        
        \item Find the equation of the tangent line at the given point.
        \begin{tasks}(2)
            \task $y=2\sqrt{x}$, $(9,6)$
                %\solution{$y=\tfrac{1}{3}(x-9)+6$}
            \task $xy=1$, $\left(5,\tfrac{1}{5}\right)$
                %\solution{$y=\tfrac{-1}{25}(x-5)+\tfrac{1}{5}$}
        \end{tasks}
        \item At what point on the parabola $y=4x^2$ is the slope of the tangent line equal to 4?
            %\solution{$(\tfrac{1}{2},1)$}
        \item Find the point on the curve $y=x\sqrt{x}$ where the tangent line is parallel to the line $6x-y=4$.
            %\solution{$(16,64)$}
        \seti
    \end{boenumerate}
    
    \subsubsection*{The First Derivative Test}
    \begin{boenumerate}
        \conti
        \item Find the intervals of increase and decrease for the following functions.
        \begin{tasks}(3)
            \task $f(x)=3x^2-18x+1$
                %\solution{Increasing: $(3,\infty)$; Decreasing: $(-\infty,3)$}
            \task $f(x)=2x^3-9x^2-60x+82$
                %\solution{Increasing: $(-\infty,-2)\cup (5,\infty)$; Decreasing: $(-2,5)$}
        \end{tasks}
    \end{boenumerate}
\end{document}

答案1

我想到了一个不错的解决方案。它相当全面,所以我想我会为其他人解答我的问题,他们可能会觉得这很有用。

\documentclass{article}

%%% Packages %%%
\usepackage[margin=0.75in]{geometry} % Set the page margins
\usepackage{lipsum} % pseudo text
\usepackage{scrextend} % To use the \addmargin command
\usepackage{newfile} % Write and read from a tex file from within this file
\usepackage{amsmath} % math mode
\usepackage{xifthen} % conditionals
\usepackage{enumitem} % A better enumerate environment.
    \newenvironment{boenumerate}    % Custom bolded enumerate environment.
    {\begin{enumerate}\renewcommand\labelenumi{\textbf\theenumi.}}
        {\end{enumerate}}
    \newcounter{saveenumi}
    \newcommand{\seti}{\setcounter{saveenumi}{\value{enumi}}}
    \newcommand{\conti}{\setcounter{enumi}{\value{saveenumi}}}
\usepackage{setspace} % Make the solutions 1.5 spaced
\usepackage{tasks}  % Make horizontal lists.
    \settasks{label={(\alph*)}, 
        label-align=left, 
        label-offset={0mm}, 
        label-width=25pt, 
        item-indent=25pt, 
        label-format={\bfseries}, 
        after-item-skip=2mm, 
        after-skip={3mm}}
    
%%% Commands and Settings %%%
% Solutions file
\newoutputstream{solutions}
\openoutputfile{\jobname.solutions}{solutions}

% Setting the subheader: \exheader{name of subheader}
\newcommand{\exheader}[1]{%
    \subsubsection*{#1}%
    \addtostream{solutions}{\textbf{#1}\newline}%
}

% Making a solution command that reports the solution at the end of the document
\newcounter{itemCounter}
\newcommand{\solution}[1]{%
    \ifthenelse{\value{task} = 1}{%
        \addtostream{solutions}{
        \textbf{\theenumi.} \textbf{\thetask} \vspace{-1.75em} \begin{addmargin}[3.5em]{0em} \unexpanded{#1}\end{addmargin}}%
        \setcounter{itemCounter}{\value{enumi}}%
        }{%
        \ifthenelse{\value{task}>1 \and \value{enumi} = \value{itemCounter}}{%
            \addtostream{solutions}{\phantom{\textbf{\theenumi.}} \textbf{\thetask} \vspace{-1.75em} \begin{addmargin}[3.5em]{0em} \unexpanded{#1}\end{addmargin}}%
            \setcounter{itemCounter}{\value{enumi}}%
            }{%
                \addtostream{solutions}{\textbf{\theenumi.} \vspace{-1.75em} \begin{addmargin}[1.5em]{0em} \unexpanded{#1}\end{addmargin}}%
            }%
        }%
    }

% Removing indenting for the whole document
\setlength{\parindent}{0pt}

\begin{document}
    \subsection*{Exercises}\addcontentsline{toc}{subsection}{Exercises}
    \exheader{The Tangent Line}
    \begin{boenumerate}
        \item Find the slope of the tangent line at the given point.
        \begin{tasks}(2)
            \task $f(x)=2x^3$,\quad $x=\tfrac{1}{3}$
                \solution{$f^\prime(x)=6x^2 \implies f^\prime(\tfrac{1}{3})=\tfrac{2}{3}$}
            \task $y=(1-2x)(3x-4)$,\quad $x=2$
                \solution{$-13$}
        \end{tasks}
        
        \item Find the equation of the tangent line at the given point.
        \begin{tasks}(2)
            \task $y=2\sqrt{x}$, $(9,6)$
                \solution{$y=\tfrac{1}{3}(x-9)+6$}
            \task $xy=1$, $\left(5,\tfrac{1}{5}\right)$
                \solution{$y=\tfrac{-1}{25}(x-5)+\tfrac{1}{5}$}
        \end{tasks}
        \item At what point on the parabola $y=4x^2$ is the slope of the tangent line equal to 4?
            \solution{$(\tfrac{1}{2},1)$}
        \item Find the point on the curve $y=x\sqrt{x}$ where the tangent line is parallel to the line $6x-y=4$.
            \solution{$(16,64)$}
        \seti
    \end{boenumerate}
    
    \exheader{The First Derivative Test}
    \begin{boenumerate}
        \conti
        \item Find the intervals of increase and decrease for the following functions.
        \begin{tasks}(3)
            \task $f(x)=3x^2-18x+1$
                \solution{Increasing: $(3,\infty)$; Decreasing: $(-\infty,3)$}
            \task $f(x)=2x^3-9x^2-60x+82$
                \solution{Increasing: $(-\infty,-2)\cup (5,\infty)$; Decreasing: $(-2,5)$}
        \end{tasks}

        \item Question prompt \solution{\lipsum[1]}
        \item Question prompt:
        \begin{tasks}
            \task Some question \solution{\lipsum[2]}
            \task Another question \solution{\lipsum[3]}
        \end{tasks}
    \end{boenumerate}

    \clearpage

    \section*{SOLUTIONS}
    \setstretch{1.5}
    \closeoutputstream{solutions}
    \input{\jobname.solutions}
\end{document}

上述代码生成以下两个页面: 在此处输入图片描述 在此处输入图片描述

以下是该方法的简要说明:

  1. 我们使用该newfile包来使用该命令。其思想是将(枚举的当前项目数)和(当前任务的任务数)\addtostream的当前状态写入不同的输出流,然后在最后显示此输出流。\theenumi\thetask
  2. 命令\solution负责完成这里的所有繁重工作。根据当前任务值,它会写入带或不带当前项目值的解决方案。
  3. 我们利用该scrextend包将\addmargin解决方案缩进为逐项列表。
  4. \unexpanded命令用于确保特殊字符被正确保存。这样就可以包含 TikZ 图像和图形环境。

相关内容