![如何在一个 tcolorbox 标题中拥有两种不同的背景颜色?](https://linux22.com/image/451458/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%B8%80%E4%B8%AA%20tcolorbox%20%E6%A0%87%E9%A2%98%E4%B8%AD%E6%8B%A5%E6%9C%89%E4%B8%A4%E7%A7%8D%E4%B8%8D%E5%90%8C%E7%9A%84%E8%83%8C%E6%99%AF%E9%A2%9C%E8%89%B2%EF%BC%9F.png)
基本上,我正在定义一个tcolorbox
,并且我希望框的标题采用“示例 X.XX 示例的名称”的形式,其中“示例 X.XX”具有彩色背景,“示例名称”具有白色背景。
我尝试了两种替代方案:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}
\begin{document}
\begin{tcolorbox}[
coltitle = white, colframe = blue, colbacktitle = blue,
arc = 0mm, outer arc = 0mm,
title = Example X.XX Name of the example]
\lipsum[1]
\end{tcolorbox}
\begin{tcolorbox}[coltitle = blue, colframe = blue, colbacktitle = white,
arc = 0mm, outer arc = 0mm,
title = \colorbox{blue}{\color{white}{Example X.XX}} Name of the example]
\lipsum[1]
\end{tcolorbox}
\end{document}
在第一种情况下,整个标题都是彩色的,而在第二种情况下,彩色框与我定义的框的框架之间存在难看的间隙。我该如何实现这一点。
答案1
您需要加载skin
库,使用enhanced
选项并创建自己的标题框。首先用白色填充整个标题区域。然后将左侧直到单词“Example X.XX”填充为另一种颜色(例如蓝色)。我在“Example X.XX”和“示例名称”之间添加了一些空格,以使单词位于不同的填充区域中。我还应用了不同的文本颜色。看看这是否是您想要的。
\documentclass{article}
\usepackage[skins]{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}
\begin{document}
\begin{tcolorbox}[
coltitle = white, colframe = blue, colbacktitle = blue,
arc = 0mm, outer arc = 0mm,
title = Example X.XX Name of the example]
\lipsum[1]
\end{tcolorbox}
\begin{tcolorbox}[
colframe = blue,
coltitle=red,
sharp corners,
enhanced,
boxed title style={
boxrule=1.4pt,
sharp corners,
interior code={
\fill [white] (interior.north west) rectangle (interior.south east);
\filldraw [blue] (interior.north west) rectangle ([xshift=3.5cm]interior.south west);
},
},
attach boxed title to top,
title = \textcolor{white}{Example X.XX}\qquad Name of the example]
\lipsum[1]
\end{tcolorbox}
\end{document}
如果您想要一个自动计数器
tcolorbox
,我建议您使用库定义自己的框xparse
。在以下示例中,计数器将在部分内发生变化。
\documentclass{article}
\usepackage[skins,xparse]{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}
\DeclareTColorBox[auto counter,number within=section]{mybox}{ O{} m }{
colframe = blue,
coltitle=red,
sharp corners,
enhanced,
boxed title style={
boxrule=1.4pt,
sharp corners,
interior code={
\fill [white] (interior.north west) rectangle (interior.south east);
\filldraw [blue] (interior.north west) rectangle ([xshift=3cm]interior.south west);
},
},
attach boxed title to top,
title = \textcolor{white}{Example \thetcbcounter}\qquad #2,
#1
}
\begin{document}
\section{One}
\begin{mybox}{First example name}
\lipsum[1]
\end{mybox}
\section{Two}
\begin{mybox}[coltitle=black]{Second Name of the example}
\lipsum[2]
\end{mybox}
\end{document}
根据您的评论。您可以这样做。我创建了新的计数器
myexample
只是为了展示效果。您可以将其用作auto counter
前面的示例。
\documentclass{article}
\usepackage[skins,xparse]{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}
\newlength{\lefttitle}
\newcounter{myexample}
\DeclareTColorBox[use counter=myexample,number within=section]{mybox}{ O{} m }{
colframe = blue,
coltitle=red,
sharp corners,
enhanced,
boxed title style={
boxrule=1.4pt,
sharp corners,
interior code={
\fill [white] (interior.north west) rectangle (interior.south east);
\node [white,fill=blue,anchor=west,outer sep=0pt,inner sep=3pt] at ([xshift=-0.05pt]interior.west) {Example~\thetcbcounter} ;
},
},
attach boxed title to top,
title =\settowidth{\lefttitle}{Example~\thetcbcounter}\hspace{\lefttitle}#2,
#1
}
\begin{document}
\section{One}
\begin{mybox}{First example name}
\lipsum[1]
\end{mybox}
\section{Two}
\setcounter{myexample}{100}
\setcounter{section}{1000}
\begin{mybox}[coltitle=black]{Second Name of the example}
\lipsum[2]
\end{mybox}
\end{document}
答案2
Tikz 库tikzmark
应该是最终的解决方案但我没有成功。
这是一个很长的路要走:
\xcolorbox[<options>]{<color>}{<text>}
可以配置与盒子相似的装扮\colorbox
,但可以通过<options>
盒子分隔线(和出血边距)在每一侧进行配置,\tcbtitlename{<color>}{<text>}
一个基于 的新用户命令,其中包含保存分离值的内部\xcolorbox
知识。tcolorbox
用法:\begin{tcolorbox}[title=\tcbtitlename{blue}{text} text, ...]
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}
\makeatletter
\pgfqkeys{/colorbox}{
left/.initial=\fboxsep,
right/.initial=\fboxsep,
top/.initial=\fboxsep,
bottom/.initial=\fboxsep,
grow to left by/.initial=0pt,
grow to right by/.initial=0pt,
grow to top by/.initial=0pt,
grow to bottom by/.initial=0pt,
}
% \xcolorbox{<color>}{<text>}
% \xcolorbox[left=<dim>, ...]{<color>}{<text>}
%
\protected\def\xcolorbox{\@ifnextchar[\xcolor@box{\xcolor@box[]}}
\protected\def\xcolor@box[#1]#2#{\xcolor@@box{#1}{#2}}
\def\xcolor@@box#1#2#3{\xcolor@b@x{#1}\relax{\color#2{#3}}}
\long\def\xcolor@b@x#1#2#3#4%
{\leavevmode
\begingroup\pgfqkeys{/colorbox}{#1}%
\setbox\z@\hbox{%
\kern\dimexpr\pgfkeysvalueof{/colorbox/left}+\pgfkeysvalueof{/colorbox/grow to left by}\relax%
{\set@color#4}%
\kern\dimexpr\pgfkeysvalueof{/colorbox/right}+\pgfkeysvalueof{/colorbox/grow to right by}\relax}%
\dimen@\ht\z@
\advance\dimen@\dimexpr\pgfkeysvalueof{/colorbox/top}+\pgfkeysvalueof{/colorbox/grow to top by}\relax
\ht\z@\dimen@
\dimen@\dp\z@
\advance\dimen@\dimexpr\pgfkeysvalueof{/colorbox/bottom}+\pgfkeysvalueof{/colorbox/grow to bottom by}\relax
\dp\z@\dimen@
\hskip-\pgfkeysvalueof{/colorbox/grow to left by}%
\setbox\z@\hbox{%
{#2{#3\color@block{\wd\z@}{\ht\z@}{\dp\z@}\box\z@}}}%
\ht\z@\dimexpr\ht\z@-\pgfkeysvalueof{/colorbox/grow to top by}\relax
\dp\z@\dimexpr\dp\z@-\pgfkeysvalueof{/colorbox/grow to bottom by}\relax
\box\z@
\hskip-\pgfkeysvalueof{/colorbox/grow to right by}%
\endgroup}
% \tcbtitlename{<color>}{<title name>}
\newcommand\tcbtitlename[2]{%
\xcolorbox[%
left= 0pt,
top=0pt,
bottom=0pt,
grow to left by=\dimexpr\kvtcb@lefttitle+\kvtcb@boxsep\relax,
grow to top by=\dimexpr\kvtcb@toptitle+\kvtcb@boxsep\relax,
grow to bottom by=\dimexpr\kvtcb@bottomtitle+\kvtcb@boxsep\relax,
]{#1}{#2}%
}
\makeatother
\begin{document}
\begin{tcolorbox}[
coltitle = white, colframe = blue, colbacktitle = blue,
arc = 0mm, outer arc = 0mm,
title = Example X.XX Name of the example
]
\lipsum[2]
\end{tcolorbox}
\begin{tcolorbox}[
coltitle = blue, colframe = blue, colbacktitle = white,
arc = 0mm, outer arc = 0mm,
title = \tcbtitlename{blue}{\color{white}Example X.XX} Name of the example,
]
\lipsum[2]
\end{tcolorbox}
\end{document}
答案3
您只能使用 TeX 基本命令\hbox
、\vbox
、\hrule
、\vrule
、\setbox
等\kern
。在这种情况下,您不需要使用 tcolorbox 包。优点:一切都由您控制。
\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\newdimen\rulew \rulew=1.4pt
\def\hruleb{\hrule height\rulew}
\def\vruleb{\vrule width\rulew}
\long\def\mybox#1#2#3{\par
\setbox0=\vbox{{\leftskip=\parindent\rightskip=\parindent \parindent=0pt
\color{black}\medskip #3\par\medskip
}}
\setbox1=\hbox{{\hskip\parindent\color{white}#1\quad}}
\vbox{
\color{blue}\hruleb
\hbox to\hsize{%
\vruleb\hss
\vbox{
\hbox{\rlap{\vrule height1.1em depth.5em width\wd1}\box1 \quad\color{black}#2}
\hruleb
\hbox{\rlap{\kern\rulew
\color{lightgray!20}\vrule height\ht0 width\dimexpr\hsize-\rulew}\box0}%
}\hss\vruleb
}
\hruleb
}}
\begin{document}
\mybox{Example X.XX}{Name of the example}{
\lipsum[1]
}
\end{document}