跳到正文

TikZ 节点、箭头和示意图

用 \node[draw] (name) {text}; 放置每个方框,用 positioning 库(right=of name)放置后续方框,再用 \draw[->] (a) -- (b); 把它们连接起来。

节点

节点是位于某一点的一段文字,可以带边框。draw 给它加上轮廓,fill 加上背景,circle 或 ellipse 让它变成圆形(默认是 rectangle)。圆括号中的名称让你之后可以引用这个节点。

不同形状、填充和大小的节点

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \node[draw] (a) at (0,0) {plain box};
  \node[draw, rounded corners, fill=blue!10] (b) at (3,0) {rounded};
  \node[draw, circle, fill=yellow!40] (c) at (5.5,0) {44};
  \node[draw, ellipse] (d) at (8,0) {ellipse};
  \node[draw, minimum width=2.5cm, minimum height=1cm] at (1,-1.6) {fixed size};
  \node[draw, text width=2.6cm, align=center] at (5,-1.6) {text that wraps onto two lines};
\end{tikzpicture}

节点之间的箭头

\draw[->] (a) -- (b); 画一个从节点 a 指向节点 b 的箭头。线条在节点的边框处开始和结束,而不是在中心。-| 和 |- 画出直角连线。

用 positioning 库绘制的小流程图

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[
    node distance=1.2cm and 1.6cm,
    box/.style={draw, rounded corners, fill=blue!8, minimum width=2.2cm, minimum height=0.8cm},
    >={Stealth},
  ]
  \node[box] (in) {Read data};
  \node[box, right=of in] (clean) {Clean};
  \node[box, right=of clean] (fit) {Fit model};
  \node[draw, circle, below=of clean] (ok) {OK?};
  \draw[->] (in) -- (clean);
  \draw[->] (clean) -- (fit);
  \draw[->] (fit) |- (ok);
  \draw[->] (ok) -| node[pos=0.75, left] {no} (in);
\end{tikzpicture}
\end{document}

right=of in 把节点放在 in 的右侧,间隔为 node distance。其他方向有 left、above、below 以及 above right 等对角方向。right=2cm of in 为单个节点设置距离。

箭头和节点上的标签

写在路径中间的 node 会给路径加上标签。above、below、left、right 设置标签在哪一侧,midway、near start、near end 或 pos=0.3 设置它沿路径的位置。label=below:text 给节点加一个标签。

带弯曲且有标签的连线的状态图

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta}
\begin{document}
\begin{tikzpicture}[state/.style={draw, circle, minimum size=1cm}, >={Stealth}, node distance=2.4cm]
  \node[state, label=below:start] (s0) {$q_0$};
  \node[state, right=of s0] (s1) {$q_1$};
  \node[state, right=of s1, fill=green!15] (s2) {$q_2$};
  \draw[->] (s0) to[bend left] node[above] {$a$} (s1);
  \draw[->] (s1) to[bend left] node[below] {$b$} (s0);
  \draw[->] (s1) -- node[above] {$a$} (s2);
\end{tikzpicture}
\end{document}

锚点

每个节点的边框上都有锚点:north、south、east、west 以及 north east 等角点。(box.north) 是 box 顶部中央的点。

从节点锚点出发的箭头

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \node[draw, minimum width=3cm, minimum height=1cm] (box) {Server};
  \draw[->] (box.north) -- ++(0,0.7) node[above] {north};
  \draw[->] (box.east) -- ++(1,0) node[right] {east};
  \draw[->] (box.south west) -- ++(-0.6,-0.6) node[below left] {south west};
  \node[anchor=west] at (box.south east) {\small (anchor=west)};
\end{tikzpicture}

节点上的 anchor=west 会让节点的 west 锚点位于给定的点上。

箭头样式

选项箭头在 Scissor LaTeX 中
->、<-、<->TikZ 默认箭头支持
-stealth、-latex经典的 stealth 和 LaTeX 箭头支持
-{Stealth}、-{Latex}(arrows.meta)较新的箭头支持
|-|两端各一条竖线支持
图形上的 >={Stealth}-> 所用的箭头支持
箭头选项,如 -{Stealth[length=3mm]}箭头的大小和形状部分支持:箭头以默认大小绘制

箭头大小与 TikZ 接近,但不完全相同。

不支持的功能

以下功能在 TeX 发行版中能编译,但在这里不行。大多数会在“问题”标签页中报告。

功能在 Scissor LaTeX 中的结果替代写法
\matrix(以及 matrix of nodes)整个图形变成带提示的虚线框用 at (x,y) 或 positioning 放置节点
\pic整个图形变成带提示的虚线框在带 shift 的 scope 中重复绘制
装饰(decorate、decoration=...)会被忽略并给出提示;路径按普通样式绘制普通路径或虚线路径
shapes.geometric 中的节点形状(diamond、trapezium ……)画成矩形,并给出提示rectangle、circle 或 ellipse
double 双线会被忽略并给出提示两个节点或两条路径
to[loop above](节点上的自环)不画自环,也没有提示;其标签落在节点上从节点边框出发的 arc 或 .. controls .. 曲线
pin=显示 pin 的文字,但没有引线label=
-| 或 |- 路径上的 pos=标签只沿第二段放置,所以 pos=0.25 会落在拐角附近用 pos=0.75 标注第二段,或把路径拆成两段
tikz-cd、circuitikz、forest图形会被略去,并给出提示简单的 TikZ 或图片

\matrix 变成占位框

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \matrix[draw] {
    \node {a}; & \node {b}; \\
    \node {c}; & \node {d}; \\
  };
\end{tikzpicture}

“问题”标签页: \matrix in a picture is not supported yet

菱形节点被画成矩形

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
  \node[draw, diamond] at (0,0) {Yes?};
  \node[draw, ellipse] at (2.5,0) {ellipse works};
\end{tikzpicture}
\end{document}

“问题”标签页: TikZ node option 'diamond' is not supported yet

TikZ 手册(位于 PGF 宏包中)介绍了节点、positioning 库和箭头样式:ctan.org/pkg/pgf。图形和路径请参阅用 TikZ 绘图。

另请参阅 TikZ 基础、TikZ 和图和图片。

常见问题

如何在 LaTeX 中绘制流程图?

用带 draw 的 TikZ 节点画方框,用 positioning 库(right=of a、below=of b)放置它们,再用 \draw[->] (a) -- (b); 画箭头。

如何在 TikZ 中把文字放在箭头上?

在路径中间写一个节点:\draw[->] (a) -- node[above] {text} (b);。

tikz-cd 在 Scissor LaTeX 中能用吗?

暂时不能。tikzcd 图会被略去并给出提示。请改用节点和箭头绘制简单的示意图。

本页的每个示例都在构建网站时由 Scissor LaTeX 排版。 最后更新:2026-11-07