技术笔记:tcolorbox宏包简明教程

简介: 技术笔记:tcolorbox宏包简明教程

嗯,我消失好几天了。那么,我都在做什么呢?没错,就是写这篇文章了。这篇文章写起来着实有些费神了。于是,如果你觉得这篇文章对你有帮助,不妨扫描文末的二维码,适量赞助一下哦~!


tcolorbox 宏包是 Thomas F. Sturm 开发的一个用于绘制彩色文本框的宏包。tcolorbox 底层基于 pgf,功能也是十分强大。


一个月前,有群友在群里问到如何制作下面这样的蓝色边框,于是这里借着实现这一边框的时机,简单讲讲 tcolorbox 的用法。


概述:安装与调用


和大多数宏包一样,tcolorbox 被 MiKTeX 和 TeX Live 都收录其中。因此,通常情况,你只需要用相应的宏包管理器安装就好。


使用起来,也很简单。和其他宏包一样,你需要在导言区,使用 \usepackage 命令调用这个宏包。


1


\usepackage{tcolorbox}


你可以在调用宏包时,用可选参数指明需要加载哪些程序库;也可以在调用 tcolorbox 之后,显式地使用 \tcbuselibrary 来调用 tcolorbox 提供的丰富程序库。比如,下面的代码调用了 skins, breakable, theorems 三个程序库。


1


2


\usepackage{tcolorbox}


\tcbuselibrary{skins, breakable, theorems}


每个程序库,都对应着一个 tcbXXX.code.tex 文件。实际上,使用 \tcbuselibrary 命令,就是调用了一个个这样的文件。以下是常见可用的 tcolorbox 程序库:


skins: 调用 tikz 宏包,提供丰富的文本框样式;


vignette: 提供一些装饰性的样式;


raster: 用以调整若干 tcolorbox 的排布样式


listings/listingsutf8/minted: 用以和对应的宏包联用,实现好看的代码清单


theorems: 用以生成定理类环境


breakable: 实现自动分页的文本框


magazine: 实现类似杂志的分段文本


fitting: 实现字体大小和文本框大小的自适应


tcolorbox 基础


基础环境和基础命令


tcolorbox 宏包提供了与宏包同名的环境,是整个宏包的基础环境,用于生成段落间的文本框。与之对应,宏包还提供了 \tcbox 命令,用于生成行内的文本框。


1


2


3


4


5


\begin{tcolorbox}【?options?】


?environment content?


\end{tcolorbox}


\tcbox【?options?】{?box content?}


tcolorbox 环境和 \tcbox 命令都可以接收一组选项,用来控制文本框的样式。我们来看看下面的代码,及其相应的效果。


tcb-basic.tex


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


40


41


42


43


44


45


\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}


\tcbset{colframe = blue!50!black, colback = white,


colupper = red!50!black, fonttitle = \bfseries,


nobeforeafter, center title}


Text \tcbox【tcbox raise base】{Hello World}\hfill


%


\tcbox【left = 0mm, right = 0mm, top = 0mm, bottom = 0mm, boxsep = 0mm,


toptitle = 0.5mm, bottomtitle = 0.5mm, title = {My table}】


{\arrayrulecolor{blue!50!black}


\renewcommand{\arraystretch}{1.2}%


\begin{tabular}{r|c|l}


One & Two & Three \


\hline\hline


Men & Mice & Lions \\hline


Upper & Middle & Lower


\end{tabular}}\hfill


%


\tcbox【colback=blue!85!black,


left = 0mm, right = 0mm, top = 0mm, bottom = 0mm,


boxsep = 1mm, arc = 0mm, boxrule = 0.5pt, title = {My picture}】


{\includegraphics【width = 5cm】{example-image}}


\end{document}


7 – 9 行是一个最朴素的 tcolorbox 环境。我们看到,在默认情况下,tcolorbox 输出了一个深灰色圆角边框、浅灰底色的文本框。


11 – 15 行则在上述最朴素的 tcolorbox 环境的基础上,增加了一条虚线。这条虚线由 \tcblower 控制,它将 tcolorbox 中的内容分成了上下两部分。


