tcolorbox

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

tcolorbox — Colored & Framed Boxes

tcolorbox — 带颜色与边框的文本框

CTAN: https://ctan.org/pkg/tcolorbox
Manual:
texdoc tcolorbox
(~500 pages)
CTAN: https://ctan.org/pkg/tcolorbox
手册:
texdoc tcolorbox
(约500页)

Setup

安装配置

latex
\usepackage[most]{tcolorbox}
% 'most' loads: skins, breakable, listings, theorems, fitting, hooks, xparse
% For minimal: \usepackage{tcolorbox} then \tcbuselibrary{...}

\tcbuselibrary{skins}       % enhanced appearance
\tcbuselibrary{breakable}   % boxes across pages
\tcbuselibrary{theorems}    % theorem integration
\tcbuselibrary{listings}    % code listings in boxes
\tcbuselibrary{listingsutf8} % UTF-8 code listings
latex
\usepackage[most]{tcolorbox}
% 'most' 会加载:skins、breakable、listings、theorems、fitting、hooks、xparse宏包
% 最小化加载:先\usepackage{tcolorbox},再用\tcbuselibrary{...}加载所需库

\tcbuselibrary{skins}       % 启用增强外观效果
\tcbuselibrary{breakable}   % 支持跨页文本框
\tcbuselibrary{theorems}    % 集成定理环境
\tcbuselibrary{listings}    % 在文本框中插入代码列表
\tcbuselibrary{listingsutf8} % 支持UTF-8编码的代码列表

Basic Usage

基础用法

latex
\begin{tcolorbox}
  This is a simple colored box.
\end{tcolorbox}

\begin{tcolorbox}[colback=blue!5, colframe=blue!75!black, title=My Title]
  A box with title and custom colors.
\end{tcolorbox}
latex
\begin{tcolorbox}
  这是一个简单的带颜色文本框。
\end{tcolorbox}

\begin{tcolorbox}[colback=blue!5, colframe=blue!75!black, title=我的标题]
  一个带标题和自定义颜色的文本框。
\end{tcolorbox}

Common Options

常用参数

