数学书:如何写练习和答案

数学书:如何写练习和答案

编辑\documentclass[12pt]{book}我想要创建分章节的练习并将所有的解决方案(带或不带提示)放在书的末尾。

我想以简单和非繁琐的方式来做到这一点:对于问题的输入,我只想\question为每个问题添加类似的答案,但所有答案都应该出现在书的末尾。

我需要的练习编号为 1.1、1.2、...、2.1、2.2、...。

问题编号为 1、2、3、...

答案应包括所附 jpg 文件中给出的练习页码。

锻炼方式:

在此处输入图片描述

解决方案类型:

在此处输入图片描述

MWE 如下(我也不知道如何在第 2 章中写解决方案,因为问题写在了中\begin{minipage} \end{minipage}。假设所有解决方案都是:这是一个解决方案 Ex不。

    \documentclass[12pt,openany,twoside]{book} 
\usepackage{fouriernc}
\usepackage[thmmarks,standard,thref]{ntheorem}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{answers}

\theoremseparator{.}
\theorembodyfont{\upshape}
\newtheorem{pro}{Problem}[chapter]

\Newassociation{loigiai}{Answer}{loigiaichung}


\begin{document}
\chapter{Firist }
\thispagestyle{empty}
\Opensolutionfile{loigiaichung}[pro]
In problem 1-3, determine whether the given differential equation is separable 
\begin{pro}
$\frac{dy}{dx}-\sin{(x+y)}=0$
\begin{loigiai}
This is a solution of Ex 1
\end{loigiai}
\end{pro}

\begin{pro}
$\frac{dy}{dx}=4y^2-3y+1$
\begin{loigiai}
This is a solution of Ex 2
\end{loigiai}
\end{pro}


\begin{pro}
$\frac{ds}{dt}=t\ln{(s^{2t})}+8t^2$
\begin{loigiai}
This is a solution Ex 3
\end{loigiai}
\end{pro}

In problem 4-6, solve the equation 
\begin{pro}
$\frac{dx}{dt}=3xt^2$
\begin{loigiai}
This is a solution Ex 4
\end{loigiai}
\end{pro}

\begin{pro}
$y^{-1}dy+ye^{\cos{x}}\sin{x}dx=0$
\begin{loigiai}
This is a solution Ex 5
\end{loigiai}
\end{pro}


\begin{pro}
$(x+xy^2)dx+ye^{\cos{x}}\sin{x}dx=0$
\begin{loigiai}
This is a solution Ex 5
\end{loigiai}
\end{pro}

\begin{pro}
$\frac{dy}{dt} = \frac{y}{t+1} + 4t^2 +  4t$, $\quad$ $y(1) = 10$
\begin{loigiai}
This is a solution Ex 6
\end{loigiai}
\end{pro}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{Second }
\thispagestyle{empty}
\begin{pro}
Eight systems of differential equations and five direction fields are given below.  Determine the system that corresponds to each direction field and sketch the solution curves that correspond to the initial conditions $(x_0, y_0) = (0,1)$ and $(x_0, y_0) = (1,-1)$.
\begin{minipage}{.25\textwidth}
$(i)$\begin{align*}
\frac{dx}{dt} & = -x \\
\frac{dy}{dt} & = y-1\end{align*}
\end{minipage}
\begin{minipage}{.25\textwidth}
$(ii)$\begin{align*}
\frac{dx}{dt} & = x^2 - 1 \\
\frac{dy}{dt} & = y\end{align*}
\end{minipage}
\begin{minipage}{.25\textwidth}
$(iii)$\begin{align*}
\frac{dx}{dt} & = x+2y \\
\frac{dy}{dt} & = -y\end{align*}
\end{minipage}
\begin{minipage}{.25\textwidth}
$(iv)$\begin{align*}
\frac{dx}{dt} & = 2x \\
\frac{dy}{dt} & =  y\end{align*}
\end{minipage}

\begin{minipage}{.25\textwidth}
$(v)$\begin{align*}
\frac{dx}{dt} & = x \\
\frac{dy}{dt}  & = 2y\end{align*}
\end{minipage}
\begin{minipage}{.25\textwidth}
$(vi)$\begin{align*}
\frac{dx}{dt} & = x-1 \\
\frac{dy}{dt} & = -y\end{align*}
\end{minipage}
\begin{minipage}{.25\textwidth}
$(vii)$\begin{align*}
\frac{dx}{dt} & = x^2-1 \\
\frac{dy}{dt} & = -y\end{align*}
\end{minipage}
\begin{minipage}{.25\textwidth}
$(viii)$\begin{align*}
\frac{dx}{dt} & = x- 2y \\
\frac{dy}{dt} & =  -y\end{align*}
\end{minipage}
\end{pro}
\chapter{Answer to all problems}
\thispagestyle{empty}
\Closesolutionfile{loigiaichung}
\section{This is solution}
\input{pro}

\end{document}

答案1

这是一个带有exercise包的解决方案。

它的选项answerdelayed允许您使用 在您想要的位置打印答案\shipoutAnswer

lastexercise意思是如果你没有放任何内容作为答案参考,Answer环境将参考上一个练习。但是,我也放了\begin{Answer}[ref={...}],这样你可以把答案的代码移动到你想要的地方,但你必须保持顺序。至于我如何为章节的第一个答案创建代码(打印CHAPTER ...在最终列表中),你也必须在相关章节中编写它。

此外,为了CHAPTER ...在最终的答案列表中打印,您必须\setboolean{firstanswerofthechapter}{true}在章节的第一个答案的开头和\setboolean{firstanswerofthechapter}{false}结尾进行设置(也许有更聪明的方法来做到这一点,但这超出了我的知识水平)。

最后,如果您需要水平列表,则必须使用tasks环境,否则,普通的\Question就足够了,我在我的 MWE 中放了两者的示例。

编辑:我根据 OP 在评论中的要求更改了编号。

如果您希望罗马数字从 i 开头,则在第二个问题中,只需删除该[resume=true]选项。

第二次编辑:为了使第二个练习的列表正确对齐,您必须使用aligned而不是align*。由于这些是系统,因此最好在前面放一个花括号,就像这里的例子 2.2 中一样:

第三次编辑:在原始帖子中,我将其counter-format={tsk[r].}作为软件包的一个选项tasks。但是,自 2019 年 10 月起,该选项已弃用。因此,当前版本已label=\roman*.
感谢 EthanAlvaree 指出这一点。

\documentclass{book}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{multicol}

\usepackage{ifthen}
\newboolean{firstanswerofthechapter}  

\usepackage{xcolor}
\colorlet{lightcyan}{cyan!40!white}

\usepackage{chngcntr}
\usepackage{stackengine}

\usepackage{tasks}
\newlength{\longestlabel}
\settowidth{\longestlabel}{\bfseries viii.}
\settasks{label=\roman*., label-format={\bfseries}, label-width=\longestlabel,
item-indent=0pt, label-offset=2pt, column-sep={10pt}}

\usepackage[lastexercise,answerdelayed]{exercise}
\counterwithin{Exercise}{chapter}
\counterwithin{Answer}{chapter}
\renewcounter{Exercise}[chapter]
\newcommand{\QuestionNB}{\bfseries\arabic{Question}.\ }
\renewcommand{\ExerciseName}{EXERCISES}
\renewcommand{\ExerciseHeader}{\noindent\def\stackalignment{l}% code from https://tex.stackexchange.com/a/195118/101651
    \stackunder[0pt]{\colorbox{cyan}{\textcolor{white}{\textbf{\LARGE\ExerciseHeaderNB\;\large\ExerciseName}}}}{\textcolor{lightcyan}{\rule{\linewidth}{2pt}}}\medskip}
\renewcommand{\AnswerName}{Exercises}
\renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}}%
    {\bigskip\noindent\textcolor{cyan}{\textbf{CHAPTER \thechapter}}\newline\newline%
        \noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page %
                \pageref{\AnswerRef}}}\smallskip}
    {\noindent\bfseries\emph{\textcolor{cyan}{\AnswerName\ \ExerciseHeaderNB, page \pageref{\AnswerRef}}}\smallskip}}
