我已经创建了一个NewDocumentEnvironment
用于打印不同格式的部分以描述问题及其解决方案(并在标题上附加可选注释):
\documentclass[10pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\usepackage{parskip}
\usepackage{tcolorbox}
\usepackage{lipsum}
\tcbuselibrary{skins,xparse,breakable}
\newcounter{problem}
\NewDocumentEnvironment{newprob}{o m +m}
{ % beginning code
\refstepcounter{problem}
\IfNoValueTF{#1}{ % start of true statement
\begin{tcolorbox}[skin=bicolor,
breakable,
title=\textbf{Problem~\theproblem:},
colframe=black!50!white,
colback=black!20!white,
colbacklower=black!5!white]
{#2}
\tcblower\vskip-\baselineskip
\tcbsubtitle[before skip=0pt]%
{\textbf{Solution:}}
{#3}
\end{tcolorbox}
} % end of true statement
{ % start of false statement
\begin{tcolorbox}[skin=bicolor,
breakable,
title=\textbf{Problem~\theproblem:}~(#1),
colframe=black!40!white,
colback=black!20!white,
colbacklower=black!5!white]
{#2}
\tcblower\vskip-\baselineskip
\tcbsubtitle[before skip=0pt]%
{\textbf{Solution:}}
{#3}
\end{tcolorbox}
} % end of false statement
} %end code
\begin{document}
\begin{newprob}
{
This is one problem...
}
{
... and this is one answer.
}
\end{newprob}
\begin{newprob}[special]
{
This is a speacial problem...
}
{
... and this is a special answer.
}
\end{newprob}
\end{document}
输出如下所示。
理想情况下,我希望使用 实现相同的结果\NewTColorBox
;但是我无法理解如何结合 的上半部分和下半部分tcolorbox
以及条件的不同格式要求。我想知道是否有更有经验的人tcolorbox
可以提供解决方案。
答案1
您需要的不是环境,而是命令。而且您不需要重复代码:只需在适当的位置有条件地排版可选参数(如果给出)。
\documentclass[10pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{geometry}
\geometry{
a4paper,
total={170mm,257mm},
left=20mm,
top=20mm,
}
\usepackage{tcolorbox}
\tcbuselibrary{skins,xparse,breakable}
\newcounter{problem}
\NewDocumentCommand{\newprob}{o +m +m}{%
\refstepcounter{problem}
\begin{tcolorbox}[
skin=bicolor,
breakable,
title=\textbf{Problem~\theproblem:}\IfValueT{#1}{~(#1)},
colframe=black!50!white,
colback=black!20!white,
colbacklower=black!5!white
]{#2}%
\tcblower\vskip-\baselineskip
\tcbsubtitle[before skip=0pt]%
{\textbf{Solution:}}{#3}
\end{tcolorbox}
}
\begin{document}
\newprob
{
This is one problem...
}
{
... and this is one answer.
}
\newprob[special]
{
This is a speacial problem...
}
{
... and this is a special answer.
}
\end{document}
或许也是可行的\NewTColorBox
,但何必呢?