OptionDefaultDescription
colback
black!5!white
Background color
colframe
black!75!white
Frame color
coltitle
white
Title text color
colbacktitle
(from
colframe
)
Title background
coltext
black
Body text color
title
(none)Box title
fonttitle
Title font (
\bfseries\large
)
fontupper
Upper (main) body font
fontlower
Lower part font
arc
1mm
Corner radius
boxrule
0.5mm
Frame thickness
leftrule
(from boxrule)Left rule only
toprule
(from boxrule)Top rule only
width
\linewidth
Box width
left
4mm
Left padding
right
4mm
Right padding
top
2mm
Top padding
bottom
2mm
Bottom padding
boxsep
1mm
All-sides padding
sharp corners
Remove rounding
rounded corners
Default
halign
justify
left
,
center
,
right
,
justify
valign
top
top
,
center
,
bottom
参数默认值说明
colback
black!5!white
背景颜色
colframe
black!75!white
边框颜色
coltitle
white
标题文字颜色
colbacktitle
继承
colframe
标题背景颜色
coltext
black
正文文字颜色
title
文本框标题
fonttitle
标题字体(默认
\bfseries\large
fontupper
上部(主内容区)字体
fontlower
下部内容区字体
arc
1mm
圆角半径
boxrule
0.5mm
边框厚度
leftrule
继承
boxrule
仅左侧边框厚度
toprule
继承
boxrule
仅顶部边框厚度
width
\linewidth
文本框宽度
left
4mm
左侧内边距
right
4mm
右侧内边距
top
2mm
顶部内边距
bottom
2mm
底部内边距
boxsep
1mm
全局内边距
sharp corners
取消圆角
rounded corners
默认启用圆角
halign
justify
水平对齐方式:
left
左对齐,
center
居中,
right
右对齐,
justify
两端对齐
valign
top
垂直对齐方式:
top
顶部,
center
居中,
bottom
底部

Box Styles

文本框样式

Titled Box

带标题的文本框

latex
\begin{tcolorbox}[
  colback=yellow!10,
  colframe=yellow!50!black,
  title=Warning,
  fonttitle=\bfseries,
  coltitle=black,
]
  Be careful with this operation.
\end{tcolorbox}
latex
\begin{tcolorbox}[
  colback=yellow!10,
  colframe=yellow!50!black,
  title=警告,
  fonttitle=\bfseries,
  coltitle=black,
]
  执行此操作时请小心。
\end{tcolorbox}

Left Bar (callout style)

左侧竖栏(提示框样式)

latex
\begin{tcolorbox}[
  enhanced,
  colback=blue!5,
  colframe=blue!5,       % hide frame
  borderline west={3pt}{0pt}{blue!70},
  sharp corners,
  boxrule=0pt,
]
  \textbf{Note:} This is a callout-style box.
\end{tcolorbox}
latex
\begin{tcolorbox}[
  enhanced,
  colback=blue!5,
  colframe=blue!5,       % 隐藏边框
  borderline west={3pt}{0pt}{blue!70},
  sharp corners,
  boxrule=0pt,
]
  \textbf{注意:} 这是一个提示框样式的文本框。
\end{tcolorbox}

Minimalist

极简风格

latex
\begin{tcolorbox}[
  enhanced,
  colback=gray!5,
  colframe=gray!5,
  boxrule=0pt,
  arc=0pt,
  left=10pt,
]
  Clean, simple box.
\end{tcolorbox}
latex
\begin{tcolorbox}[
  enhanced,
  colback=gray!5,
  colframe=gray!5,
  boxrule=0pt,
  arc=0pt,
  left=10pt,
]
  简洁干净的文本框。
\end{tcolorbox}

Upper/Lower Parts

上下分区文本框

latex
\begin{tcolorbox}[
  colback=white,
  colframe=blue!50!black,
  title=Theorem,
]
  $a^2 + b^2 = c^2$
  \tcblower
  This is the proof part (lower section).
\end{tcolorbox>
latex
\begin{tcolorbox}[
  colback=white,
  colframe=blue!50!black,
  title=定理,
]
  $a^2 + b^2 = c^2$
  \tcblower
  这是证明部分(下分区)。
\end{tcolorbox}

Breakable Boxes (Multi-Page)

跨页文本框

latex
\tcbuselibrary{breakable}

\begin{tcolorbox}[breakable, colback=white, colframe=blue!50!black]
  Very long content that may span multiple pages...
  % The box will automatically break at page boundaries
\end{tcolorbox}

% Enhanced breakable (better visuals)
\begin{tcolorbox}[enhanced jigsaw, breakable, colback=white, colframe=blue!50!black]
  Long content...
\end{tcolorbox}
latex
\tcbuselibrary{breakable}

\begin{tcolorbox}[breakable, colback=white, colframe=blue!50!black]
  超长内容将自动跨页显示...
  % 文本框会在分页处自动拆分
\end{tcolorbox}

% 增强版跨页(视觉效果更优)
\begin{tcolorbox}[enhanced jigsaw, breakable, colback=white, colframe=blue!50!black]
  超长内容...
\end{tcolorbox}

Theorem Boxes

定理框

latex
\tcbuselibrary{theorems}

% Define theorem types
\newtcbtheorem[number within=section]{theorem}{Theorem}{
  colback=blue!5,
  colframe=blue!50!black,
  fonttitle=\bfseries,
}{thm}

\newtcbtheorem[number within=section]{definition}{Definition}{
  colback=green!5,
  colframe=green!50!black,
  fonttitle=\bfseries,
}{def}

\newtcbtheorem[number within=section]{example}{Example}{
  colback=yellow!5,
  colframe=yellow!50!black,
  fonttitle=\bfseries,
}{ex}

\newtcbtheorem[number within=section]{proposition}{Proposition}{
  colback=red!5,
  colframe=red!50!black,
  fonttitle=\bfseries,
}{prop}

% Usage — two arguments: title and label
\begin{theorem}{Pythagoras}{pythagoras}
  For a right triangle with legs $a$, $b$ and hypotenuse $c$:
  \[ a^2 + b^2 = c^2 \]
\end{theorem}

\begin{definition}{Group}{group}
  A \emph{group} is a set $G$ with a binary operation...
\end{definition}

% Reference: Theorem~\ref{thm:pythagoras}
latex
\tcbuselibrary{theorems}

% 定义定理类型
\newtcbtheorem[number within=section]{theorem}{定理}{
  colback=blue!5,
  colframe=blue!50!black,
  fonttitle=\bfseries,
}{thm}

\newtcbtheorem[number within=section]{definition}{定义}{
  colback=green!5,
  colframe=green!50!black,
  fonttitle=\bfseries,
}{def}

\newtcbtheorem[number within=section]{example}{示例}{
  colback=yellow!5,
  colframe=yellow!50!black,
  fonttitle=\bfseries,
}{ex}

\newtcbtheorem[number within=section]{proposition}{命题}{
  colback=red!5,
  colframe=red!50!black,
  fonttitle=\bfseries,
}{prop}

% 使用方法——两个参数:标题和标签
\begin{theorem}{勾股定理}{pythagoras}
  对于直角三角形,直角边为$a$$b$,斜边为$c$  \[ a^2 + b^2 = c^2 \]
\end{theorem}

\begin{definition}{}{group}
  \emph{}是一个集合$G$,配备二元运算...
\end{definition}

% 引用方法:定理~\ref{thm:pythagoras}

Theorem with Proof Below

带证明区的定理框

latex
\newtcbtheorem{mythm}{Theorem}{
  colback=blue!5,
  colframe=blue!50!black,
  fonttitle=\bfseries,
  separator sign={\ ---},
  description delimiters parenthesis,
}{thm}

\begin{mythm}{Fundamental Theorem}{fundamental}
  Every continuous function on $[a,b]$ is integrable.
  \tcblower
  \textit{Proof.} By construction... $\square$
\end{mythm}
latex
\newtcbtheorem{mythm}{定理}{
  colback=blue!5,
  colframe=blue!50!black,
  fonttitle=\bfseries,
  separator sign={\ ---},
  description delimiters parenthesis,
}{thm}

\begin{mythm}{微积分基本定理}{fundamental}
  闭区间$[a,b]$上的连续函数都是可积的。
  \tcblower
  \textit{证明:} 根据构造... $\square$
\end{mythm}

Code Boxes (Listings)

代码框(代码列表)

latex
\tcbuselibrary{listings}
% or \tcbuselibrary{listingsutf8} for UTF-8

% Inline listing box
\begin{tcblisting}{
  colback=gray!5,
  colframe=gray!75!black,
  listing only,        % show only code (not rendered)
  title=Python Example,
  listing options={language=Python, basicstyle=\ttfamily\small},
}
def hello():
    print("Hello, world!")
\end{tcblisting}

% Side-by-side: code + output
\begin{tcblisting}{
  colback=white,
  colframe=blue!50!black,
  listing side text,   % code left, text right
  title=LaTeX Example,
}
$\int_0^1 x^2\, dx = \frac{1}{3}$
\end{tcblisting}
latex
\tcbuselibrary{listings}
% 若需支持UTF-8编码,使用\tcbuselibrary{listingsutf8}

% 仅显示代码的文本框
\begin{tcblisting}{
  colback=gray!5,
  colframe=gray!75!black,
  listing only,        % 仅显示代码(不渲染效果)
  title=Python示例,
  listing options={language=Python, basicstyle=\ttfamily\small},
}
def hello():
    print("Hello, world!")
\end{tcblisting}

% 左右分栏:代码 + 渲染效果
\begin{tcblisting}{
  colback=white,
  colframe=blue!50!black,
  listing side text,   % 左侧代码,右侧渲染效果
  title=LaTeX示例,
}
$\int_0^1 x^2\, dx = \frac{1}{3}$
\end{tcblisting}

Code Box Style

自定义代码框样式

latex
\newtcblisting{codebox}[2][]{
  colback=gray!5,
  colframe=gray!80!black,
  title=#2,
  fonttitle=\bfseries\ttfamily,
  listing only,
  listing options={
    basicstyle=\ttfamily\small,
    keywordstyle=\color{blue},
    commentstyle=\color{green!60!black},
    stringstyle=\color{red!70!black},
    #1
  },
}

% Usage
\begin{codebox}[language=Python]{my\_script.py}
import numpy as np
x = np.linspace(0, 1, 100)
\end{codebox}
latex
\newtcblisting{codebox}[2][]{
  colback=gray!5,
  colframe=gray!80!black,
  title=#2,
  fonttitle=\bfseries\ttfamily,
  listing only,
  listing options={
    basicstyle=\ttfamily\small,
    keywordstyle=\color{blue},
    commentstyle=\color{green!60!black},
    stringstyle=\color{red!70!black},
    #1
  },
}

% 使用方法
\begin{codebox}[language=Python]{my\_script.py}
import numpy as np
x = np.linspace(0, 1, 100)
\end{codebox}

Using minted Instead of listings

使用minted替代listings

latex
\tcbuselibrary{minted}

\begin{tcblisting}{
  listing engine=minted,
  minted language=python,
  minted style=monokai,
  colback=gray!10,
  listing only,
}
def hello():
    print("Hello!")
\end{tcblisting}
latex
\tcbuselibrary{minted}

\begin{tcblisting}{
  listing engine=minted,
  minted language=python,
  minted style=monokai,
  colback=gray!10,
  listing only,
}
def hello():
    print("Hello!")
\end{tcblisting}

Skins

外观主题(Skins)

latex
\tcbuselibrary{skins}

% Enhanced (required for many visual features)
\begin{tcolorbox}[enhanced, ...]

% Bicolor (different upper/lower colors)
\begin{tcolorbox}[bicolor, colback=blue!10, colbacklower=green!10, ...]

% Beamer style
\begin{tcolorbox}[beamer, colback=blue!10, colframe=blue!50!black]

% Watermark
\begin{tcolorbox}[enhanced, watermark text=DRAFT, watermark color=red!20]
latex
\tcbuselibrary{skins}

% Enhanced主题(支持多数视觉特效)
\begin{tcolorbox}[enhanced, ...]

% 双色主题(上下分区不同颜色)
\begin{tcolorbox}[bicolor, colback=blue!10, colbacklower=green!10, ...]

% Beamer风格
\begin{tcolorbox}[beamer, colback=blue!10, colframe=blue!50!black]

% 水印效果
\begin{tcolorbox}[enhanced, watermark text=草稿, watermark color=red!20]

Custom Box Definitions

自定义文本框定义

latex
% Simple named box
\newtcolorbox{important}{
  colback=red!5,
  colframe=red!50!black,
  fonttitle=\bfseries,
  title=Important,
}

% Box with parameter
\newtcolorbox{mybox}[1]{
  colback=blue!5,
  colframe=blue!50!black,
  title=#1,
  fonttitle=\bfseries,
}

% Box with optional + required args
\newtcolorbox{notebox}[2][]{
  enhanced,
  colback=yellow!10,
  colframe=yellow!50!black,
  title=#2,
  #1,  % optional overrides
}

% Usage
\begin{important}
  Don't forget this!
\end{important}

\begin{mybox}{Title Here}
  Content.
\end{mybox}

\begin{notebox}[colback=red!10]{Warning}
  Override the background.
\end{notebox}
latex
% 简单命名文本框
\newtcolorbox{important}{
  colback=red!5,
  colframe=red!50!black,
  fonttitle=\bfseries,
  title=重要提示,
}

% 带参数的文本框
\newtcolorbox{mybox}[1]{
  colback=blue!5,
  colframe=blue!50!black,
  title=#1,
  fonttitle=\bfseries,
}

% 带可选参数+必填参数的文本框
\newtcolorbox{notebox}[2][]{
  enhanced,
  colback=yellow!10,
  colframe=yellow!50!black,
  title=#2,
  #1,  % 可选参数覆盖默认设置
}

% 使用示例
\begin{important}
  不要忘记这一点!
\end{important}

\begin{mybox}{自定义标题}
  文本内容。
\end{mybox}

\begin{notebox}[colback=red!10]{警告}
  覆盖默认背景颜色。
\end{notebox}

Complete Example: Lecture Notes Setup

完整示例:讲义模板配置

latex
\usepackage[most]{tcolorbox}

\newtcbtheorem[number within=section]{theorem}{Theorem}{
  enhanced, breakable,
  colback=blue!3, colframe=blue!50!black,
  fonttitle=\bfseries,
  separator sign={.},
}{thm}

\newtcbtheorem[number within=section]{definition}{Definition}{
  enhanced, breakable,
  colback=green!3, colframe=green!50!black,
  fonttitle=\bfseries,
}{def}

\newtcbtheorem[number within=section]{example}{Example}{
  enhanced, breakable,
  colback=yellow!5, colframe=yellow!50!black,
  fonttitle=\bfseries,
}{ex}

\newtcolorbox{remark}{
  enhanced,
  colback=gray!5, colframe=gray!5,
  borderline west={3pt}{0pt}{gray!50},
  sharp corners, boxrule=0pt,
}

\newtcolorbox{warning}{
  enhanced,
  colback=red!5, colframe=red!50!black,
  fonttitle=\bfseries, title=Warning,
}
latex
\usepackage[most]{tcolorbox}

\newtcbtheorem[number within=section]{theorem}{定理}{
  enhanced, breakable,
  colback=blue!3, colframe=blue!50!black,
  fonttitle=\bfseries,
  separator sign={.},
}{thm}

\newtcbtheorem[number within=section]{definition}{定义}{
  enhanced, breakable,
  colback=green!3, colframe=green!50!black,
  fonttitle=\bfseries,
}{def}

\newtcbtheorem[number within=section]{example}{示例}{
  enhanced, breakable,
  colback=yellow!5, colframe=yellow!50!black,
  fonttitle=\bfseries,
}{ex}

\newtcolorbox{remark}{
  enhanced,
  colback=gray!5, colframe=gray!5,
  borderline west={3pt}{0pt}{gray!50},
  sharp corners, boxrule=0pt,
}

\newtcolorbox{warning}{
  enhanced,
  colback=red!5, colframe=red!50!black,
  fonttitle=\bfseries, title=警告,
}

Common Pitfalls

常见问题排查

ProblemCauseFix
Box doesn't break across pagesMissing
breakable
Add
breakable
option
Visual artifacts when breakingStandard skin + breakableUse
enhanced jigsaw
+
breakable
enhanced
features not working
Forgot
skins
library
Use
\tcbuselibrary{skins}
or
[most]
Theorem numbering wrongWrong counterCheck
number within
option
Listing encoding errorsUTF-8 in codeUse
listingsutf8
library
Overfull boxesBox wider than marginsSet
width=\linewidth
explicitly
Colors too brightFull saturationUse
color!intensity
like
blue!50!black
\newtcbtheorem
needs 2 args
Forgot label
\begin{thm}{Title}{label}
— both required
tcolorbox + hyperref conflictLoad orderLoad tcolorbox after hyperref
问题原因解决方法
文本框无法跨页未添加
breakable
参数
增加
breakable
选项
跨页时出现视觉异常使用标准主题+breakable改用
enhanced jigsaw
+
breakable
组合
Enhanced主题特效不生效未加载
skins
使用
\tcbuselibrary{skins}
[most]
参数加载所有常用库
定理编号错误计数器设置错误检查
number within
参数配置
代码列表出现编码错误代码含UTF-8字符使用
listingsutf8
文本框超出页面边距宽度设置过大显式设置
width=\linewidth
颜色过于刺眼饱和度太高使用
颜色!深度
格式,如
blue!50!black
而非纯
blue
\newtcbtheorem
需要2个参数
遗漏标签参数
\begin{thm}{标题}{标签}
——两个参数均为必填
tcolorbox与hyperref冲突加载顺序错误在hyperref之后加载tcolorbox

Tips

使用技巧

  • Use
    [most]
    when loading to get all common libraries
  • enhanced
    skin is needed for
    borderline
    ,
    watermark
    ,
    shadow
    , etc.
  • Combine
    breakable
    +
    enhanced jigsaw
    for clean page breaks
  • Mix
    \newtcbtheorem
    with
    \newtcolorbox
    — theorems for numbered, plain boxes for unnumbered
  • Use
    \tcbset{...}
    for global defaults across all boxes
  • Colors: mix with black for professional look:
    blue!50!black
    not
    blue
  • 加载宏包时使用
    [most]
    参数,可一次性加载所有常用库
  • borderline
    watermark
    shadow
    等特效需要启用
    enhanced
    主题
  • 组合使用
    breakable
    +
    enhanced jigsaw
    可实现更美观的跨页效果
  • 结合
    \newtcbtheorem
    (编号型)和
    \newtcolorbox
    (非编号型)使用
  • 使用
    \tcbset{...}
    设置全局文本框默认样式
  • 颜色搭配:与黑色混合可更显专业,如
    blue!50!black
    优于纯
    blue