tcolorbox
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesetcolorbox — Colored & Framed Boxes
tcolorbox — 带颜色与边框的文本框
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 listingslatex
\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
常用参数
| Option | Default | Description |
|---|---|---|
| | Background color |
| | Frame color |
| | Title text color |
| (from | Title background |
| | Body text color |
| (none) | Box title |
| Title font ( | |
| Upper (main) body font | |
| Lower part font | |
| | Corner radius |
| | Frame thickness |
| (from boxrule) | Left rule only |
| (from boxrule) | Top rule only |
| | Box width |
| | Left padding |
| | Right padding |
| | Top padding |
| | Bottom padding |
| | All-sides padding |
| Remove rounding | |
| ✅ | Default |
| | |
| | |
| 参数 | 默认值 | 说明 |
|---|---|---|
| | 背景颜色 |
| | 边框颜色 |
| | 标题文字颜色 |
| 继承 | 标题背景颜色 |
| | 正文文字颜色 |
| 无 | 文本框标题 |
| 标题字体(默认 | |
| 上部(主内容区)字体 | |
| 下部内容区字体 | |
| | 圆角半径 |
| | 边框厚度 |
| 继承 | 仅左侧边框厚度 |
| 继承 | 仅顶部边框厚度 |
| | 文本框宽度 |
| | 左侧内边距 |
| | 右侧内边距 |
| | 顶部内边距 |
| | 底部内边距 |
| | 全局内边距 |
| 取消圆角 | |
| ✅ | 默认启用圆角 |
| | 水平对齐方式: |
| | 垂直对齐方式: |
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
常见问题排查
| Problem | Cause | Fix |
|---|---|---|
| Box doesn't break across pages | Missing | Add |
| Visual artifacts when breaking | Standard skin + breakable | Use |
| Forgot | Use |
| Theorem numbering wrong | Wrong counter | Check |
| Listing encoding errors | UTF-8 in code | Use |
| Overfull boxes | Box wider than margins | Set |
| Colors too bright | Full saturation | Use |
| Forgot label | |
| tcolorbox + hyperref conflict | Load order | Load tcolorbox after hyperref |
| 问题 | 原因 | 解决方法 |
|---|---|---|
| 文本框无法跨页 | 未添加 | 增加 |
| 跨页时出现视觉异常 | 使用标准主题+breakable | 改用 |
| Enhanced主题特效不生效 | 未加载 | 使用 |
| 定理编号错误 | 计数器设置错误 | 检查 |
| 代码列表出现编码错误 | 代码含UTF-8字符 | 使用 |
| 文本框超出页面边距 | 宽度设置过大 | 显式设置 |
| 颜色过于刺眼 | 饱和度太高 | 使用 |
| 遗漏标签参数 | |
| tcolorbox与hyperref冲突 | 加载顺序错误 | 在hyperref之后加载tcolorbox |
Tips
使用技巧
- Use when loading to get all common libraries
[most] - skin is needed for
enhanced,borderline,watermark, etc.shadow - Combine +
breakablefor clean page breaksenhanced jigsaw - Mix with
\newtcbtheorem— theorems for numbered, plain boxes for unnumbered\newtcolorbox - Use for global defaults across all boxes
\tcbset{...} - Colors: mix with black for professional look: not
blue!50!blackblue
- 加载宏包时使用参数,可一次性加载所有常用库
[most] - 、
borderline、watermark等特效需要启用shadow主题enhanced - 组合使用+
breakable可实现更美观的跨页效果enhanced jigsaw - 结合(编号型)和
\newtcbtheorem(非编号型)使用\newtcolorbox - 使用设置全局文本框默认样式
\tcbset{...} - 颜色搭配:与黑色混合可更显专业,如优于纯
blue!50!blackblue