TiKZ
允许定义一个.style
稍后可在另一个中使用的:
\tikzset{
first/.style={rectangle, draw},
second/.style={first, inner sep=2mm}
}
中的任何更改first
都会被继承second
。是否可以对进行类似操作tcolorbox
?
举个例子,看看下面的代码,其中 anewtcolorbox
和 anewtcbox
用完全相同的选项定义。如何将相同的选项应用于两个定义而无需再次输入它们?
\documentclass{beamer}
\usetheme{Madrid}
\usepackage{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\tcbuselibrary{skins}
\makeatletter
\newtcolorbox{myblock}[2][]{
enhanced,
frame hidden, interior hidden, segmentation hidden,
coltitle=black,
fonttitle=\bfseries\rmfamily,
fontupper=\tiny,
title={#2},
overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
\draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
\shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
\shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
#1
}
\newtcbox{mybox}[2][]{
enhanced,
frame hidden, interior hidden, segmentation hidden,
coltitle=black,
fonttitle=\bfseries\rmfamily,
fontupper=\tiny,
title={#2},
overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
\draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
\shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
\shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
#1
}
\makeatother
\begin{document}
\begin{frame}{A nice box for beamer}
\begin{myblock}{Oups, nous avons rencontré une erreur.}
Note partagée non trouvée
L'URL fornie ne correspond pas à une note partagée valide. Cela a pu être causé par une erreur typographique dans le lien, ou le propiétaire l'a privaisée.
\end{myblock}
\mybox{Oups, this is a long title}{Note partagée non trouvée}
\mybox[left=0mm,right=1cm]{Oups}{Note partagée non trouvée}
\end{frame}
\end{document}
另一个例子。我想定义一些 tcolorbox,如 beamer 块、示例块和警报块。我可以对\mybox
前面的代码中的定义进行类似的操作,其中可选参数可以帮助更改某些方面,如标题颜色、边距等。
\mybox{Oups, this is a long title}{Note partagée non trouvée}
\mybox[left=0mm,right=1cm]{Oups}{Note partagée non trouvée}
但是如果我想使用\myexample
,,我知道\myalert
的\myblock
唯一方法是重新输入所有定义中的所有参数。你知道更简单的方法吗?
答案1
想法是一样的;\tcbset
你可以使用定义一个包含你的框将共享的设置的通用样式:
\makeatletter
\tcbset{common/.style={
enhanced,
frame hidden, interior hidden, segmentation hidden,
coltitle=black,
fonttitle=\bfseries\rmfamily,
fontupper=\tiny,
title={#1},
overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
\draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
\shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
\shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
}
}
\newtcolorbox{myblock}[2][]{common={#2},#1}
\newtcbox{mybox}[2][]{common={#2},#1}
\makeatother
一个完整的例子
\documentclass{beamer}
\usetheme{Madrid}
\usepackage{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}
\tcbuselibrary{skins}
\makeatletter
\tcbset{common/.style={
enhanced,
frame hidden, interior hidden, segmentation hidden,
coltitle=black,
fonttitle=\bfseries\rmfamily,
fontupper=\tiny,
title={#1},
overlay unbroken={\draw[gray,line width=1pt] (frame.north west) rectangle (frame.south east);
\draw[gray,line width=1pt] ([xshift=\kvtcb@lefttitle+\kvtcb@boxsep]interior.north west)--([xshift=-(\kvtcb@righttitle+\kvtcb@boxsep)]interior.north east);
\shade (frame.south west)--++(0,-1mm)--(frame.south)--cycle;
\shade (frame.south east)--++(0,-1mm)--(frame.south)--cycle;},
}
}
\newtcolorbox{myblock}[2][]{common={#2},#1}
\newtcbox{mybox}[2][]{common={#2},#1}
\makeatother
\begin{document}
\begin{frame}{A nice box for beamer}
\begin{myblock}{test text}
test text
\end{myblock}
\mybox{Oups, this is a long title}{test text}
\mybox[left=0mm,right=1cm]{Oups}{test text}
\end{frame}
\end{document}
当然,由于这些样式实际上是 TikZ 样式,因此存在“继承”:
\tcbset{
style1/.style={colback=cyan!10},
style2/.style={style1,colframe=orange},
style3/.style={style2,outer arc=0pt,arc=0pt
}
}
\newtcolorbox{myblockA}{style1}
\newtcolorbox{myblockB}{style2}
\newtcolorbox{myblockC}{style3}