17 – 21 行则第一次指定了 tcolorbox 环境的选项。tcolorbox 环境和 \tcbox 都可以接受一串 key-value 的参数列表。这里,我们给 tcolorbox 环境传入了一个名为 title 的参数,它的值是 I Love Sophia。顾名思义,这给 tcolorbox 输出的文本框起了一个名字。默认情况下,这个名字会显示在文本框的头部。


23 – 25 行引入了一个新的命令——\tcbset。显而易见,tcb 是宏包名字的缩写,而 set 自然是「设置」的意思。\tcbset 也可以接受一串 key-value 参数列表,这些参数将对(同一个分组内) \tcbset 之后的所有 tcbcolorbox 环境和 \tcbox 命令生效。这里引入了一些参数,我们在此不深究它们的含义,且先看效果再说。


在 27 行,我们遇到了第一个 \tcbox 命令。在先前 \tcbset 设置的选项之外,这个 \tcbox 还显式地引入了 tcbox raise base 这一参数。结合效果,不难猜出这个选项调整了 \tcbox 生成盒子的基线(baseline)。


29 行和 40 行开头的两个 \tcbox 命令,一方面说明 tcolorbox 宏包提供了相当多的控制选项,因此文本框的样式是高度可定制的;另一方面说明了 \tcbox 内里可以装各种类型的文本——包括表格和图档。


定义和重定义新的 box


tcolorbox 宏包提供了 4 个命令,分别用来定义和重定义 tcolorbox 环境和 \tcbox 命令制作的文本框样式。


1


2


3


4


\newtcolorbox【?init options?】{?name?}【?number?】【?default?】{?options?}


\renewtcolorbox【?init options?】{?name?}【?number?】【?default?】{?options?}


\newtcbox【?init options?】{\?name?}【?number?】【?default?】{?options?}


\renewtcbox【?init options?】{\?name?}【?number?】【?default?】{?options?}


\newtcolorbox 类似 LaTeX 的 \newenvironment 命令,能够基于 tcolorbox 环境创建一个新的 box 环境;\renewcolorbox 则类似 LaTeX 的 \renewenvironment。\newtcbox 类似 LaTeX 的 \newcommand 命令,能够基于 \tcbox 命令创建一个新的 box 命令;\renewtcbox 则类似 LaTeX 的 \renewcommand 命令。


在这里


?init options? 通常是用来控制计数器的,具体可以参见 tcolorbox 宏包说明手册的第 5 章


?name? 是环境或命令的名字


?number? 是环境或命令参数的个数


?default? 是环境或命令可选参数的默认值


?options? 接受类似 \tcbset 的 key-value 参数列表


tcb-env.tex


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


\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}


这里,在 5 – 7 行,我们用 \newtcolorbox 定义了名为 mybox 的环境;在 9 //代码效果参考:http://www.lyjsj.net.cn/wz/art_23136.html

– 12 行,我们用 \renewtcolorbox 重新定义了名为 mybox 的环境;14 – 16 行则用原始的 tcolorbox 重复实现了 mybox 环境的效果。

tcb-env-complex.tex


1


2


3


4


5


6


7


8


9


10


11


12


13


14


\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}


在这里,我们用 \newtcolorbox 命令定义了一个稍微复杂的 mybox 环境。它接收两个参数,其中第一个参数是可选的,默认值为空;第二个参数则是必填的参数。第一个参数用作 tcolorbox 环境参数补充,第二个参数则是 box 的标题。另外值得注意的是,代码中引入了 most 这个程序库。most 会自动调取除 minted 和 documentation 之外所有的 tcolorbox 程序库。


tcb-command.tex


1


2


3


4


5


6


7


8


9


10


11


12


\documentclass{article}


\usepackage{tcolorbox}


\begin{document}


\newtcbox{\mybox}【1】【red】


{on line, arc = 0pt, outer arc = 0pt,


colback = #1!10!white, colframe = #1!50!black,


boxsep = 0pt, left = 1pt, right = 1pt, top = 2pt, bottom = 2pt,


boxrule = 0pt, bottomrule = 1pt, toprule = 1pt}


The \mybox【green】{quick} brown \mybox{fox}


\mybox【blue】{jumps} over the \mybox【green】{lazy} \mybox{dog}.


\end{document}


这里我们用 \newtcbox 命令定义了一个新的 \mybox 命令,它可以用来高亮选中的单词。


warp 既有环境


有的时候,我们希望将既有的 LaTeX 环境改造成带文本框样式的环境。这时候,可以使用 \tcolorboxenvironment环境。它的用法是


1


\tcolorboxenvironment{?name?}{?options?}


tex-env-redefine.tex


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


\documentclass{article}


\usepackage{tcolorbox}


\tcbuselibrary{most}


\newenvironment{myitemize}{\begin{itemize}}{\end{itemize}}


\tcolorboxenvironment{myitemize}


{blanker, before skip = 6pt, after skip = 6pt,


borderline west = {3mm}{0pt}{red}}


\begin{document}


Some text.


\begin{myitemize}


\item Alpha


\item Beta


\item Gamma


\end{myitemize}


More text.


\end{document}


在这里,我们用 \tcolorboxenvironment 命令,在 myitemize 环境外重新 wrap 了一层。新的 myitemize 环境在左侧有一道宽为 3mm 的红色提示线。不难发现,\tcolorboxenvironment 命令可以在(几乎)不改变原有环境效果的情况下,定义一个新的文本框环境。这种功能十分实用。


常用选项


这里列一些常用的选项,用来控制 tcolorbox 环境和 \tcbox 命令的效果。其它未尽选项和效果,可以查看 tcolorbox 的说明手册。


标题的内容控制


title: 设置标题内容,默认为空。


adjusted title: 设置标题内容,默认为空。使用 title 生成的标题,标题栏会根据标题内容的高低自动调整;而使用 adjusted title 生成的标题,标题栏的高度由当前行文本框中,标题占据高度最高的文本框决定。


squeezed title: 设置标题内容,默认为空。使用 squeezed title 生成的标题,如果超过允许的长度,不会换行,而是会在横向进行压缩。


squeezed title: 设置标题内容,默认为空。合并了 adjusted title 和 squeezed title 的效果。


tcb-titles.tex


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


\documentclass{article}


\usepackage{tcolorbox}


\tcbuselibrary{most}


\begin{document}


\tcbset{colback=white,arc=0mm,width=(\linewidth-4pt)/4,


equal height group=AT,before=,after=\hfill,fonttitle=\bfseries}


\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}


相关文章
|
存储 敏捷开发 缓存
Java开发手册——嵩山版(清幽现云山,虚静出内功)-4
Java开发手册——嵩山版(清幽现云山,虚静出内功)
1385 1
|
5天前
|
NoSQL Linux Shell
技术笔记:linux系统开发基础
技术笔记:linux系统开发基础
14 0
|
2月前
|
算法 芯片
嵌入式工程师如何快速的阅读datasheet的方法
嵌入式工程师如何快速的阅读datasheet的方法
89 0
|
12月前
|
测试技术
《软件方法》上册笔记
《软件方法》上册笔记
74 0
|
存储 自然语言处理 Linux
0基础C语言自学教程——收官之战——第十四节 文件的编译和链接
这个时候程序将使用一个运行时堆栈(stack),存储函数的局部变量和返回地址。程序同时也可以使用静态(static)内存,存储于静态内存中的变量在程序的整个执行过程一直保留他们的值。
131 0
0基础C语言自学教程——收官之战——第十四节 文件的编译和链接
|
设计模式 缓存 IDE
Java开发手册——嵩山版(清幽现云山,虚静出内功)-1
Java开发手册——嵩山版(清幽现云山,虚静出内功)
151 0
Java开发手册——嵩山版(清幽现云山,虚静出内功)-1
|
C++ Python
C++入门详细笔记(共八章)(中)
C++入门详细笔记(共八章)
85 0
C++入门详细笔记(共八章)(中)
|
存储 算法 搜索推荐
C++入门详细笔记(共八章)(下)
C++入门详细笔记(共八章)
96 0
C++入门详细笔记(共八章)(下)
|
SQL 存储 安全
Java开发手册——嵩山版(清幽现云山,虚静出内功)-3
Java开发手册——嵩山版(清幽现云山,虚静出内功)
127 0
|
存储 XML JSON
Java开发手册——嵩山版(清幽现云山,虚静出内功)-2
Java开发手册——嵩山版(清幽现云山,虚静出内功)
154 0