考虑以下代码:
\documentclass[10pt]{article}
\usepackage{ifmtarg}
\usepackage{xcolor-material}
\usepackage[most]{tcolorbox}
\makeatletter
\newcommand{\makeboxtitle}[1]{\@ifmtarg{#1}{}{\textsc{\textbf{#1}\strut}}}
\makeatother
\newtcolorbox{untitledbox}{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
}
\newtcolorbox{titledbox}[1]{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
title = \makeboxtitle{#1},
}
\begin{document}
\begin{untitledbox} This is an untitled box.\end{untitledbox}
\begin{titledbox}{} This is a titled box.\end{titledbox}
\begin{titledbox}{A title} This is a titled box.\end{titledbox}
\end{document}
产生以下结果:
问题是,即使没有指定标题,第二个框也不等同于第一个框(仍然会出现红色的空标题行)。如何修改命令\makeboxtitle
或titledbox
环境,以便当标题为空时,看起来就像没有标题一样?
答案1
您希望标题成为可选参数(因为它是在您的设置中是可选的)并使用xparse
库:
\documentclass[10pt]{article}
\usepackage{xcolor-material}
\usepackage[most]{tcolorbox}
\tcbuselibrary{xparse}
\newtcolorbox{untitledbox}{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
}
\NewTColorBox{titledbox}{o}{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
fonttitle=\scshape,
IfValueT={#1}{title = \strut#1},
}
\begin{document}
\begin{untitledbox} This is an untitled box.\end{untitledbox}
\begin{titledbox} This is a titled box.\end{titledbox}
\begin{titledbox}[A title] This is a titled box.\end{titledbox}
\end{document}
请注意,这\bfseries\scshape
不是通常的组合,对于大多数字体,您只会得到\bfseries
。
您可以使用强制性参数来做到这一点,但我不认为这是最好的方法。
\documentclass[10pt]{article}
\usepackage{xcolor-material}
\usepackage[most]{tcolorbox}
\newtcolorbox{untitledbox}{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
}
\tcbset{
my title/.code{
\if\relax\detokenize{#1}\relax\else\tcbset{title=\strut#1}\fi
},
}
\newtcolorbox{titledbox}[1]{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
fonttitle=\scshape,
my title={#1},
}
\begin{document}
\begin{untitledbox} This is an untitled box.\end{untitledbox}
\begin{titledbox}{} This is a titled box.\end{titledbox}
\begin{titledbox}{A title} This is a titled box.\end{titledbox}
\end{document}
答案2
无需使用ifmtarg
,tcolorbox
将检查空标题。您设置的方式tcolorbox
不会找到空标题,而是会\makeboxtitle{}
在标题中查找(这不是空的),因此将产生非空标题的输出(意味着标题的红色背景显示出来)。
现在\makeboxtitle{}
将使用来\@ifmtarg
检查标题是否为空,如果不是,将设置一些字体命令。相反,您可以使用键fonttitle
来tcolorbox
指定使用的字体,并tcolorbox
决定标题何时为空。这就是以下内容:
\documentclass[10pt]{article}
\usepackage{xcolor-material}
\usepackage[most]{tcolorbox}
\newtcolorbox{untitledbox}{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
}
\newtcolorbox{titledbox}[1]{
enhanced, breakable, sharp corners, center title,
colframe = MaterialIndigo900,
colback = MaterialIndigo50,
colbacktitle = MaterialRed900,
fonttitle = \scshape\bfseries\strut,
title = {#1},
}
\begin{document}
\begin{untitledbox} This is an untitled box.\end{untitledbox}
\begin{titledbox}{} This is a titled box.\end{titledbox}
\begin{titledbox}{A title} This is a titled box.\end{titledbox}
\end{document}