LaTeX TikZ 基础使用笔记

· 更新于 2026年9月16日· 约 249 分钟· 49740 字

TikZ 是 LaTeX 中非常强大的绘图包,适合画流程图、几何图、函数图、示意图等。

1. 引入 TikZ

最小示例:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% 绘图命令
\end{tikzpicture}
\end{document}

如果写在普通文档中:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% 绘图命令
\end{tikzpicture}
\end{document}

常用编译方式:pdflatexxelatexlualatex 都可以。
如果单独画图,推荐使用 standalone 文档类。

2. tikzpicture 环境

所有 TikZ 图形通常放在 tikzpicture 环境中,[] 内可写可选参数:

\begin{tikzpicture}[scale=1.2]
% 绘图命令
\end{tikzpicture}

也可以放在 figure 环境中:

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
% 绘图命令
\end{tikzpicture}
\caption{TikZ 示例}
\label{fig:tikz-example}
\end{figure}

引用:图 \ref{fig:tikz-example}

3. 坐标系与坐标写法

3.1 直角坐标

\draw (0,0) -- (2,1);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;

3.2 极坐标

格式:(角度:半径)

\draw (0,0) -- (45:2);
\draw (0,0) -- (90:1.5);

3.3 相对坐标

\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;

说明:

  • ++(1,0):以上一个点为基准移动,并更新当前点。
  • +(1,0):以上一个点为基准移动,但不更新当前点。

对比:

\draw (0,0) -- +(1,0) -- +(0,1);
\draw (2,0) -- ++(1,0) -- ++(0,1);

3.4 命名坐标

\coordinate (A) at (0,0);
\coordinate (B) at (2,1);
\draw (A) -- (B);
\node[above] at (A) {A};
\node[above] at (B) {B};

4. 基本绘图命令

4.1 直线、折线、闭合路径

\draw (0,0) -- (2,0);
\draw (0,0) -- (1,1) -- (2,0);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;

4.2 矩形

\draw (0,0) rectangle (2,1);

4.3 圆和椭圆

\draw (0,0) circle (1);
\draw (0,0) ellipse (2 and 1);

4.4 圆弧

\draw (0,0) arc (0:90:1);
\draw (0,0) arc (0:180:2 and 1);

4.5 曲线

\draw (0,0) .. controls (1,0) and (1,1) .. (2,1);
\draw (0,0) parabola (1,1);
\draw (0,0) sin (1,1);

4.6 填充与描边

\fill[red!30] (0,0) circle (1);
\draw[fill=blue!20, draw=blue] (2,0) rectangle (4,1);
\filldraw[fill=green!20, draw=green!60!black] (0,-2) circle (0.8);
\shade[left color=red, right color=blue] (2,-2) rectangle (4,-1);

5. 常用绘图选项

\draw[red, thick, dashed, ->] (0,0) -- (2,1);
\draw[blue, ultra thick, dotted] (0,1) -- (2,1);
\draw[green!50!black, rounded corners=3pt] (0,2) rectangle (2,3);

常用选项:

选项作用
draw描边
fill填充
thickthinultra thick线宽
dasheddotted虚线、点线
redbluegreen!50!black颜色
opacity=0.5透明度
rounded corners圆角
rotate=30旋转
scale=1.2缩放
xshift=1cmyshift=0.5cm平移
-><-<->箭头
>=Stealth箭头样式
line width=1pt线宽

颜色混合示例:

\draw[red!50!blue] (0,0) -- (1,0);
\draw[blue!20] (0,0) -- (0,1);

6. 节点 node

节点用于放置文字、标签、框等。

\node at (0,0) {文字};
\node[draw] at (1,0) {带边框};
\node[draw, circle] at (2,0) {A};
\node[draw, rounded corners, fill=blue!10] at (3,0) {圆角框};

6.1 节点位置

\node[above] at (0,0) {上};
\node[below] at (0,0) {下};
\node[left] at (0,0) {左};
\node[right] at (0,0) {右};
\node[above right] at (0,0) {右上};
\node[below left] at (0,0) {左下};

6.2 命名节点

\node[draw, circle] (A) at (0,0) {A};
\node[draw, circle] (B) at (2,0) {B};
\draw[->] (A) -- (B);

6.3 多行文本

需要 align 选项:

\node[draw, align=center] at (0,0) {第一行\\第二行};

6.4 锚点

\node[draw, anchor=west] at (0,0) {左对齐};
\node[draw, anchor=east] at (0,0) {右对齐};

7. 箭头与箭头库

使用 StealthLatex 等箭头样式需要加载 arrows.meta 库,例如:

\usetikzlibrary{arrows.meta}
\draw[->] (0,0) -- (1,0);
\draw[<-] (0,0.5) -- (1,0.5);
\draw[<->] (0,1) -- (1,1);
\draw[-{Stealth[length=3mm]}] (0,1.5) -- (1,1.5);
\draw[->, >=Latex] (0,2) -- (1,2);

8. 样式定义 tikzset

可以统一定义样式:

\tikzset{
myline/.style={draw=blue, thick, ->},
mybox/.style={draw, rounded corners, fill=yellow!20, align=center}
}
\begin{tikzpicture}
\draw[myline] (0,0) -- (2,1);
\node[mybox] at (0,-1) {样式节点};
\end{tikzpicture}

9. 循环 foreach

9.1 画竖线

\foreach \x in {0,1,2,3} {
\draw[gray] (\x,0) -- (\x,2);
}

