仅使用 tcolorbox 包解决偶数练习

仅使用 tcolorbox 包解决偶数练习

tcolorbox为教科书制作了练习,我想为lowerbox=invisible偶数练习提供带有选项的解决方案,并在教科书的末尾提供奇数练习的解决方案。最终,在我正在撰写的关于这门课程的书的某个项目中提供所有解决方案。我举了一个例子,我lowerbox=invisible在偶数练习中放置了“本地”选项,以观察读者我对练习所追求的行为,我想在第一次(在章节内)仅为偶数练习编写解决方案,在第二次,在书的末尾创建一个包含所有解决方案的文件。

我给出了练习的例子,在文档中给出tcolorbox

\documentclass{article}

\usepackage[most]{tcolorbox}
\tcbuselibrary{skins,breakable}

\NewTColorBox[auto counter,number within=section]{exercise}{+O{}}{%
enhanced,colframe=green!20!black,colback=yellow!10!white,coltitle=green!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
\shade[inner color=green!80!yellow,outer color=yellow!10!white]
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Exercise~ \thetcbcounter:},
label={exercise@\thetcbcounter},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution@\thetcbcounter}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{\thetcbcounter}{solutions/exercise-\thetcbcounter.tex}},
#1
}
\NewTotalTColorBox{\solution}{mm}{%
enhanced,colframe=red!20!black,colback=yellow!10!white,coltitle=red!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
\shade[inner color=red!50!yellow,outer color=yellow!10!white]
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Solution of Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={solution@#1},
attach title to upper=\par,
}{\input{#2}}
\tcbset{no solution/.style={no recording,after upper=}}

\begin{document}

\section{Exercises}
\tcbstartrecording
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sin((\sin x)^2) \right) '
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
\end{exercise}
\begin{exercise}[lowerbox=invisible]    %%  Local option
Compute the derivative of the following function:
\begin{equation*}
f(x)=(\sin(\sin x))^2
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (\sin(\sin x))^2 \right) '
=2\sin(\sin x) \cos(\sin x) \cos x.
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sqrt{x^3-6x^2+2x}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sqrt{x^3-6x^2+2x} \right) '
= \frac{3x^2-12x+2}{2\sqrt{x^3-6x^2+2x}}.
\end{align*}
\end{exercise}
\begin{exercise}[lowerbox=invisible]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\cos((2x^2+3)^3)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \cos((2x^2+3)^3) \right) '
=-\sin((2x^2+3)^3) 3(2x^2+3)^2 2\cdot 2x\\
&=-12x(2x^2+3)^2\sin((2x^2+3)^3).
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=(x^2+1) \sqrt{x^4+1}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (x^2+1) \sqrt{x^4+1} \right) '
= 2x\sqrt{x^4+1} + \frac{2x^3(x^2+1)}{\sqrt{x^4+1}}.
\end{align*}
\end{exercise}
\tcbstoprecording
\newpage
\section{Solutions}
\tcbinputrecords
\end{document}

并得到pdf文件:

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

答案1

我看到的最简单的方法是使用包装宏,该宏根据开关或数字是否是奇数来\solutionbox决定显示解决方案。\ifshowallsolutions

对于环境也采取同样的做法exercise,即预防lowerbox=ignored和解决invisible

通过对整组解决方案使用不同的标签名称,可以避免多个标签警告。此外,使用\IfRefUndefinedExpandablefrom可以捕获对已删除解决方案的引用refcount

更新

\documentclass{article}

\usepackage{refcount}
\usepackage[most]{tcolorbox}

\newif\ifshowallsolutions

\makeatletter
\newcounter{tcb@cnt@exercise}

\NewTColorBox[use counter=tcb@cnt@exercise,number within=section]{exerciseinternal}{s+O{}}{%
enhanced,colframe=green!20!black,colback=yellow!10!white,coltitle=green!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
\shade[inner color=green!80!yellow,outer color=yellow!10!white]
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Exercise~ \thetcbcounter:},
label={exercise@\thetcbcounter},
attach title to upper=\quad,
after upper={\IfRefUndefinedExpandable{solution@\thetcbcounter}{}{\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution@\thetcbcounter}}}},
IfBooleanTF={#1}{lowerbox=invisible}{lowerbox=ignored},
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solutionbox{\thetcbcounter}{solutions/exercise-\thetcbcounter.tex}{\number\c@tcb@cnt@exercise}},
#2
}


\NewDocumentEnvironment{exercise}{+O{}}{%
  \ifodd\c@tcb@cnt@exercise%
  \exerciseinternal[#1]%
  \else
  \exerciseinternal*[#1]%
  \fi
}{\endexerciseinternal}

\NewDocumentCommand{\solutionbox}{+m+m+m}{%
  \ifshowallsolutions
  \solution{#1}{#2}
  \else
  \ifodd#3%
  \solution{#1}{#2}%
  \fi
  \fi
}


\NewTotalTColorBox{\solution}{mm}{%
enhanced,colframe=red!20!black,colback=yellow!10!white,coltitle=red!40!black,
fonttitle=\bfseries,
underlay={\begin{tcbclipinterior}
\shade[inner color=red!50!yellow,outer color=yellow!10!white]
(interior.north west) circle (2cm);
\draw[help lines,step=5mm,yellow!80!black,shift={(interior.north west)}]
(interior.south west) grid (interior.north east);
\end{tcbclipinterior}},
title={Solution of Exercise~\ref{exercise@#1} on page~\pageref{exercise@#1}:},
phantomlabel={\ifshowallsolutions solutionredisplayed@#1\else solution@#1\fi},
attach title to upper=\par,
}{%
  \input{#2}%
}
\tcbset{no solution/.style={no recording,after upper=}}
\makeatother

\begin{document}

\section{Exercises}
\tcbstartrecording
\begin{exercise}[coltitle=blue]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sin((\sin x)^2)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sin((\sin x)^2) \right) '
=\cos((\sin x)^2) 2\sin x \cos x.
\end{align*}
\end{exercise}
\begin{exercise}    %%  Local option
Compute the derivative of the following function:
\begin{equation*}
f(x)=(\sin(\sin x))^2
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (\sin(\sin x))^2 \right) '
=2\sin(\sin x) \cos(\sin x) \cos x.
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=\sqrt{x^3-6x^2+2x}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \sqrt{x^3-6x^2+2x} \right) '
= \frac{3x^2-12x+2}{2\sqrt{x^3-6x^2+2x}}.
\end{align*}
\end{exercise}
\begin{exercise}[lowerbox=invisible]
Compute the derivative of the following function:
\begin{equation*}
f(x)=\cos((2x^2+3)^3)
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( \cos((2x^2+3)^3) \right) '
=-\sin((2x^2+3)^3) 3(2x^2+3)^2 2\cdot 2x\\
&=-12x(2x^2+3)^2\sin((2x^2+3)^3).
\end{align*}
\end{exercise}
\begin{exercise}
Compute the derivative of the following function:
\begin{equation*}
f(x)=(x^2+1) \sqrt{x^4+1}
\end{equation*}
\tcblower
The derivative is:
\begin{align*}
f ' (x) &= \left( (x^2+1) \sqrt{x^4+1} \right) '
= 2x\sqrt{x^4+1} + \frac{2x^3(x^2+1)}{\sqrt{x^4+1}}.
\end{align*}
\end{exercise}
\tcbstoprecording
\newpage
\section{Selected Solutions}
\tcbinputrecords

\section{All solutions}
\showallsolutionstrue
\tcbinputrecords
\end{document}

在此处输入图片描述

相关内容