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

我們看到,兩組紅色的文本框,它們的標題分別由 title 和 squeezed title 指定。因此,標題占據的高度會隨着標題本身的高度變化。兩組藍色的文本框,它們的標題則分別由 adjusted title 和 squeezed title* 指定。於是,標題占據的高度,會根據所處同一行的文本框中,標題所需高度最高的那個文本框來確定,看起來,就整齊多了。

后面兩組文本框,他們的標題分別由 squeezed title 和 squeezed title* 指定。我們發現,當標題長度過長時,標題在水平方向上會被壓縮。

標題位置

默認情況下,文本框的標題會附在文本框的上方。這一狀態稱為 attach。我們也可以將標題從默認位置上移開,這個動作稱為 detachdetach 之后,我們就可以用 tcolorbox 提供的宏,將標題放在任意自己想要放的位置。其中,\tcbtitletext 儲存了標題的文本內容,\tcbtitle 則儲存了標題的文本及樣式。

  • attach title: 默認樣式,標題將顯示在文本框上方的單獨單元格里。
  • detach title: 取消文本框上方單獨的單元格,標題需要用戶手工指定位置。
  • attach title to upper = ⟨text⟩: 取消文本框上方單獨的單元格,標題放在文本框 upper 部分的頭部。
tcb-title-pos.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
\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 傳入分隔符——這兩種方式的效果是一致的。

upper 和 lower 的狀態

tcolorbox 宏包輸出的文本框,在邏輯上可以分為 upper 和 lower 兩個部分,在代碼中用 \tcblower 命令分割 upper 和 lower 兩部分——如果沒有 \tcblower,則當前文本框只有 upper 部分,而沒有 lower 部分。

upper 和 lower 部分都有可見 (visible) 和不可見 (invisible) 兩種狀態,lower 部分還有被忽略 (ignored) 這一額外的狀態。

  • upperbox = ⟨visible|invisible⟩: 控制 upper 部分是否可見
  • lowerbox = ⟨visible|invisible|ignored⟩: 控制 lower 部分是否可見
  • visible: 同時控制 upper 和 lower 部分可見
  • invisible: 同時控制 upper 和 lower 部分不可見

下面的代碼很容易理解,就不做詳細解釋了。

tcb-visible.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\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}

upper 和 lower 部分的分隔

默認情況下,upper 和 lower 部分之間會有一個分隔,不過,我們也可以通過 lower separated 來控制是否顯示這一分隔符。

  • lower separated = ⟨true|false⟩: 控制是否顯示 upper 和 lower 兩部分中間的分隔。
tcb-lower-separated.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
\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}

這里用 tcbraster 環境做了一個明顯的左右對比。我們可以看到,當開啟 lower separated=false 時,各個 skin 下的分隔都不見了。值得注意的是,中間一行中,我們使用了 sidebyside 選項,它能將 upper 和 lower 兩個部分從上下的布局樣式,改變成左右的布局樣式。

顏色和字體控制

tcolorbox 提供了很多顏色控制選項,用來控制文本框各個部分的顏色效果。

  • colframe: 文本框框沿的顏色,默認是 black!75!white
  • colback: 文本框內的底色,默認是 black!5!white
  • colbacktitle: 文本框中標題框的底色,默認是 black!50!white
  • colupper: upper 部分文字的顏色。
  • collower: lower 部分文字的顏色。
  • coltext: 同時設置 upper 和 lower 部分文字的顏色。
  • coltitle: title 部分文字的顏色。
tcb-color.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
\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

線條

tcolorbox 輸出的文本框,其框線都可以設置寬度。

  • toprule/bottomrule/leftrule/rightrule: 分別設置文本框上、下、左、右的框線寬度。
  • titlerule: 設置標題框和內容框之間的框線寬度。
  • boxrule: 一口氣設置上述 5 條框線的寬度。
tcb-rule.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
\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}

這里我們首先用 boxrule = 0.5mm 將文本框所有的框線設置為 0.5mm,而后按照上下左右標題的順序,依次設置相應的框線為 3mm

弧度

文本框的弧度也是可以定制的。並且,文本框的弧度,在 tcolorbox 中被細分為內弧和外弧,可以分別設置。默認情況下,外弧的弧度與內弧保持一致(auto outer arc)。

  • arc=⟨length⟩: 設置內弧的半徑。
  • outer arc=⟨length⟩: 設置外弧的半徑,可以與內弧不同。
  • auto outer arc: 讓外弧的半徑自動隨着內弧的半徑增減,默認啟用。
tcb-arc.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
\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}

這里,第一個文本框,我們設置內弧的半徑為 0mm,得到了一個直角;第二個文本框,我們設置內弧半徑為 3mm,得到了一個比較圓潤的邊框;第三個文本框,我們設置內弧半徑為 1mm,外弧半徑為 4mm——與此同時,第四個文本框將內外弧半徑調換,得到了兩個有些另類的效果。

除了手工設置內外弧的半徑之外,tcolorbox 還預定義了一些樣式:circular arcbean arcoctogon arc

theorems 程序包

tcolorbox 宏包提供了 theorems 程序包來實現定理類的環境。theorems 程序包會自動加載 amsmath 宏包。加載調用的方法如前所述:

1
\tcbuselibrary{theorems}

基本用法

theorems 程序包提供了兩個命令,來生成文本框樣式的定理類環境。

  • \newtcbtheorem[⟨init options⟩]{⟨name⟩}{⟨display name⟩}{⟨options⟩}{⟨prefix⟩}
  • \renewtcbtheorem[⟨init options⟩]{⟨name⟩}{⟨display name⟩}{⟨options⟩}{⟨prefix⟩}