\setlength{\QuestionIndent}{16pt}

\begin{document}
    \chapter{First}
    
    \begin{Exercise}\label{EX11}
        \vspace{-\baselineskip}% <-- You don't need this line of code if there's some text here
        \Question In problem \ref{EX11-1-i}-\ref{EX11-1-iii}, determine whether the given differential equation is separable  
        \begin{tasks}(2)
            \task\label{EX11-1-i} $\frac{dy}{dx}-\sin{(x+y)}=0$     
            \task $\frac{dy}{dx}=4y^2-3y+1$ 
            \task\label{EX11-1-iii} $\frac{ds}{dt}=t\ln{(s^{2t})}+8t^2$ 
        \end{tasks}
        \Question In problem \ref{EX11-2-iv}-\ref{EX11-2-viii}, solve the equation 
        \begin{tasks}[resume=true](2)
            \task\label{EX11-2-iv} $\frac{dx}{dt}=3xt^2$
            \task $y^{-1}dy+ye^{\cos{x}}\sin{x}dx=0$
            \task $(x+xy^2)dx+ye^{\cos{x}}\sin{x}dx=0$
            \task\label{EX11-2-viii} $\frac{dy}{dt} = \frac{y}{t+1} + 4t^2 +  4t$, $\quad$ $y(1) = 10$
        \end{tasks}
    \end{Exercise}
    \setboolean{firstanswerofthechapter}{true}
    \begin{multicols}{2}
        \begin{Answer}[ref={EX11}]
            \Question 
            \begin{tasks}
                \task This is a solution of Ex 1
                \task This is a solution of Ex 2 
                \task This is a solution of Ex 3 
            \end{tasks} 
            \Question 
            \begin{tasks}[resume=true]
                \task This is a solution of Ex 4
                \task This is a solution of Ex 5 
                \task This is a solution of Ex 6 
                \task This is a solution of Ex 7 
            \end{tasks} 
        \end{Answer}
    \end{multicols}
    \setboolean{firstanswerofthechapter}{false}
    
    \begin{Exercise}\label{EX12}
        Another exercise. 
        \Question If you don't need a horizontal list, you can simply use \verb|\Question|
    \end{Exercise}
    \begin{multicols}{2}
        \begin{Answer}[ref={EX12}]
            \Question This is a solution of Ex 1
        \end{Answer}
    \end{multicols}
    
    \chapter{Second}
    
    \begin{Exercise}\label{EX21}
        \vspace{-\baselineskip}% <-- You don't need this line of code if there's some text here
        \Question Eight systems of differential equations and five direction fields are given below.  Determine the system that corresponds to each direction field and sketch the solution curves that correspond to the initial conditions $(x_0, y_0) = (0,1)$ and $(x_0, y_0) = (1,-1)$.
        \begin{tasks}(3)
            \task $\begin{aligned}
            \frac{dx}{dt} & = -x \\     
            \frac{dy}{dt} & = y-1
            \end{aligned}$
            \task $\begin{aligned}
            \frac{dx}{dt} & = x^2 - 1 \\        
            \frac{dy}{dt} & = y
            \end{aligned}$
            \task $\begin{aligned}
            \frac{dx}{dt} & = x+2y \\
            \frac{dy}{dt} & = -y
            \end{aligned}$
            \task $\begin{aligned}
            \frac{dx}{dt} & = 2x \\
            \frac{dy}{dt} & =  y
            \end{aligned}$
            \task $\begin{aligned}
            \frac{dx}{dt} & = x \\
            \frac{dy}{dt}  & = 2y
            \end{aligned}$ 
            \task$\begin{aligned}
            \frac{dx}{dt} & = x-1 \\
            \frac{dy}{dt} & = -y
            \end{aligned}$
            \task$\begin{aligned}
            \frac{dx}{dt} & = x^2-1 \\
            \frac{dy}{dt} & = -y
            \end{aligned}$        
            \task $\begin{aligned}
            \frac{dx}{dt} & = x- 2y \\
            \frac{dy}{dt} & =  -y
            \end{aligned}$
        \end{tasks}
    \end{Exercise}
    \setboolean{firstanswerofthechapter}{true}
    \begin{multicols}{2}
        \begin{Answer}[ref={EX21}]
            \Question 
            \begin{tasks}
                \task This is a solution of Ex 1
                \task This is a solution of Ex 2 
                \task This is a solution of Ex 3 
                \task This is a solution of Ex 4 
                \task This is a solution of Ex 5 
                \task This is a solution of Ex 6 
                \task This is a solution of Ex 7 
                \task This is a solution of Ex 8 
            \end{tasks}
        \end{Answer}
    \end{multicols}
    \setboolean{firstanswerofthechapter}{false}
    \newpage        
    \begin{Exercise}\label{EX22}
        Since these are systems, maybe it's better to put the \verb|aligned| enviroment within  \verb|\left\{| and \verb|\right.|: 
        \Question Eight systems of differential equations and five direction fields are given below.  Determine the system that corresponds to each direction field and sketch the solution curves that correspond to the initial conditions $(x_0, y_0) = (0,1)$ and $(x_0, y_0) = (1,-1)$.
        \begin{tasks}(3)
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = -x \\     
            \frac{dy}{dt} & = y-1
            \end{aligned}\right.$
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = x^2 - 1 \\        
            \frac{dy}{dt} & = y
            \end{aligned}\right.$
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = x+2y \\
            \frac{dy}{dt} & = -y
            \end{aligned}\right.$
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = 2x \\
            \frac{dy}{dt} & =  y
            \end{aligned}\right.$
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = x \\
            \frac{dy}{dt}  & = 2y
            \end{aligned}\right.$ 
            \task$\left\{\begin{aligned}
            \frac{dx}{dt} & = x-1 \\
            \frac{dy}{dt} & = -y
            \end{aligned}\right.$
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = x^2-1 \\
            \frac{dy}{dt} & = -y
            \end{aligned}\right.$        
            \task $\left\{\begin{aligned}
            \frac{dx}{dt} & = x- 2y \\
            \frac{dy}{dt} & =  -y
            \end{aligned}\right.$
        \end{tasks}
    \end{Exercise}
    \begin{multicols}{2}
        \begin{Answer}[ref={EX22}]
            \Question 
            \begin{tasks}
                \task This is a solution of Ex 1
                \task This is a solution of Ex 2 
                \task This is a solution of Ex 3 
                \task This is a solution of Ex 4 
                \task This is a solution of Ex 5 
                \task This is a solution of Ex 6 
                \task This is a solution of Ex 7 
                \task This is a solution of Ex 8 
            \end{tasks}
        \end{Answer}
    \end{multicols}
    
    \chapter{Answer to all problems}
    
    \begin{multicols}{2}\raggedcolumns
        \shipoutAnswer
    \end{multicols}
    
\end{document}

在此处输入图片描述

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

答案2

  • 下列的这个答案,我使用该newfile包将解决方案写入单独的文件,然后将其读入。导入行如下:

    \newoutputstream{solutions}
    \openoutputfile{\jobname.solutions}{solutions}
    \addtostream{solutions}{...}
    \closeoutputstream{solutions}
    \input{\jobname.solutions}
    
  • 我定义了两个计数器:exblockexquestionexblock计数“练习”部分,而exquestion计数练习中的问题。 通过这些,我可以为每个问题赋予唯一的标签。

  • 练习部分以\exheader命令开始。此宏包含对 ! 的调用。\section之后\exheader,您可以exercises使用\question宏在环境中输入问题。它需要两个参数:问题和解决方案。

    \exheader{}
    \begin{exercises}
        \question{Question}{Answer}
    \end{exercises}
    
  • 最后调用宏\printsolutions打印所有章节练习的答案。

完整代码如下:

\documentclass{book}

\usepackage{newfile}
\usepackage{amsmath}
\usepackage{xifthen}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{lipsum}

\setlength{\parindent}{0pt}

\newoutputstream{solutions}
\openoutputfile{\jobname.solutions}{solutions}
\newcounter{exblock}
\newcounter{exquestion}
\newcounter{exsubquestion}
\newlist{questionlist}{enumerate}{1}
\newlist{solutionlist}{enumerate}{1}
\newlist{subquestionlist}{enumerate*}{1}
\newlist{subsolutionlist}{enumerate*}{1}
\setlist[questionlist,solutionlist]{label=\arabic*.}
\setlist[subquestionlist,subsolutionlist]{label=(\alph*),itemjoin={},before=~}
\newcommand{\exheader}{%
    \ifthenelse{\value{exblock}>0}{\addtostream{solutions}{\noexpand\end{solutionlist}}}{}%
    \refstepcounter{exblock}%
    \setcounter{exquestion}{0}%
    \section{Exercises}%
    \label{ex.\arabic{exblock}}%
    \addtostream{solutions}{\noexpand\paragraph{Exercises \noexpand\ref{ex.\arabic{exblock}}, page \noexpand\pageref{ex.\arabic{exblock}}}}%
    \addtostream{solutions}{\noexpand\begin{solutionlist}}%
}
\newcommand{\printsolutions}{%
    \ifthenelse{\value{exblock}>0}{\addtostream{solutions}{\noexpand\end{solutionlist}}}{}%
    \closeoutputstream{solutions}%
    \input{\jobname.solutions}%
}
\newenvironment{exercises}[1][]{%
    \begin{questionlist}[start=\value{exquestion}+1, #1]%
}{%
    \end{questionlist}%
}
\newenvironment{exercisescol}[2][]{%
    \begin{multicols}{#2}%
    \begin{questionlist}[start=\value{exquestion}+1, #1]%
}{%
    \end{questionlist}%
    \end{multicols}%
}
\newcommand{\question}[2]{%
    \refstepcounter{exquestion}%
    \label{ex.\arabic{exblock}.\arabic{exquestion}}%
    \item #1%
    \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exquestion}}.] #2}%
}
\newenvironment{subquestions}[1][]{%
    \refstepcounter{exquestion}%
    \label{ex.\arabic{exblock}.\arabic{exquestion}}%
    \setcounter{exsubquestion}{0}%
    \addtostream{solutions}{\noexpand\item[\noexpand\ref{ex.\arabic{exblock}.\arabic{exquestion}}.]\noexpand\begin{subsolutionlist}}%
    \item #1%
    \begin{subquestionlist}%
}{%
    \end{subquestionlist}%
    \addtostream{solutions}{\noexpand\end{subsolutionlist}}
}
\newcommand{\subquestion}[2]{%
    \refstepcounter{exsubquestion}%
    \item #1%
    \addtostream{solutions}{\noexpand\item #2}%
}


\setlist*[questionlist]{font=\bfseries}
\setlist*[solutionlist]{noitemsep,font=\bfseries}
\setlist*[subquestionlist]{font=\bfseries}
\setlist*[subsolutionlist]{noitemsep,font=\bfseries,itemjoin={\\},before={}}

\begin{document}

\chapter{Antidifferentiation}

\section{Introduction}
\lipsum[1-4]

\exheader
Find the indicated integral.
\begin{exercisescol}[noitemsep]{2}
    \question{$\int\sqrt{x}dx$}{$\tfrac{2}{3}x\sqrt{x}+C$}
    \question{$\int3e^xdx$}{$3e^x+C$}
    \question{$\int(3x^2-\sqrt{5x}+2)dx$}{$x^3-\tfrac{2}{3}x\sqrt{5x}+2x+C$}
    \question{$\int(\tfrac{1}{2x}-\tfrac{2}{x^2}+\tfrac{3}{\sqrt{x}})dx$}{$\tfrac{1}{2}\ln|x|+\tfrac{2}{x}+6\sqrt{x}+C$}
\end{exercisescol}
Now some more complicated questions:
\begin{exercises}
    \question{Find the function $f$ whose tangent has slope $x^3-\tfrac{2}{x^2}+2$ for each value of $x$ and whose graph passes through the point $(1,3)$.}{$f(x)=\tfrac{1}{4}x^4+\tfrac{2}{x}+2x-\tfrac{5}{4}$}
    \begin{subquestions}[Suppose a car supplies a constant deceleration of $A$ meters per second squared. If it is traveling at 90 kilometers per hour when the brakes are applied, its stopping distance is 50 meters.]
        \subquestion{What is $A$?}{$A=6.25$}
        \subquestion{What would the stopping distance have been if the car had been traveling at only 54 kilometers per hour when the brakes were applied?}{42 meters}
    \end{subquestions}
\end{exercises}

\chapter{Integration by Substitution}

\section{Method}
\lipsum[5-6]

\section{Examples}
\lipsum[7-9]

\exheader
Find the indicated integral.
\begin{exercises}
    \question{$\int(2x+6)^5dx$}{$\tfrac{1}{12}(2x+6)^6+C$}
    \question{$\int xe^{x^2}dx$}{$\tfrac{1}{2}e^{x^2}+C$}
\end{exercises}

\chapter{Appendix}

\section{Solutions}

\printsolutions

\end{document}

在此处输入图片描述

更多功能:

  • exercises即使您打断它来写提示等,环境仍会继续正确地对问题进行编号。
  • 环境exercises可以将任何enumitemnoitemsep您想要传递给enumerate包含问题的环境的选项(如)。要为问题或解决方案设置全局选项,请分别使用\setlist*[questionlist]{...}\setlist*[solutionlist]{...}
  • 您可以将exercises环境替换为exercisescol以数字为参数并调用multicol环境将问题放入列中。
  • 您可以\questionsubquestions环境替换宏,并用宏填充它\subquestion(其工作方式与\question)。subquestions将第一个子问题之前打印的提示作为可选参数(仅在问题中,而不是在解决方案中):

    \begin{subquestions}[Some introductory remarks]
        \subquestion{First part}{First solution}
        \subquestion{Second part}{Second solution}
    \end{subquestions}
    
  • \usepackage{hyperref}如果您想将解决方案的链接包含回问题,请使用。

相关内容