技术笔记: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}


相关文章
LaTeX中定义新命令和环境
LaTeX中定义新命令和环境
841 0
LaTeX中定义新命令和环境
|
11月前
|
机器学习/深度学习 算法 搜索推荐
从理论到实践,Python算法复杂度分析一站式教程,助你轻松驾驭大数据挑战!
【10月更文挑战第4天】在大数据时代,算法效率至关重要。本文从理论入手,介绍时间复杂度和空间复杂度两个核心概念,并通过冒泡排序和快速排序的Python实现详细分析其复杂度。冒泡排序的时间复杂度为O(n^2),空间复杂度为O(1);快速排序平均时间复杂度为O(n log n),空间复杂度为O(log n)。文章还介绍了算法选择、分而治之及空间换时间等优化策略,帮助你在大数据挑战中游刃有余。
316 3
|
JSON 数据格式
VSCode无法写入用户设置 请打开用户设置并清除错误或警告, 然后重式
VSCode无法写入用户设置 请打开用户设置并清除错误或警告, 然后重式
|
数据采集 机器学习/深度学习 人工智能
Python的主要应用领域
Python的主要应用领域
1348 0
Python的主要应用领域
conda常用操作和配置镜像源
conda常用操作和配置镜像源
29224 0
|
9月前
|
人工智能 JSON API
使用 Qwen 生成数据模型和进行结构化输出
本教程展示如何使用CAMEL框架和Qwen模型生成结构化数据。CAMEL是一个强大的多智能体框架,支持复杂的AI任务;Qwen由阿里云开发,具备自然语言处理等先进能力。教程涵盖安装、API密钥设置、定义Pydantic模型,并演示了通过Qwen生成JSON格式的学生信息。最后,介绍了如何利用Qwen生成多个随机学生信息的JSON格式数据。欢迎在[CAMEL GitHub](https://github.com/camel-ai/camel)上为项目点星支持。
3039 70
|
12月前
|
数据采集 机器学习/深度学习 算法
【BetterBench博士】2024年华为杯E题:高速公路应急车道紧急启用模型 Python代码实现
本文介绍了2024年“华为杯”中国研究生数学建模竞赛的选题分析,重点讨论了高速公路应急车道启用模型的问题。文章详细描述了如何使用YOLOv5和SORT算法进行车辆检测与跟踪,计算车流密度、流量及速度,并利用随机森林回归预测交通拥堵。此外,还提出了多情景动态分析和虚拟应急车道控制策略,以及优化数据采集点布置的方法。提供了完整的Python代码和B站视频教程链接,帮助读者深入理解并实践该模型。
538 6
【BetterBench博士】2024年华为杯E题:高速公路应急车道紧急启用模型 Python代码实现
|
自然语言处理 开发者
通用文本向量模型全新升级至V3,开通百炼速来体验~~
阿里云新推出通用文本向量模型text-embedding-v3,基于LLM,支持50+语言,包括新增的意大利语等。模型升级亮点:8K长文本支持、可变向量维度、Sparse向量及不分Query/Document类型。现在提供50万免费tokens,有效期180天,计费0.0007元/1000 tokens。体验请访问[阿里云百炼官网](https://bailian.console.aliyun.com/?spm=a2c6h.13046898.publish-article.6.63066ffaL32qHM)
5712 0
|
12月前
|
Web App开发 XML 数据可视化
MathML详解
MathML(数学标记语言)是一种基于XML的语言,用于在Web页面中结构化地展示数学公式和符号。它通过内容模型和表现模型描述数学表达式的语义和排版,广泛应用于教育、科学出版等领域,并支持屏幕阅读器提升可访问性。尽管现代浏览器如Firefox对其支持良好,但在某些浏览器中可能需额外插件才能正确渲染。MathML的优点包括结构化表示和高可读性,但也存在一定的学习曲线和兼容性问题。
|
存储 Linux 数据处理
在Linux中,什么是管道操作,以及如何使用它?
在Linux中,什么是管道操作,以及如何使用它?