兩個命令分別都有 4 個必需參數和 1 個可選參數。

  • name: 創建的 LaTeX 環境名稱
  • display name: 創建的環境的標題名稱
  • options: 傳入 tcolorbox 的參數
  • prefix: 用於生成環境的 label
  • init options: 用於控制編號

我們來看幾個例子。

tcb-theorem-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
\documentclass[UTF8]{ctexart}
\usepackage{tcolorbox}
\tcbuselibrary{most}
\begin{document}
\section{標題}
\newtcbtheorem[number within=section]{mytheo}{我的定理環境}%
{colback=green!5,colframe=green!35!black,fonttitle=\bfseries}{th}
\begin{mytheo}{定理標題}{theoexample}
這里是定理內容。計數器按照設定,隨着~\verb|\section|~的更新而更新。

定理編號為:\ref{th:theoexample},位於第~\pageref{th:theoexample}~頁。
\end{mytheo}
\begin{mytheo}[label=myownlabel]{定理標題}{}
通過可選參數,可以繼續向~\verb|tcolorbox|~環境傳入參數。
\verb'label'~參數可以留空;當它不為空時,則可以作為定理環境的引用:\ref{myownlabel}。
\end{mytheo}
\begin{mytheo}{}{}
如果定理標題留空,那么編號之后的分隔符(默認是冒號)會自動消失。
\end{mytheo}
\begin{mytheo*}{不編號的定理之標題}
\verb'\newtcbtheorem'~也會同時定義帶星花的版本,用於產生不編號的定理。
不編號的定理,當然也就沒有交叉引用一說了。
\end{mytheo*}
\begin{mytheo*}{}
無標題的不編號的定理,當然也是可以有的。
\end{mytheo*}
\end{document}

選項

theorems 程序包也提供了諸多選項,用來控制生成的定理環境樣式。

  • separator sign = ⟨sign⟩: 設置定理環境編號和標題之間的分隔符,默認是 :
    • separator sign colon: 相當於 separator sign = {:}
    • separator sign dash: 相當於 separator sign = {-}
    • separator sign none: 相當於 separator sign = {}
  • description delimiters={⟨left⟩}{⟨right⟩}: 設置定理標題左右的定界符,默認是空。
    • description delimiters parenthesis: 相當於 description delimiters = {(}{)}
    • description delimiters none: 相當於 description delimiters = {}{}
  • description color = ⟨color⟩: 設置定理標題的字體顏色。
  • description font = ⟨text⟩: 設置定理標題的字體。
  • terminator sign = ⟨sign⟩: 設置定理標題結束后的終止符。
    • terminator sign colon: 相當於 terminator sign = {:}
    • terminator sign dash: 相當於 terminator sign = {-}
    • terminator sign none: 相當於 terminator sign = {}
tcb-theorems-title.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
\documentclass[UTF8]{ctexart}
\usepackage{tcolorbox}
\tcbuselibrary{most}
\usepackage{amsmath, amssymb}
\begin{document}
\section{標題}
\newtcbtheorem[number within=section]{mytheo}{定理}%
{colback=green!5,colframe=green!35!black,fonttitle=\bfseries}{th}
\begin{mytheo}{我的定理}{}
這是默認樣式。
\end{mytheo}
\begin{mytheo}[separator sign = {\ $\blacktriangleright$}]{我的定理}{}
分隔符修改為 $\blacktriangleright$。
\end{mytheo}
\begin{mytheo}[description delimiters parenthesis]{我的定理}{}
定界符修改為圓括號。
\end{mytheo}
\begin{mytheo}[description color=red!25!yellow,
description font= {\mdseries\itshape}]{我的定理}{}
標題的字體及顏色修改。
\end{mytheo}
\begin{mytheo}[terminator sign={.}]{我的定理}{}
標題后的終止符。
\end{mytheo}
\end{document}

實際動手制作一個定理環境看看

tcb-theorem-final.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
\documentclass[UTF8]{ctexart}
\usepackage{enumitem}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable, theorems}

\newtcbtheorem{question}{題~(理}%
{enhanced, breakable,
colback = white, colframe = cyan, colbacktitle = cyan,
attach boxed title to top left = {yshift = -2mm, xshift = 5mm},
boxed title style = {sharp corners},
fonttitle = \sffamily\bfseries, separator sign = {).~}}{qst}

\begin{document}
\section{測試}

\begin{question}{函數}{example}
已知函數 $ f(x) = (x - 2)\mathrm{e}^{2} + a (x - 1)^{2} $ 有兩個零點.
\begin{enumerate}[label=(\arabic*)]
\item 求 $ a $ 的取值范圍;
\item 設 $ x_{1} $, $ x_{2} $ 是 $ f(x) $ 的兩個零點,證明 $ x_{1} + x_{2} < 2 $.
\end{enumerate}
\end{question}

\end{document}

這里我們調用了 skins/breakable/theorems 三個程序包,其中 theorems 我們已經見過了。skins 程序包提供了各種「皮膚」,可以在基礎 tcolorbox 的基礎上擴展更多樣式:attach boxed title to top left 這一效果就來自 skins 程序包中的 enhanced 主題。breakable 的效果則使得文本框能夠跨頁。

其他的參數很好理解,就不用展開講了。

希望你看過這篇文章之后,能夠用 tcolorbox 宏包,制作出自己喜歡的文本框。

俗話說,投資效率是最好的投資。 如果您感覺我的文章質量不錯,讀后收獲很大,預計能為您提高 10% 的工作效率,不妨小額捐助我一下,讓我有動力繼續寫出更多好文章。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM