\documentclass{article} \usepackage{tcolorbox} \usepackage{colortbl} \usepackage{geometry} \geometry{margin = 1in} \begin{document} \begin{tcolorbox} This is my first \textbf{tcolorbox}. \end{tcolorbox}
\begin{tcolorbox} This is another \textbf{tcolorbox}. \tcblower Here, you see the lower part of the box. \end{tcolorbox}
\begin{tcolorbox}[title = {I Love Sophia}] This is a \textbf{tcolorbox} with title. \tcblower Here, you see the lower part of the box. \end{tcolorbox}
\documentclass{article} \usepackage{tcolorbox} \begin{document} \newtcolorbox{mybox}{colframe = red!75!black} \begin{mybox} This is my own box. \end{mybox}
\renewtcolorbox{mybox}{colback = red!25!white, colframe = red!75!black} \begin{mybox} This is my own box. \end{mybox}
\begin{tcolorbox}[colback = red!25!white, colframe = red!75!black] This is my own box. \end{tcolorbox} \end{document}
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \newtcolorbox{mybox}[2][] {colback = red!5!white, colframe = red!75!black, fonttitle = \bfseries, colbacktitle = red!85!black, enhanced, attach boxed title to top center={yshift=-2mm}, title=#2,#1} \begin{mybox}[colback=yellow]{Hello there} This is my own box with a mandatory title and options. \end{mybox} \end{document}
\noindent \foreach\n in {xxx,ggg,AAA,\"Agypten} {\begin{tcolorbox}[title=\n,colframe=red!75!black] Some content.\end{tcolorbox}}
\noindent \foreach\n in {xxx,ggg,AAA,\"Agypten} {\begin{tcolorbox}[adjusted title=\n,colframe=blue!75!black] Some content.\end{tcolorbox}}
\begin{tcbitemize}[raster columns=3,raster equal height, colframe=red!75!black,colback=red!5!white,fonttitle=\bfseries] \tcbitem[squeezed title={Short title}] First box \tcbitem[squeezed title={This is a very very long title}] Second box \tcbitem[squeezed title={This title is clearly to long for this application}] Third box \end{tcbitemize}
\begin{tcbitemize}[raster columns=3,raster equal height, colframe=blue!75!black,colback=red!5!white,fonttitle=\bfseries] \tcbitem[squeezed title*={Short title}] First box \tcbitem[squeezed title*={This is a very very long title}] Second box \tcbitem[squeezed title*={This title is clearly to long for this application}] Third box \end{tcbitemize} \end{document}
我们看到,两组红色的文本框,它们的标题分别由 title 和 squeezed title 指定。因此,标题占据的高度会随着标题本身的高度变化。两组蓝色的文本框,它们的标题则分别由 adjusted title 和 squeezed title* 指定。于是,标题占据的高度,会根据所处同一行的文本框中,标题所需高度最高的那个文本框来确定,看起来,就整齐多了。
后面两组文本框,他们的标题分别由 squeezed title 和 squeezed title* 指定。我们发现,当标题长度过长时,标题在水平方向上会被压缩。
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \newtcolorbox{mybox}[2][]{colbacktitle=red!10!white, colback=blue!10!white,coltitle=red!70!black, title={#2},fonttitle=\bfseries,#1} \begin{mybox}{My title} This is a \textbf{tcolorbox}. \end{mybox} \begin{mybox}[detach title,before upper={\tcbtitle\quad}]{My title} This is a \textbf{tcolorbox}. \end{mybox} \begin{mybox}[detach title,after upper={\par\hfill\tcbtitle}]{My title} This is a \textbf{tcolorbox}. \end{mybox}
\begin{mybox}[attach title to upper={\ ---\ }]{My title} This is a \textbf{tcolorbox}. \end{mybox} \begin{mybox}[attach title to upper,after title={:\ }]{My title} This is a \textbf{tcolorbox}. \end{mybox} \end{document}
在这里,6 -- 8 行是正常的情形;9 -- 11 行是 detach 的情形,然后用 before upper 将 \tcbtitle 放在 upper 部分之前;12 -- 14 行也是 detach 的情形,然后用 after upper 将 \tcbtitle 换行后放在 upper 部分的末尾;16 -- 18 行和 19 -- 21 行,都是 attach title to upper 的情形,前者直接给 attach title to upper 传入了标题后的分隔符,后者则使用 after title 传入分隔符——这两种方式的效果是一致的。
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \begin{tcolorbox}[upperbox=invisible,colback=white] This is a \textbf{tcolorbox} (but invisible). \end{tcolorbox} \begin{tcolorbox}[upperbox=invisible,colback=white] This is a \textbf{tcolorbox} (but invisible). \tcblower This is the lower part. \end{tcolorbox} \begin{tcolorbox}[lowerbox=invisible,colback=white] This is a \textbf{tcolorbox}. \tcblower This is the lower part (but invisible). \end{tcolorbox} \begin{tcolorbox}[lowerbox=ignored,colback=white] This is a \textbf{tcolorbox}. \tcblower This is the lower part (but ignored). \end{tcolorbox} \end{document}
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \begin{tcbraster}[colback=red!5!white,colframe=red!75!black, fonttitle=\bfseries,fontlower=\itshape] % \begin{tcolorbox}[title=Lower separated] This is the upper part. \tcblower This is the lower part. \end{tcolorbox} % \begin{tcolorbox}[title=Lower not separated,lower separated=false] This is the upper part. \tcblower This is the lower part. \end{tcolorbox} % \begin{tcolorbox}[sidebyside,title=Lower separated] This is the upper part. \tcblower This is the lower part. \end{tcolorbox} % \begin{tcolorbox}[sidebyside,title=Lower not separated,lower separated=false] This is the upper part. \tcblower This is the lower part. \end{tcolorbox} % \begin{tcolorbox}[beamer,title=Lower separated] This is the upper part. \tcblower This is the lower part. \end{tcolorbox} % \begin{tcolorbox}[beamer,title=Lower not separated,lower separated=false] This is the upper part. \tcblower This is the lower part. \end{tcolorbox} % \end{tcbraster} \end{document}
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \begin{tcolorbox}[title = {My Box}, colframe = red!50!white, colback = green!50!white, colbacktitle = green!30!white, colupper = yellow!25!red, collower = yellow!75!red, coltitle = black!90!white] This is a \textbf{tcolorbox}. \tcblower This is the lower part. \end{tcolorbox} \end{document}
fontupper: 控制 upper 部分的字体
fontlower: 控制 lower 部分的字体
fonttitle: 控制 title 部分的字体
tcb-font.tex
1 2 3 4 5 6 7 8 9 10 11
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \begin{tcolorbox}[title = {My Box}, fonttitle = \bfseries, fontupper = \sffamily, fontlower = \itshape] This is a \textbf{tcolorbox}. \tcblower This is the lower part. \end{tcolorbox} \end{document}
宽度控制
tcolorbox 宏包提供了三个选项对宽度进行最基本的控制,它们分别是:
width: 文本框的总宽度,默认是 \linewidth。
textwidth: 文本框内文本的宽度。
add to width: 在现有设置的基础上,调整文本框的总宽度。
tcb-width.tex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \tcbset{width=5cm, colback=red!5!white, colframe=red!75!black} \begin{tcolorbox} This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[text width = 5cm] \rule{5cm}{2ex} \end{tcolorbox} \begin{tcolorbox}[add to width = -1cm] This is a \textbf{tcolorbox}. \end{tcolorbox} \end{document}
这里,我们首先将 width 设置为 5cm,然后将 text width 设置为 5cm,最后将 width 在 5cm 的基础上,用 add to width 减去 1cm。
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \tcbset{boxrule = 0.5mm, colback=red!5!white, colframe=red!75!black, title = {My title}} \begin{tcolorbox} This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[toprule = 3mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[bottomrule = 3mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[leftrule = 3mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[rightrule = 3mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[titlerule = 3mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \end{document}
\documentclass{article} \usepackage{tcolorbox} \tcbuselibrary{most} \begin{document} \tcbset{colback = red!5!white, colframe = red!75!black} \begin{tcolorbox}[arc = 0mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[arc = 3mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[arc = 4mm, outer arc = 1mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \begin{tcolorbox}[arc = 1mm, outer arc = 4mm] This is a \textbf{tcolorbox}. \end{tcolorbox} \end{document}
这里我们调用了 skins/breakable/theorems 三个程序包,其中 theorems 我们已经见过了。skins 程序包提供了各种「皮肤」,可以在基础 tcolorbox 的基础上扩展更多样式:attach boxed title to top left 这一效果就来自 skins 程序包中的 enhanced 主题。breakable 的效果则使得文本框能够跨页。