9.2 画点

\foreach \x in {0,...,5} {
\fill[red] (\x,0) circle (2pt);
}

9.3 带标签

\foreach \x/\y in {0/A, 1/B, 2/C} {
\node[draw, circle] at (\x,0) {\y};
}

9.4 极坐标循环

\foreach \a in {0,30,...,330} {
\fill[blue] (\a:1) circle (1.5pt);
}

10. 坐标计算与定位库

10.1 calc 库

\usetikzlibrary{calc}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,1);
\coordinate (M) at ($ (A)!0.5!(B) $);
\draw (A) -- (B);
\fill[red] (M) circle (2pt);
\end{tikzpicture}

10.2 positioning 库

\usetikzlibrary{positioning}
\begin{tikzpicture}
\node[draw] (A) {A};
\node[draw, right=of A] (B) {B};
\node[draw, below=1cm of A] (C) {C};
\draw[->] (A) -- (B);
\draw[->] (A) -- (C);
\end{tikzpicture}

11. 函数图像

\begin{tikzpicture}[domain=0:4, samples=100]
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-0.2) -- (0,2.5) node[above] {$y$};
\draw[blue, thick] plot (\x, {sin(\x r) + 1})
node[right] {$y=\sin x+1$};
\end{tikzpicture}

注意:TikZ 中三角函数默认使用弧度,sin(\x r) 表示把 \x 转为弧度。

12. 局部作用域 scope

\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\begin{scope}[shift={(2,0)}, rotate=30, red]
\draw (0,0) rectangle (1,1);
\end{scope}
\end{tikzpicture}

13. 常用 TikZ 库

按需加载:

\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{intersections}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usetikzlibrary{matrix}
\usetikzlibrary{chains}
\usetikzlibrary{external}

常见库用途:

  • arrows.meta:箭头样式
  • positioning:节点相对定位
  • calc:坐标计算
  • shapes.geometric:菱形、星形等形状
  • patterns:填充图案
  • decorations.pathreplacing:大括号等装饰
  • fit:让节点包围一组元素
  • backgrounds:背景层
  • matrix:矩阵布局
  • chains:链式节点
  • external:外部化编译,加速大型文档

14. 图案填充

\usetikzlibrary{patterns}
\begin{tikzpicture}
\draw[pattern=north east lines] (0,0) rectangle (2,1);
\draw[pattern=dots, pattern color=blue] (3,0) rectangle (5,1);
\end{tikzpicture}

15. 综合示例:简单流程图

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, shapes.geometric}
\begin{document}
\begin{tikzpicture}[
node distance=1.2cm,
>={Stealth[length=2mm]},
every node/.style={font=\small},
box/.style={
draw,
rounded corners,
fill=blue!10,
minimum width=2cm,
minimum height=0.8cm,
align=center
},
decision/.style={
draw,
diamond,
aspect=2,
fill=orange!15,
align=center
}
]
\node[box] (start) {开始};
\node[decision, below=of start] (check) {条件?};
\node[box, below=of check, fill=green!15] (yes) {执行};
\node[box, right=of check, fill=red!15] (no) {结束};
\draw[->] (start) -- (check);
\draw[->] (check) -- node[left] {是} (yes);
\draw[->] (check) -- node[above] {否} (no);
\end{tikzpicture}
\end{document}

16. 综合示例:坐标轴与函数

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=-3:3, samples=100, scale=0.8]
\draw[->] (-3.2,0) -- (3.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,3.2) node[above] {$y$};
\draw[red, thick] plot (\x, {0.5*\x*\x}) node[right] {$y=0.5x^2$};
\draw[blue, thick] plot (\x, {sin(\x r)}) node[above right] {$y=\sin x$};
\end{tikzpicture}
\end{document}

17. 常见问题与技巧

  1. 报错 Unknown key 或找不到形状时,通常需要加载对应库:

    \usetikzlibrary{arrows.meta}
    \usetikzlibrary{positioning}
    \usetikzlibrary{shapes.geometric}
  2. 节点文字换行需要加:

    align=center

    然后使用 \\ 换行。

  3. 想让图形单独编译,推荐:

    \documentclass[tikz,border=5pt]{standalone}
  4. 调整图形整体大小:

    \begin{tikzpicture}[scale=0.8]
  5. 调整节点间距:

    \begin{tikzpicture}[node distance=1.5cm]
  6. 使用样式让代码更整洁:

    \tikzset{
    box/.style={draw, rounded corners, fill=blue!10},
    arrow/.style={->, thick, >=Stealth}
    }
  7. 调试时可以画网格:

    \draw[help lines, step=0.5] (0,0) grid (4,3);
  8. 中文文档建议使用 xelatex 编译,并加载 ctex

    \documentclass{ctexart}
    \usepackage{tikz}

18. 最小可复制模板

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, calc, shapes.geometric, patterns}
\begin{document}
\begin{tikzpicture}[
>={Stealth[length=2mm]},
thick,
every node/.style={font=\small}
]
\draw[->] (0,0) -- (3,0) node[right] {$x$};
\draw[->] (0,0) -- (0,2) node[above] {$y$};
\draw[blue, fill=blue!10] (1,0.5) circle (0.5);
\node[draw, rounded corners, fill=green!10] at (2,1) {TikZ};
\end{tikzpicture}
\end{document}

掌握以上内容后,就可以开始用 TikZ 画常见的几何图、流程图、坐标图和示意图了。