问题
如何指定tcolorbox
已定义样式的嵌套的外观\tcbset{my-box-style/.style={ ... }}
?
现在的情况
在我的文档中,我使用tcolorbox
包来定义许多环境,例如定理、引理、证明和其他类型的框。所有这些框都共享一个基本样式\tcbset{my-box-style/.style={ ... }}
,但会覆盖一些颜色:
\tcbset{my-box-substyle-a/.style={ my-box-style, ... <overrides> ... }
我想用序言中指定的样式来控制嵌套框的外观。具体来说,我想更改left
嵌套框的参数值。
我尝试every box on layer n
以多种不同的方式使用该密钥,但无法使其发挥作用。
问题
代码
上图来源:
\documentclass{report}
\usepackage[many]{tcolorbox}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
\tcbset{my-box-style/.style={
colback=white,
sharp corners,
oversize,
left=10pt,
right=10pt,
boxsep=0pt,
}}
\tcbset{my-box-substyle-a/.style={
my-box-style,
colback=green!10,
}}
\tcbset{my-box-substyle-b/.style={
my-box-style,
colback=blue!10,
}}
% My failed attempt:
\tcbset{every box on layer 2/.style={
left=5pt,
right=5pt,
}}
% End of my attempt.
\begin{document}
I want the inner boxes from here:
\begin{tcolorbox}[my-box-substyle-a]
Outer box.
\begin{tcolorbox}[my-box-substyle-b]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
\begin{tcolorbox}[my-box-substyle-b]
Outer box.
\begin{tcolorbox}[my-box-substyle-a]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
to look like the inner box here:
\begin{tcolorbox}[my-box-substyle-a]
Outer box.
\begin{tcolorbox}[my-box-substyle-b,left=5pt,right=5pt]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
\begin{tcolorbox}[my-box-substyle-b]
Outer box.
\begin{tcolorbox}[my-box-substyle-a,left=5pt,right=5pt]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
without having to type \textbf{left=5pt,right=5pt} manually in each inner box in my (long) document. The order in which the styles are nested should not matter.
\end{document}
答案1
这里的问题是样式every box on layer n
被应用前env的可选参数tcolorbox
,因此设置left
和将被style 中的right
类似设置()覆盖。left=10pt, right=10pt
my-box-style
选项1:
在下面的例子中,late options
样式由 定义every box on layer 2
并应用于 的末尾my-box-style
。
\documentclass{report}
\usepackage[many]{tcolorbox}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
\tcbset{my-box-style/.style={
colback=white,
sharp corners,
oversize,
left=10pt,
right=10pt,
boxsep=0pt,
late options/.try,
}}
\tcbset{my-box-substyle-a/.style={
my-box-style,
colback=green!10,
}}
\tcbset{my-box-substyle-b/.style={
my-box-style,
colback=blue!10,
}}
\tcbset{every box on layer 2/.append style={
late options/.style={
left=5pt,
right=5pt,
}
}}
\begin{document}
I want the inner boxes from here:
\begin{tcolorbox}[my-box-substyle-a]
Outer box.
\begin{tcolorbox}[my-box-substyle-b]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
\begin{tcolorbox}[my-box-substyle-b]
Outer box.
\begin{tcolorbox}[my-box-substyle-a]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
\end{document}
选项 2:这次on layer={<num>}{<options>}
提供了一个新的选项,我认为比以下方式更清晰every box on layer n
:
\documentclass{report}
\usepackage[many]{tcolorbox}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex}
\makeatletter
\tcbset{
on layer/.code 2 args={%
\ifnum\c@tcblayer=\numexpr#1\relax
\pgfkeysalso{#2}%
\fi
}
}
\makeatother
\tcbset{my-box-style/.style={
colback=white,
sharp corners,
boxsep=0pt,
on layer={1}{left=10pt, right=10pt},
on layer={2}{left=5pt, right=5pt},
oversize,
}}
\tcbset{my-box-substyle-a/.style={
my-box-style,
colback=green!10,
}}
\tcbset{my-box-substyle-b/.style={
my-box-style,
colback=blue!10,
}}
\begin{document}
I want the inner boxes from here:
\begin{tcolorbox}[my-box-substyle-a]
Outer box.
\begin{tcolorbox}[my-box-substyle-b]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
\begin{tcolorbox}[my-box-substyle-b]
Outer box.
\begin{tcolorbox}[my-box-substyle-a]
Inner box.
\end{tcolorbox}
\end{tcolorbox}
\end{document}
2022 年 2 月 4 日更新:根据tcolorbox
选项的文档oversize
,我已移至oversize
最后。这不会改变输出。