作者:Smooth(连接教育高级讲师)
首发于:UNSW学习知识库(UNSW Study Wiki)
创作时间:2025年3月10日
在UNSW计算机科学课程COMP3121/9101《算法设计与分析》的课程大纲里,你可能会惊讶地发现一个与LaTeX相关的工具——TikZ。这个看似与算法无关的绘图工具,实则是每位算法学习者值得掌握的利器。
TikZ是什么?
TikZ("TikZ ist kein Zeichenprogramm" 的递归缩写,德语意为"TikZ不是绘图软件")是LaTeX文档排版系统中的矢量图形绘制工具包。它通过纯文本代码生成精确的学术图表,主要特点包括:
-
原生LaTeX集成:直接嵌入.tex文件,完美支持数学公式排版
-
参数化绘图:通过变量和函数实现动态图形生成
-
出版物级输出:生成矢量PDF,缩放无损清晰度
-
跨平台兼容:纯文本格式适合版本控制(Git)
% 示例:绘制红黑树节点
\node[circle, draw=black, fill=red!30] (root) at (0,0) {$k$};
\node[circle, draw=black, fill=black!30] (left) at (-1,-1) {$l$};
\draw[->] (root) -- (left);
典型应用场景
算法过程可视化
在动态规划教学中,TikZ可绘制带注释的矩阵填充过程:
\matrix (m) [matrix of math nodes, nodes={draw, minimum size=8mm}]{
0 & 1 & 2 & 3 \\
1 & 0 & 1 & 2 \\
2 & 1 & 0 & 1 \\};
\draw[red,->] (m-1-3) -- (m-2-4) node[midway,above] {replace};
数据结构表达
绘制AVL树时,TikZ的树形布局引擎能自动处理节点位置:
\usetikzlibrary{trees}
\begin{tikzpicture}[level distance=15mm]
\node {50}
child { node {17}
child { node {12} }
child { node {23} }
}
child { node {72} };
\end{tikzpicture}
数学证明辅助
在贪心算法正确性证明中,用坐标系统绘制反例图示:
\draw[->] (0,0) -- (4,0) node[right] {time};
\draw[->] (0,0) -- (0,3) node[above] {profit};
\foreach \x/\y in {1/2, 2/1.5, 3/2.5}
\node[draw] at (\x,\y) {};
COMP3121/9101学习建议
最终目标是生成包含以下特征的树形结构: • 带圆形边框的节点(编号 1-7) • 无重叠节点 • 自定义填充颜色和样式。 • 使用全局样式优化代码。
最终结果:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
every node/.style={draw, circle}, % 所有节点带圆形边框
level 1/.style={sibling distance=4cm}, % 一级子节点间距
level 2/.style={sibling distance=2cm} % 二级子节点间距
]
% 根节点
\node[fill=yellow!30] {1}
% 左侧子树
child {
node[fill=blue!30] {2}
child { node[fill=green!30] {4} }
child { node[fill=green!30] {5} }
}
% 右侧子树
child {
node[fill=blue!30] {3}
child { node[fill=green!30] {6} }
child { node[fill=green!30] {7} }
};
\end{tikzpicture}
\end{document}
% 声明文档类型为 standalone(独立文档),自动裁剪内容至绘图区域
\documentclass{standalone}
% 导入 TikZ 绘图包
\usepackage{tikz}
% 文档内容开始
\begin{document}
% 创建 TikZ 绘图环境,配置全局样式参数
\begin{tikzpicture}[
% 所有节点的默认样式设置(适用于整个树结构)
every node/.style={
draw, % 绘制节点边框
circle % 将节点形状设为圆形
},
% 第1层级节点样式配置(根节点的直接子节点)
level 1/.style={
sibling distance=4cm % 设置兄弟节点水平间距为4厘米
},
% 第2层级节点样式配置(孙子节点)
level 2/.style={
sibling distance=2cm % 设置兄弟节点水平间距为2厘米
}
]
% 创建根节点(层级0)
% [fill=yellow!30] 表示30%透明度的黄色填充
\node[fill=yellow!30] {1} % 节点内容为数字1
% 添加第一个子节点分支(左侧子树)
child {
% 创建第1层子节点(节点2)
% [fill=blue!30] 表示30%透明度的蓝色填充
node[fill=blue!30] {2}
% 添加第2层子节点分支(节点2的左侧)
child {
% 创建第2层子节点(节点4)
% [fill=green!30] 表示30%透明度的绿色填充
node[fill=green!30] {4}
}
% 添加第2层子节点分支(节点2的右侧)
child {
node[fill=green!30] {5} % 节点5
}
}
% 添加第二个子节点分支(右侧子树)
child {
node[fill=blue!30] {3} % 第1层子节点(节点3)
child {
node[fill=green!30] {6} % 节点3的左侧子节点(节点6)
}
child {
node[fill=green!30] {7} % 节点3的右侧子节点(节点7)
}
}; % 分号结束整个树结构绘制
\end{tikzpicture}
% 文档内容结束
\end{document}
环境配置
创建独立文档:
新建 .tex
文件并使用 standalone 文档类(文档类型),该类型会自动裁剪空白区域:
\documentclass{standalone}
\begin{document}
% 绘图代码将放在此处
\end{document}
导入 TikZ 包:
在 preamble(导言区,即 \begin{document}
之前的区域)添加 TikZ 支持:
\usepackage{tikz}
类比:类似 C 语言中的 #include
指令。
基础绘图
-
使用
tikzpicture
环境
所有绘图操作需放置在 tikzpicture
环境中:
\begin{tikzpicture}
\draw[fill=blue!40!white] (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);
\end{tikzpicture}
• \draw
用于绘制路径 • fill=blue!40!white
表示填充颜色(40% 蓝色 + 白色混合)
练习:编译代码观察效果,尝试修改坐标和颜色。
节点与边
-
创建节点
使用 \node
命令定义节点:
\node (a) at (0, 0) {Hello! $2 + 2 = 4$}; % 命名节点为 "a"
\node (b) [below=1cm of a] {Hi! $3 + 3 = 6$}; % 在节点 a 下方 1cm 处创建节点 b
• (a)
是节点名称(用于后续引用) • below=1cm of a
表示相对定位
-
节点样式
为所有节点添加圆形边框(使用全局样式):
\begin{tikzpicture}[every node/.style={draw, circle}]
% 所有节点自动应用此样式
\end{tikzpicture}
绘制树结构
-
使用
child
操作
通过嵌套 child
创建树形结构:
\begin{tikzpicture}[every node/.style={draw, circle}]
\node {1}
child {node {2}}
child {node {3}};
\end{tikzpicture}
• 根节点 "1" 有两个子节点 "2" 和 "3"
-
扩展子树
为节点 "2" 和 "3" 添加子节点:
\node {2}
child {node {4}}
child {node {5}};
\node {3}
child {node {6}}
child {node {7}};
样式优化
-
解决节点重叠
使用 层级样式(level styles)调整间距:
\begin{tikzpicture}[
every node/.style={draw, circle},
level 1/.style={sibling distance=3cm}, % 一级子节点间距
level 2/.style={sibling distance=1.5cm} % 二级子节点间距
]
-
自定义颜色与填充
为单个节点添加样式:
\node[fill=yellow, red] {1}; % 黄色填充 + 红色文字
\node[fill=blue!20, green] {2}; % 20% 蓝色填充 + 绿色文字
任务要求与评分标准
提交内容
-
编译后的 PDF 文件
-
完整的
.tex
源代码
评分要点
• ✅ 节点 1-7 带圆形边框
• ✅ 节点无重叠(使用 sibling distance
)
• ✅ 自定义填充颜色和文字颜色
• ✅ 代码格式规范(缩进、空格)