![使用 tcolorbox 制作的练习题答案-编号标题](https://linux22.com/image/350732/%E4%BD%BF%E7%94%A8%20tcolorbox%20%E5%88%B6%E4%BD%9C%E7%9A%84%E7%BB%83%E4%B9%A0%E9%A2%98%E7%AD%94%E6%A1%88-%E7%BC%96%E5%8F%B7%E6%A0%87%E9%A2%98.png)
使用软件包中的出色宏tcolorbox
,我在一本概率书中的每一章都写了带有解决方案的练习。因为我想将所有解决方案写在一章中,所以我在这里使用了我的问题中开发的真正巧妙的宏:
现在,我使用以下宏对章节进行“文字”编号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three\or
four\or
five\or
six\or
seven\or
eight\or
nine\or
ten
\else
I need more words\fi}
\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\patchcmd{\@makechapterhead}{\thechapter}{\words{chapter}}{}{}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
并且我想对解决方案的标题使用相同的编号,例如Solutions of the exercises of the chapter one
,
Solutions of the exercises of the chapter two
.. ETC。
我怎样才能做到这一点 ?
我在这里给出使用的 .tex 文件:
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{xpatch}
% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\noindent \bgroup\bfseries Solutions of the exercises of the chapter #1\egroup%
}
\makeatletter
\xpretocmd{\chapter}{%
\begingroup
%% \ifnum\value{chapter}>0\relax
%% \tcbrecord{\string\clearpage}% Write a clearpage after the first chapter for each new chapter
%% \fi %% Uncomment these 3 lines if you want a pagebreak between the solutions of 2 chapters
\c@chapter \numexpr\c@chapter+1% Increase the count register `\@chapter` by one to trick `\thechapter` using the 'correct' chapter number
\tcbrecord{%
\solutionchapterformat{\thechapter}}%
\endgroup
}{}{}
\NewDocumentCommand{\extrasolutioncontent}{+m}{%
%%\tcbrecord{Extra solution stuff\par}% Remove this later on!
\tcbrecord{\detokenize{#1}}%
}
\newcommand{\fetchsolutions}{%
%For the first chapter
\begingroup
\c@chapter1%
\solutionchapterformat{\thechapter}%
\endgroup% Now get the rest of the stuff
\tcbinputrecords
}
\makeatother
\NewTColorBox[auto counter,number within=chapter]{exercise}{m+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:#1},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution:#1}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
#2
}
\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=}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three\or
four\or
five\or
six\or
seven\or
eight\or
nine\or
ten
\else
I need more words\fi}
\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\patchcmd{\@makechapterhead}{\thechapter}{\words{chapter}}{}{}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{geometry}
\geometry{paperwidth=180mm, paperheight=180mm, top=-10mm, bottom=25mm}
\begin{document}
\chapter{The first chapter}
\tcbstartrecording
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
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}
\chapter{The second chapter}
\begin{exercise}{Ex2}[coltitle=cyan!80!black]
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}
We can write any code between two exercises, directly...
\extrasolutioncontent{We can write too any code between two solutions, by using the ingenious $\backslash$extrasolutioncontent macro developed in the answer...}
\begin{exercise}{Ex3}[coltitle=cyan!80!black]
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
\chapter{Solutions of the exercices}
\fetchsolutions
\end{document}
以及一些“有趣”的页面的编辑……
编辑。我使用包编辑我的问题以给出答案pgfplots
,我给出的是“答案”,但网站的网站管理员说我必须编辑我的问题才能给出答案,我这样做了!
序言中的以下命令:
\def\mywordscounter{{"zero","one","two","three"}} %%% You can go far from..
%% Counters of pgf begins always by 0...
%% The non-numeric-values must be between " "
% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\section{Solutions of the exercises of the chapter \protect\pgfmathparse{\mywordscounter[#1]}\pgfmathresult}%
}
可以完成这项工作。可以注意到,根据@ChristianHupfer的建议,在命令\protect
之前添加了命令,它提供了合适的输出。\solutionchapterformat
不幸的是,我的文件有问题,因为其内容是错误的!这是更新后使用的 tex 文件:
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{xpatch}
\usepackage{pgfplots}
\def\mywordscounter{{"zero","one","two","three"}} %%% You can go far from..
%% Counters of pgf begins always by 0...
%% The non-numeric-values must be between " "
% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\section{Solutions of the exercises of the chapter \protect\pgfmathparse{\mywordscounter[#1]}\pgfmathresult}%
}
\makeatletter
\xpretocmd{\chapter}{%
\begingroup
%% \ifnum\value{chapter}>0\relax
%% \tcbrecord{\string\clearpage}% Write a clearpage after the first chapter for each new chapter
%% \fi %% Uncomment these 3 lines if you want a pagebreak between the solutions of 2 chapters
\c@chapter \numexpr\c@chapter+1% Increase the count register `\@chapter` by one to trick `\thechapter` using the 'correct' chapter number
\tcbrecord{%
\solutionchapterformat{\thechapter}}%
\endgroup
}{}{}
\NewDocumentCommand{\extrasolutioncontent}{+m}{%
%%\tcbrecord{Extra solution stuff\par}% Remove this later on!
\tcbrecord{\detokenize{#1}}%
}
\newcommand{\fetchsolutions}{%
%For the first chapter
\begingroup
\c@chapter1%
\solutionchapterformat{\thechapter}%
\endgroup% Now get the rest of the stuff
\tcbinputrecords
}
\makeatother
\NewTColorBox[auto counter,number within=chapter]{exercise}{m+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:#1},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution:#1}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
#2
}
\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=}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three
\else
I need more words\fi}
\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\patchcmd{\@makechapterhead}{\thechapter}{\words{chapter}}{}{}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\tableofcontents
\chapter{The first chapter}
\tcbstartrecording
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
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}
\chapter{The second chapter}
\begin{exercise}{Ex2}[coltitle=cyan!80!black]
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}
We can write any code between two exercises, directly...
\extrasolutioncontent{We can write too any code between two solutions, by using the ingenious $\backslash$extrasolutioncontent macro developed in the answer...}
\begin{exercise}{Ex3}[coltitle=cyan!80!black]
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
\fetchsolutions
\end{document}
这会导致内容输出错误......“第 0.017 章练习的解决方案”!!
有什么问题 ?
答案1
使用fmtcount
包及其\numberstring
宏\numberstringnum
来产生数字的单词对应关系。
\documentclass{book}
\usepackage{fmtcount}
\usepackage[most]{tcolorbox}
\usepackage{xpatch}
% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\noindent \bgroup\bfseries Solutions of the exercises of the chapter \numberstringnum{#1}\egroup%
}
\makeatletter
\xpretocmd{\chapter}{%
\begingroup
%% \ifnum\value{chapter}>0\relax
%% \tcbrecord{\string\clearpage}% Write a clearpage after the first chapter for each new chapter
%% \fi %% Uncomment these 3 lines if you want a pagebreak between the solutions of 2 chapters
\c@chapter \numexpr\c@chapter+1% Increase the count register `\@chapter` by one to trick `\thechapter` using the 'correct' chapter number
\tcbrecord{%
\solutionchapterformat{\thechapter}}%
\endgroup
}{}{}
\NewDocumentCommand{\extrasolutioncontent}{+m}{%
%%\tcbrecord{Extra solution stuff\par}% Remove this later on!
\tcbrecord{\detokenize{#1}}%
}
\newcommand{\fetchsolutions}{%
%For the first chapter
\begingroup
\c@chapter1%
\solutionchapterformat{\thechapter}%
\endgroup% Now get the rest of the stuff
\tcbinputrecords
}
\makeatother
\NewTColorBox[auto counter,number within=chapter]{exercise}{m+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:#1},
attach title to upper=\quad,
after upper={\par\hfill\textcolor{green!40!black}%
{\itshape Solution on page~\pageref{solution:#1}}},
lowerbox=ignored,
savelowerto=solutions/exercise-\thetcbcounter.tex,
record={\string\solution{#1}{solutions/exercise-\thetcbcounter.tex}},
#2
}
\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=}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MACRO FOR LITERAL NUMBERING CHAPTERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\words[1]{\expandafter\xwords\csname c@#1\endcsname}
\def\xwords#1{\ifcase#1\or
one\or
two\or
three
\else
I need more words\fi}
\renewcommand{\words}[1]{\numberstring{chapter}}
\usepackage{etoolbox} %% comment if 'etoolbox' have been loaded before
\makeatletter
\patchcmd{\@makechapterhead}{\thechapter}{\words{chapter}}{}{}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END MACRO FOR LITERAL NUMBERING %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{geometry}
\geometry{paperwidth=180mm, paperheight=180mm, top=-10mm, bottom=25mm}
\begin{document}
\chapter{The first chapter}
\tcbstartrecording
\begin{exercise}{Ex1}[coltitle=cyan!80!black]
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}
\chapter{The second chapter}
\begin{exercise}{Ex2}[coltitle=cyan!80!black]
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}
We can write any code between two exercises, directly...
\extrasolutioncontent{We can write too any code between two solutions, by using the ingenious $\backslash$extrasolutioncontent macro developed in the answer...}
\begin{exercise}{Ex3}[coltitle=cyan!80!black]
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
\chapter{Solutions of the exercices}
\fetchsolutions
\end{document}
答案2
我可以通过使用pgfplots
包来解决我的问题,该包可以定义“文字”计数器。我的问题序言中的以下命令:
\usepackage{pgfplots}
\def\mywordscounter{{"zero","one","two","three"}} %%% You can go far from..
%% Counters of pgf begins always by 0...
%% The non-numeric-values must be between " "
% Formatting command as a 'headline' of the solutions of chapter X
\NewDocumentCommand{\solutionchapterformat}{m}{%
\noindent\bgroup\bfseries Solutions of the exercises of the chapter \pgfmathparse{\mywordscounter[#1]}\pgfmathresult\egroup%
}
给出了合适的行为。感谢 Christian Hupfer 提供的原始精美宏。
但还有最后一个问题!如果我想以节的格式输出解决方案的标题。如果我使用以下命令:
\NewDocumentCommand{\solutionchapterformat}{m}{%
\section{Solutions of the exercises of the chapter \pgfmathparse{\mywordscounter[#1]}\pgfmathresult}%
}
我收到错误信息:
! Incomplete \iffalse; all text was ignored after line ...
我在这里做错了什么?