搜索
File 2026.09.16编程

LaTeX TikZ 基础使用笔记

Date / 日期
Updated / 更新
2026.09.16
Reading / 阅读
257 Min
Words / 字数
51317
Category / 分类
编程Latex
Tags / 标签
#编程#Latex#Tikz

LaTeX TikZ 基础使用笔记

· 更新于 2026年9月16日· 约 257 分钟· 51317 字

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

引入 TikZ

最小示例:

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

如果写在普通文档中:

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

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

tikzpicture 环境

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

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

也可以放在 figure 环境中:

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

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

坐标系与坐标写法

直角坐标

LaTeX
\draw (0,0) -- (2,1);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
TikZ 源码
\draw (0,0) -- (2,1);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;

极坐标

格式:(角度:半径)

LaTeX
\draw (0,0) -- (45:2);
\draw (0,0) -- (90:1.5);
TikZ 源码
\draw (0,0) -- (45:2);
\draw (0,0) -- (90:1.5);

相对坐标

LaTeX
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;
TikZ 源码
\draw (0,0) -- ++(1,0) -- ++(0,1) -- ++(-1,0) -- cycle;

说明:

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

对比:

LaTeX
\draw (0,0) -- +(1,0) -- +(0,1);
\draw (2,0) -- ++(1,0) -- ++(0,1);
TikZ 源码
\draw (0,0) -- +(1,0) -- +(0,1);
\draw (2,0) -- ++(1,0) -- ++(0,1);

命名坐标

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

基本绘图命令

直线、折线、闭合路径

LaTeX
\draw (0,0) -- (2,0);
\draw (0,0) -- (1,1) -- (2,0);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
TikZ 源码
\draw (0,0) -- (2,0);
\draw (0,0) -- (1,1) -- (2,0);
\draw (0,0) -- (1,0) -- (1,1) -- cycle;

矩形

LaTeX
\draw (0,0) rectangle (2,1);
TikZ 源码
\draw (0,0) rectangle (2,1);

圆和椭圆

LaTeX
\draw (0,0) circle (1);
\draw (0,0) ellipse (2 and 1);
TikZ 源码
\draw (0,0) circle (1);
\draw (0,0) ellipse (2 and 1);

圆弧

LaTeX
\draw (0,0) arc (0:90:1);
\draw (0,0) arc (0:180:2 and 1);
TikZ 源码
\draw (0,0) arc (0:90:1);
\draw (0,0) arc (0:180:2 and 1);

曲线

LaTeX
\draw (0,0) .. controls (1,0) and (1,1) .. (2,1);
\draw (0,0) parabola (1,1);
\draw (0,0) sin (1,1);
TikZ 源码
\draw (0,0) .. controls (1,0) and (1,1) .. (2,1);
\draw (0,0) parabola (1,1);
\draw (0,0) sin (1,1);

填充与描边

LaTeX
\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);
TikZ 源码
\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);

常用绘图选项

LaTeX
\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);
TikZ 源码
\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填充
thick、thin、ultra thick线宽
dashed、dotted虚线、点线
red、blue、green!50!black颜色
opacity=0.5透明度
rounded corners圆角
rotate=30旋转
scale=1.2缩放
xshift=1cm、yshift=0.5cm平移
->、<-、<->箭头
>=Stealth箭头样式
line width=1pt线宽

颜色混合示例:

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

节点 node

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

LaTeX
\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) {圆角框};
TikZ 源码
\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) {圆角框};

节点位置

LaTeX
\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) {左下};
TikZ 源码
\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) {左下};

命名节点

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

多行文本

需要 align 选项:

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

锚点

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

箭头与箭头库

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

LaTeX
\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);
TikZ 源码
\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);

样式定义 tikzset

可以统一定义样式:

LaTeX
\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}

循环 foreach

画竖线

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

画点

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

带标签

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

极坐标循环

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

坐标计算与定位库

calc 库

LaTeX
\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}
TikZ 源码
\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}

positioning 库

LaTeX
\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}
TikZ 源码
\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}

函数图像

LaTeX
\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 源码
\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 转为弧度。

局部作用域 scope

LaTeX
\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}
TikZ 源码
\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}

常用 TikZ 库

按需加载:

LaTeX
\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:外部化编译,加速大型文档

图案填充

LaTeX
\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}
TikZ 源码
\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}

综合示例:简单流程图

LaTeX
\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}
TikZ 源码
\usetikzlibrary{arrows.meta, positioning, shapes.geometric}
\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}

综合示例:坐标轴与函数

LaTeX
\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}
TikZ 源码
\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}

常见问题与技巧

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

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

    LaTeX
    align=center

    然后使用 \\ 换行。

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

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

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

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

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

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

    LaTeX
    \documentclass{ctexart}
    \usepackage{tikz}

最小可复制模板

LaTeX
\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 源码
\usetikzlibrary{arrows.meta, positioning, calc, shapes.geometric, patterns}
\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}

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

Comment评论

添加新评论