tikz
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTikZ/PGF — Vector Graphics & Diagrams
TikZ/PGF — 矢量图形与图表
Setup
配置
latex
\usepackage{tikz}
% Load libraries as needed:
\usetikzlibrary{arrows.meta, calc, positioning, decorations.pathmorphing,
patterns, shapes.geometric, shapes.misc, fit, backgrounds,
automata, trees, mindmap, circuits.logic.US}latex
\usepackage{tikz}
% 按需加载库:
\usetikzlibrary{arrows.meta, calc, positioning, decorations.pathmorphing,
patterns, shapes.geometric, shapes.misc, fit, backgrounds,
automata, trees, mindmap, circuits.logic.US}Basic Drawing
基础绘图
Minimal Example
最简示例
latex
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- cycle; % triangle
\end{tikzpicture}latex
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- cycle; % 三角形
\end{tikzpicture}Path Operations
路径操作
| Operation | Syntax | Example |
|---|---|---|
| Line | | |
| Horizontal-vertical | <code>-|</code> | `(0,0) - |
| Vertical-horizontal | <code>|-</code> | `(0,0) |
| Curve | | |
| Smooth curve | | |
| Arc | | |
| Circle | | |
| Ellipse | | |
| Rectangle | | |
| Grid | | |
| Parabola | | |
| Sin/Cos | | |
| Plot | | |
| Cycle | | close path back to start |
| 操作 | 语法 | 示例 |
|---|---|---|
| 直线 | | |
| 先水平后垂直 | <code>-|</code> | `(0,0) - |
| 先垂直后水平 | <code>|-</code> | `(0,0) |
| 曲线 | | |
| 平滑曲线 | | |
| 弧线 | | |
| 圆形 | | |
| 椭圆 | | |
| 矩形 | | |
| 网格 | | |
| 抛物线 | | |
| 正/余弦曲线 | | |
| 函数绘图 | | |
| 闭合路径 | | 闭合路径回到起点 |
Path Actions
路径操作命令
| Command | Effect |
|---|---|
| Stroke the path |
| Fill the path |
| Fill and stroke |
| Invisible path (for coordinates, nodes) |
| Clip subsequent drawing to path |
| Gradient fill |
| Gradient fill + stroke |
| Shorthand for |
| Shorthand for |
| 命令 | 效果 |
|---|---|
| 描边路径 |
| 填充路径 |
| 先填充再描边 |
| 不可见路径(用于定义坐标、节点) |
| 将后续绘图限制在路径范围内 |
| 渐变填充 |
| 渐变填充+描边 |
| |
| |
Common Draw Options
常用绘图选项
latex
\draw[
color=blue, % or just: blue
line width=1pt, % or: thin, thick, very thick, ultra thick
dashed, % or: dotted, dash dot, dash dot dot
dash pattern={on 3pt off 2pt},
rounded corners=5pt,
-> % arrow tip (see arrows section)
opacity=0.5,
double,
double distance=2pt,
] (0,0) -- (2,2);latex
\draw[
color=blue, % 也可直接写:blue
line width=1pt, % 可选:thin, thick, very thick, ultra thick
dashed, % 可选:dotted, dash dot, dash dot dot
dash pattern={on 3pt off 2pt},
rounded corners=5pt,
-> % 箭头样式(见箭头章节)
opacity=0.5,
double,
double distance=2pt,
] (0,0) -- (2,2);Common Fill Options
常用填充选项
latex
\fill[
fill=blue!20, % 20% blue
fill opacity=0.5,
pattern=north lines, % requires patterns library
pattern color=gray,
] (0,0) rectangle (2,1);latex
\fill[
fill=blue!20, % 20%浓度蓝色
fill opacity=0.5,
pattern=north lines, % 需要引入patterns库
pattern color=gray,
] (0,0) rectangle (2,1);Coordinate Systems
坐标系
latex
% Cartesian (default)
(2, 3)
% Polar (angle:radius)
(45:2cm)
% Relative (shift from last point)
++(1,0) % move reference point
+(1,0) % don't move reference point
% Named
\coordinate (A) at (1,2);
\draw (A) -- (2,3);
% Intersection (calc library)
\usetikzlibrary{calc}
($(A)!0.5!(B)$) % midpoint of A and B
($(A)!0.5!90:(B)$) % midpoint rotated 90°
($(A) + (1,2)$) % A shifted by (1,2)
($(A)!1cm!(B)$) % 1cm from A toward B
% Perpendicular coordinates (A -| B) = (x of B, y of A)
(A |- B) % (x of A, y of B)
% Barycentric
(barycentric cs:A=1,B=1,C=1) % centroidlatex
% 笛卡尔坐标(默认)
(2, 3)
% 极坐标(角度:半径)
(45:2cm)
% 相对坐标(相对于上一个点的偏移)
++(1,0) % 移动参考点
+(1,0) % 不移动参考点
% 命名坐标
\coordinate (A) at (1,2);
\draw (A) -- (2,3);
% 交点计算(需要calc库)
\usetikzlibrary{calc}
($(A)!0.5!(B)$) % A和B的中点
($(A)!0.5!90:(B)$) % 中点绕A旋转90度
($(A) + (1,2)$) % A点偏移(1,2)
($(A)!1cm!(B)$) % 从A向B方向移动1cm的点
% 垂直坐标 (A -| B) = (B的x坐标, A的y坐标)
(A |- B) % (A的x坐标, B的y坐标)
% 重心坐标
(barycentric cs:A=1,B=1,C=1) % 三个点的重心Nodes
节点
Basic Nodes
基础节点
latex
\node (name) at (0,0) {Text};
% Node options
\node[
draw, % draw border
fill=yellow!20,
rectangle, % shape (default)
circle,
ellipse,
rounded corners,
minimum width=2cm,
minimum height=1cm,
inner sep=5pt, % padding
outer sep=2pt, % margin
text=red,
font=\bfseries\small,
align=center, % for multi-line: use \\ in text
text width=3cm,
anchor=north, % positioning anchor
rotate=45,
] (mynode) at (1,2) {Hello\\World};latex
\node (name) at (0,0) {文本内容};
% 节点选项
\node[
draw, % 绘制边框
fill=yellow!20,
rectangle, % 形状(默认)
circle,
ellipse,
rounded corners,
minimum width=2cm,
minimum height=1cm,
inner sep=5pt, % 内边距
outer sep=2pt, % 外边距
text=red,
font=\bfseries\small,
align=center, % 多行文本使用\\换行
text width=3cm,
anchor=north, % 定位锚点
rotate=45,
] (mynode) at (1,2) {你好\\世界};Node Anchors
节点锚点
north west north north east
┌──────────┐
west │ center │ east
└──────────┘
south west south south eastAlso: , , , and angle anchors like , .
basemidtext45135 north west north north east
┌──────────┐
west │ center │ east
└──────────┘
south west south south east其他锚点:、、,以及角度锚点如、等。
basemidtext45135Nodes on Paths
路径上的节点
latex
\draw (0,0) -- node[above] {label} (3,0);
\draw (0,0) -- node[midway, sloped, above] {sloped text} (3,2);
\draw (0,0) -- node[pos=0.3, below] {at 30\%} (3,0);
\draw (0,0) to[out=90,in=0] node[near start, left] {A} (2,2);latex
\draw (0,0) -- node[above] {标签} (3,0);
\draw (0,0) -- node[midway, sloped, above] {倾斜文本} (3,2);
\draw (0,0) -- node[pos=0.3, below] {30%位置} (3,0);
\draw (0,0) to[out=90,in=0] node[near start, left] {A} (2,2);Common Shapes (requires libraries)
常用形状(需要引入对应库)
latex
\usetikzlibrary{shapes.geometric, shapes.misc}
\node[diamond, draw] {D};
\node[star, draw, star points=5] {S};
\node[regular polygon, regular polygon sides=6, draw] {Hex};
\node[trapezium, draw] {T};
\node[cylinder, draw, shape border rotate=90] {DB};
\node[cloud, draw, cloud puffs=10] {Cloud};
\node[cross out, draw] at (0,0) {};
\node[strike out, draw] at (0,0) {};latex
\usetikzlibrary{shapes.geometric, shapes.misc}
\node[diamond, draw] {D};
\node[star, draw, star points=5] {S};
\node[regular polygon, regular polygon sides=6, draw] {六边形};
\node[trapezium, draw] {T};
\node[cylinder, draw, shape border rotate=90] {数据库};
\node[cloud, draw, cloud puffs=10] {云};
\node[cross out, draw] at (0,0) {};
\node[strike out, draw] at (0,0) {};Arrows
箭头
latex
\usetikzlibrary{arrows.meta}
% Arrow tips (arrows.meta syntax)
\draw[->] (0,0) -- (1,0); % default
\draw[-Stealth] (0,0) -- (1,0); % filled triangle
\draw[-Latex] (0,0) -- (1,0); % larger filled
\draw[-{Stealth[length=5mm]}] (0,0) -- (1,0);
\draw[{Latex[red]}-{Latex[blue]}] (0,0) -- (1,0); % colored
\draw[-{>[scale=2]}] (0,0) -- (1,0);
% Common tips: >, Stealth, Latex, To, Circle, Square, |, Hooks
% Modifiers: [length=..], [width=..], [open], [fill=..], [scale=..]latex
\usetikzlibrary{arrows.meta}
% 箭头样式(arrows.meta语法)
\draw[->] (0,0) -- (1,0); % 默认箭头
\draw[-Stealth] (0,0) -- (1,0); % 实心三角形箭头
\draw[-Latex] (0,0) -- (1,0); % 更大的实心箭头
\draw[-{Stealth[length=5mm]}] (0,0) -- (1,0);
\draw[{Latex[red]}-{Latex[blue]}] (0,0) -- (1,0); % 彩色箭头
\draw[-{>[scale=2]}] (0,0) -- (1,0);
% 常用箭头样式:>, Stealth, Latex, To, Circle, Square, |, Hooks
% 修饰参数:[length=..], [width=..], [open], [fill=..], [scale=..]Styles
样式
latex
% Define in preamble or tikzpicture options
\tikzset{
mybox/.style = {draw, fill=blue!20, rounded corners, minimum width=2cm},
myarrow/.style = {-Stealth, thick, red},
highlight/.style = {fill=yellow, draw=orange, line width=2pt},
}
% Use
\node[mybox] {Box};
\draw[myarrow] (0,0) -- (1,1);
% Style with parameters
\tikzset{
box/.style = {draw, fill=#1!20, minimum width=1.5cm},
box/.default = blue,
}
\node[box] {Blue}; % default
\node[box=red] {Red}; % override
% every node/.style applies to all nodes
\begin{tikzpicture}[every node/.style={font=\small}]latex
% 在导言区或tikzpicture选项中定义
\tikzset{
mybox/.style = {draw, fill=blue!20, rounded corners, minimum width=2cm},
myarrow/.style = {-Stealth, thick, red},
highlight/.style = {fill=yellow, draw=orange, line width=2pt},
}
% 使用方式
\node[mybox] {盒子};
\draw[myarrow] (0,0) -- (1,1);
% 带参数的样式
\tikzset{
box/.style = {draw, fill=#1!20, minimum width=1.5cm},
box/.default = blue,
}
\node[box] {蓝色}; % 默认颜色
\node[box=red] {红色}; % 自定义颜色
% every node/.style 应用到所有节点
\begin{tikzpicture}[every node/.style={font=\small}]Foreach Loops
Foreach循环
latex
% Basic
\foreach \x in {0,1,2,3}
\draw (\x, 0) circle (0.3);
% With step
\foreach \x in {0,0.5,...,3}
\fill (\x,0) circle (1pt);
% Multiple variables
\foreach \x/\y in {0/A, 1/B, 2/C}
\node at (\x, 0) {\y};
% Counter
\foreach \x [count=\i] in {a,b,c,d}
\node at (\i, 0) {\x};
% Evaluate
\foreach \x [evaluate=\x as \y using \x*\x] in {1,...,5}
\fill (\x, \y/5) circle (2pt);
% Remember
\foreach \x [remember=\x as \lastx (initially 0)] in {1,...,5}
\draw (\lastx, 0) -- (\x, 0);latex
% 基础用法
\foreach \x in {0,1,2,3}
\draw (\x, 0) circle (0.3);
% 带步长
\foreach \x in {0,0.5,...,3}
\fill (\x,0) circle (1pt);
% 多变量
\foreach \x/\y in {0/A, 1/B, 2/C}
\node at (\x, 0) {\y};
% 计数器
\foreach \x [count=\i] in {a,b,c,d}
\node at (\i, 0) {\x};
% 表达式计算
\foreach \x [evaluate=\x as \y using \x*\x] in {1,...,5}
\fill (\x, \y/5) circle (2pt);
% 记忆上一个值
\foreach \x [remember=\x as \lastx (initially 0)] in {1,...,5}
\draw (\lastx, 0) -- (\x, 0);Scopes and Transformations
作用域与变换
latex
\begin{tikzpicture}
\draw (0,0) -- (1,0);
\begin{scope}[shift={(2,0)}, rotate=45, scale=0.5, red, thick]
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
\end{scope}
% Transformations
% shift={(x,y)}, xshift=1cm, yshift=2cm
% rotate=45, rotate around={45:(1,1)}
% scale=2, xscale=2, yscale=0.5
% xslant=0.5, yslant=0.5
\end{tikzpicture}latex
\begin{tikzpicture}
\draw (0,0) -- (1,0);
\begin{scope}[shift={(2,0)}, rotate=45, scale=0.5, red, thick]
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
\end{scope}
% 变换选项
% shift={(x,y)}, xshift=1cm, yshift=2cm
% rotate=45, rotate around={45:(1,1)}
% scale=2, xscale=2, yscale=0.5
% xslant=0.5, yslant=0.5
\end{tikzpicture}Clipping
裁剪
latex
\begin{tikzpicture}
\clip (0,0) circle (1.5cm);
% Everything below is clipped to the circle
\fill[blue!30] (-2,-2) rectangle (2,2);
\draw[step=0.5, gray] (-2,-2) grid (2,2);
\end{tikzpicture}latex
\begin{tikzpicture}
\clip (0,0) circle (1.5cm);
% 后续所有绘图都会被裁剪到圆形范围内
\fill[blue!30] (-2,-2) rectangle (2,2);
\draw[step=0.5, gray] (-2,-2) grid (2,2);
\end{tikzpicture}Layers (backgrounds library)
图层(需要backgrounds库)
latex
\usetikzlibrary{backgrounds}
\begin{tikzpicture}
\node[draw, fill=white] (A) {Foreground};
\begin{pgfonlayer}{background}
\fill[yellow!30] (A.south west) rectangle (A.north east);
\end{pgfonlayer}
\end{tikzpicture}latex
\usetikzlibrary{backgrounds}
\begin{tikzpicture}
\node[draw, fill=white] (A) {前景内容};
\begin{pgfonlayer}{background}
\fill[yellow!30] (A.south west) rectangle (A.north east);
\end{pgfonlayer}
\end{tikzpicture}Pics
可复用图形(Pic)
latex
\tikzset{
myshape/.pic = {
\draw (-0.5,-0.5) rectangle (0.5,0.5);
\draw (0,0) circle (0.3);
}
}
\begin{tikzpicture}
\pic at (0,0) {myshape};
\pic[rotate=45, scale=1.5] at (2,0) {myshape};
\end{tikzpicture}latex
\tikzset{
myshape/.pic = {
\draw (-0.5,-0.5) rectangle (0.5,0.5);
\draw (0,0) circle (0.3);
}
}
\begin{tikzpicture}
\pic at (0,0) {myshape};
\pic[rotate=45, scale=1.5] at (2,0) {myshape};
\end{tikzpicture}Decorations
装饰效果
latex
\usetikzlibrary{decorations.pathmorphing, decorations.markings, decorations.text}
% Wavy/zigzag lines
\draw[decorate, decoration={zigzag, amplitude=2mm, segment length=5mm}]
(0,0) -- (4,0);
\draw[decorate, decoration={snake, amplitude=1mm}]
(0,0) -- (4,0);
\draw[decorate, decoration={coil, aspect=0.5}]
(0,0) -- (4,0);
\draw[decorate, decoration={random steps, segment length=3mm}]
(0,0) -- (4,0);
% Brace
\draw[decorate, decoration={brace, amplitude=5pt}]
(0,0) -- (3,0) node[midway, above=5pt] {label};
% Text along path
\draw[decorate, decoration={text along path, text={Hello World}}]
(0,0) .. controls (1,1) .. (3,0);
% Markings (arrows along path)
\draw[decoration={markings, mark=at position 0.5 with {\arrow{>}}},
postaction={decorate}]
(0,0) -- (3,0);latex
\usetikzlibrary{decorations.pathmorphing, decorations.markings, decorations.text}
% 波浪/锯齿线
\draw[decorate, decoration={zigzag, amplitude=2mm, segment length=5mm}]
(0,0) -- (4,0);
\draw[decorate, decoration={snake, amplitude=1mm}]
(0,0) -- (4,0);
\draw[decorate, decoration={coil, aspect=0.5}]
(0,0) -- (4,0);
\draw[decorate, decoration={random steps, segment length=3mm}]
(0,0) -- (4,0);
% 大括号
\draw[decorate, decoration={brace, amplitude=5pt}]
(0,0) -- (3,0) node[midway, above=5pt] {标签};
% 沿路径排布的文本
\draw[decorate, decoration={text along path, text={Hello World}}]
(0,0) .. controls (1,1) .. (3,0);
% 路径标记(沿路径添加箭头)
\draw[decoration={markings, mark=at position 0.5 with {\arrow{>}}},
postaction={decorate}]
(0,0) -- (3,0);Patterns
填充图案
latex
\usetikzlibrary{patterns}
\fill[pattern=north east lines] (0,0) rectangle (2,1);
\fill[pattern=crosshatch dots, pattern color=blue] (0,0) circle (1);
% Available: north east lines, north west lines, horizontal lines,
% vertical lines, crosshatch, dots, crosshatch dots,
% fivepointed stars, sixpointed stars, bricks, checkerboardlatex
\usetikzlibrary{patterns}
\fill[pattern=north east lines] (0,0) rectangle (2,1);
\fill[pattern=crosshatch dots, pattern color=blue] (0,0) circle (1);
% 可用图案:north east lines, north west lines, horizontal lines,
% vertical lines, crosshatch, dots, crosshatch dots,
% fivepointed stars, sixpointed stars, bricks, checkerboardCommon Diagram Patterns
常用图表模板
Flowchart
流程图
latex
\usetikzlibrary{arrows.meta, shapes.geometric, positioning}
\tikzset{
startstop/.style = {rectangle, rounded corners, draw, fill=red!20,
minimum width=3cm, minimum height=1cm},
process/.style = {rectangle, draw, fill=blue!20,
minimum width=3cm, minimum height=1cm},
decision/.style = {diamond, draw, fill=green!20,
minimum width=3cm, minimum height=1cm, aspect=1.5},
io/.style = {trapezium, trapezium left angle=70, trapezium right angle=110,
draw, fill=orange!20, minimum width=3cm, minimum height=1cm},
arrow/.style = {thick, -Stealth},
}
\begin{tikzpicture}[node distance=1.5cm]
\node[startstop] (start) {Start};
\node[io, below=of start] (input) {Input $x$};
\node[decision, below=of input] (decide) {$x > 0$?};
\node[process, below left=of decide] (neg) {$y = -x$};
\node[process, below right=of decide] (pos) {$y = x$};
\node[io, below=2cm of decide] (output) {Output $y$};
\node[startstop, below=of output] (stop) {Stop};
\draw[arrow] (start) -- (input);
\draw[arrow] (input) -- (decide);
\draw[arrow] (decide) -| node[near start, above] {No} (neg);
\draw[arrow] (decide) -| node[near start, above] {Yes} (pos);
\draw[arrow] (neg) |- (output);
\draw[arrow] (pos) |- (output);
\draw[arrow] (output) -- (stop);
\end{tikzpicture}latex
\usetikzlibrary{arrows.meta, shapes.geometric, positioning}
\tikzset{
startstop/.style = {rectangle, rounded corners, draw, fill=red!20,
minimum width=3cm, minimum height=1cm},
process/.style = {rectangle, draw, fill=blue!20,
minimum width=3cm, minimum height=1cm},
decision/.style = {diamond, draw, fill=green!20,
minimum width=3cm, minimum height=1cm, aspect=1.5},
io/.style = {trapezium, trapezium left angle=70, trapezium right angle=110,
draw, fill=orange!20, minimum width=3cm, minimum height=1cm},
arrow/.style = {thick, -Stealth},
}
\begin{tikzpicture}[node distance=1.5cm]
\node[startstop] (start) {开始};
\node[io, below=of start] (input) {输入 $x$};
\node[decision, below=of input] (decide) {$x > 0$?};
\node[process, below left=of decide] (neg) {$y = -x$};
\node[process, below right=of decide] (pos) {$y = x$};
\node[io, below=2cm of decide] (output) {输出 $y$};
\node[startstop, below=of output] (stop) {结束};
\draw[arrow] (start) -- (input);
\draw[arrow] (input) -- (decide);
\draw[arrow] (decide) -| node[near start, above] {否} (neg);
\draw[arrow] (decide) -| node[near start, above] {是} (pos);
\draw[arrow] (neg) |- (output);
\draw[arrow] (pos) |- (output);
\draw[arrow] (output) -- (stop);
\end{tikzpicture}Tree
树状图
latex
\begin{tikzpicture}[
level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm},
every node/.style={draw, circle, minimum size=8mm},
edge from parent/.style={draw, -Stealth},
]
\node {1}
child { node {2}
child { node {4} }
child { node {5} }
}
child { node {3}
child { node {6} }
child { node {7} }
};
\end{tikzpicture}latex
\begin{tikzpicture}[
level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm},
every node/.style={draw, circle, minimum size=8mm},
edge from parent/.style={draw, -Stealth},
]
\node {1}
child { node {2}
child { node {4} }
child { node {5} }
}
child { node {3}
child { node {6} }
child { node {7} }
};
\end{tikzpicture}Graph / Network
图/网络
latex
\usetikzlibrary{positioning}
\begin{tikzpicture}[
vertex/.style={draw, circle, fill=blue!20, minimum size=8mm},
edge/.style={thick},
>=Stealth,
]
\node[vertex] (a) at (0,2) {A};
\node[vertex] (b) at (2,3) {B};
\node[vertex] (c) at (4,2) {C};
\node[vertex] (d) at (2,0) {D};
\draw[edge] (a) -- node[above left] {3} (b);
\draw[edge] (b) -- node[above right] {2} (c);
\draw[edge] (a) -- node[below left] {7} (d);
\draw[edge] (c) -- node[below right] {1} (d);
\draw[edge] (b) -- node[right] {4} (d);
\end{tikzpicture}latex
\usetikzlibrary{positioning}
\begin{tikzpicture}[
vertex/.style={draw, circle, fill=blue!20, minimum size=8mm},
edge/.style={thick},
>=Stealth,
]
\node[vertex] (a) at (0,2) {A};
\node[vertex] (b) at (2,3) {B};
\node[vertex] (c) at (4,2) {C};
\node[vertex] (d) at (2,0) {D};
\draw[edge] (a) -- node[above left] {3} (b);
\draw[edge] (b) -- node[above right] {2} (c);
\draw[edge] (a) -- node[below left] {7} (d);
\draw[edge] (c) -- node[below right] {1} (d);
\draw[edge] (b) -- node[right] {4} (d);
\end{tikzpicture}Finite Automaton / State Machine
有限自动机/状态机
latex
\usetikzlibrary{automata, positioning}
\begin{tikzpicture}[shorten >=1pt, node distance=2.5cm, on grid, auto,
every state/.style={draw, minimum size=1cm}]
\node[state, initial] (q0) {$q_0$};
\node[state, right=of q0] (q1) {$q_1$};
\node[state, accepting, right=of q1] (q2) {$q_2$};
\path[->]
(q0) edge node {a} (q1)
(q0) edge [loop above] node {b} ()
(q1) edge node {a,b} (q2)
(q1) edge [bend left] node {b} (q0)
(q2) edge [loop above] node {a} ();
\end{tikzpicture}latex
\usetikzlibrary{automata, positioning}
\begin{tikzpicture}[shorten >=1pt, node distance=2.5cm, on grid, auto,
every state/.style={draw, minimum size=1cm}]
\node[state, initial] (q0) {$q_0$};
\node[state, right=of q0] (q1) {$q_1$};
\node[state, accepting, right=of q1] (q2) {$q_2$};
\path[->]
(q0) edge node {a} (q1)
(q0) edge [loop above] node {b} ()
(q1) edge node {a,b} (q2)
(q1) edge [bend left] node {b} (q0)
(q2) edge [loop above] node {a} ();
\end{tikzpicture}Mind Map
思维导图
latex
\usetikzlibrary{mindmap}
\begin{tikzpicture}[mindmap, grow cyclic,
every node/.style={concept, execute at begin node=\hskip0pt},
concept color=blue!40,
level 1/.append style={level distance=4cm, sibling angle=90},
level 2/.append style={level distance=2.5cm, sibling angle=45},
]
\node {Main Topic}
child[concept color=red!40] { node {Sub 1}
child { node {Detail A} }
child { node {Detail B} }
}
child[concept color=green!40] { node {Sub 2}
child { node {Detail C} }
}
child[concept color=orange!40] { node {Sub 3} }
child[concept color=purple!40] { node {Sub 4} };
\end{tikzpicture}latex
\usetikzlibrary{mindmap}
\begin{tikzpicture}[mindmap, grow cyclic,
every node/.style={concept, execute at begin node=\hskip0pt},
concept color=blue!40,
level 1/.append style={level distance=4cm, sibling angle=90},
level 2/.append style={level distance=2.5cm, sibling angle=45},
]
\node {主主题}
child[concept color=red!40] { node {子主题1}
child { node {细节A} }
child { node {细节B} }
}
child[concept color=green!40] { node {子主题2}
child { node {细节C} }
}
child[concept color=orange!40] { node {子主题3} }
child[concept color=purple!40] { node {子主题4} };
\end{tikzpicture}Block Diagram / System Diagram
框图/系统图
latex
\usetikzlibrary{positioning, arrows.meta, fit}
\begin{tikzpicture}[
block/.style={draw, rectangle, fill=blue!10, minimum height=1cm,
minimum width=2cm, align=center},
sum/.style={draw, circle, minimum size=6mm},
>=Stealth, thick,
]
\node[sum] (sum) {};
\node[block, right=1.5cm of sum] (ctrl) {Controller\\$G_c(s)$};
\node[block, right=1.5cm of ctrl] (plant) {Plant\\$G(s)$};
\node[block, below=1cm of plant] (fb) {Sensor\\$H(s)$};
\draw[->] ++(-1.5,0) -- node[above] {$R(s)$} (sum);
\draw[->] (sum) -- node[above] {$E(s)$} (ctrl);
\draw[->] (ctrl) -- (plant);
\draw[->] (plant) -- ++(2,0) node[above left] {$Y(s)$};
\draw[->] (plant.east) ++(1,0) |- (fb);
\draw[->] (fb) -| (sum) node[near end, left] {$-$};
\node at (sum) {$\Sigma$};
\end{tikzpicture>latex
\usetikzlibrary{positioning, arrows.meta, fit}
\begin{tikzpicture}[
block/.style={draw, rectangle, fill=blue!10, minimum height=1cm,
minimum width=2cm, align=center},
sum/.style={draw, circle, minimum size=6mm},
>=Stealth, thick,
]
\node[sum] (sum) {};
\node[block, right=1.5cm of sum] (ctrl) {控制器\\$G_c(s)$};
\node[block, right=1.5cm of ctrl] (plant) {被控对象\\$G(s)$};
\node[block, below=1cm of plant] (fb) {传感器\\$H(s)$};
\draw[->] ++(-1.5,0) -- node[above] {$R(s)$} (sum);
\draw[->] (sum) -- node[above] {$E(s)$} (ctrl);
\draw[->] (ctrl) -- (plant);
\draw[->] (plant) -- ++(2,0) node[above left] {$Y(s)$};
\draw[->] (plant.east) ++(1,0) |- (fb);
\draw[->] (fb) -| (sum) node[near end, left] {$-$};
\node at (sum) {$\Sigma$};
\end{tikzpicture>Venn Diagram
韦恩图
latex
\begin{tikzpicture}
\fill[red!20] (0,0) circle (1.5cm);
\fill[blue!20] (1.5,0) circle (1.5cm);
\begin{scope}
\clip (0,0) circle (1.5cm);
\fill[purple!30] (1.5,0) circle (1.5cm);
\end{scope}
\draw (0,0) circle (1.5cm) node[left=1cm] {$A$};
\draw (1.5,0) circle (1.5cm) node[right=1cm] {$B$};
\node at (0.75, 0) {$A \cap B$};
\end{tikzpicture}latex
\begin{tikzpicture}
\fill[red!20] (0,0) circle (1.5cm);
\fill[blue!20] (1.5,0) circle (1.5cm);
\begin{scope}
\clip (0,0) circle (1.5cm);
\fill[purple!30] (1.5,0) circle (1.5cm);
\end{scope}
\draw (0,0) circle (1.5cm) node[left=1cm] {$A$};
\draw (1.5,0) circle (1.5cm) node[right=1cm] {$B$};
\node at (0.75, 0) {$A \cap B$};
\end{tikzpicture}Timeline
时间线
latex
\begin{tikzpicture}
\draw[thick, -Stealth] (0,0) -- (10,0);
\foreach \x/\year/\event in {
1/2020/Founded,
3/2021/v1.0,
5.5/2022/IPO,
8/2023/Global%
}{
\draw (\x, -0.15) -- (\x, 0.15);
\node[above=3pt] at (\x, 0.15) {\footnotesize \year};
\node[below=3pt, align=center, text width=1.5cm] at (\x, -0.15) {\footnotesize \event};
}
\end{tikzpicture}latex
\begin{tikzpicture}
\draw[thick, -Stealth] (0,0) -- (10,0);
\foreach \x/\year/\event in {
1/2020/成立,
3/2021/v1.0发布,
5.5/2022/上市,
8/2023/全球化%
}{
\draw (\x, -0.15) -- (\x, 0.15);
\node[above=3pt] at (\x, 0.15) {\footnotesize \year};
\node[below=3pt, align=center, text width=1.5cm] at (\x, -0.15) {\footnotesize \event};
}
\end{tikzpicture}Circuit (Logic Gates)
电路(逻辑门)
latex
\usetikzlibrary{circuits.logic.US}
\begin{tikzpicture}[circuit logic US, every circuit symbol/.style={thick}]
\node[and gate] (and1) at (2,1) {};
\node[or gate] (or1) at (4,0.5) {};
\draw (0,1.25) node[left] {$A$} -- (and1.input 1);
\draw (0,0.75) node[left] {$B$} -- (and1.input 2);
\draw (and1.output) -- (or1.input 1);
\draw (0,0) node[left] {$C$} -- (or1.input 2);
\draw (or1.output) -- ++(1,0) node[right] {$Y$};
\end{tikzpicture}latex
\usetikzlibrary{circuits.logic.US}
\begin{tikzpicture}[circuit logic US, every circuit symbol/.style={thick}]
\node[and gate] (and1) at (2,1) {};
\node[or gate] (or1) at (4,0.5) {};
\draw (0,1.25) node[left] {$A$} -- (and1.input 1);
\draw (0,0.75) node[left] {$B$} -- (and1.input 2);
\draw (and1.output) -- (or1.input 1);
\draw (0,0) node[left] {$C$} -- (or1.input 2);
\draw (or1.output) -- ++(1,0) node[right] {$Y$};
\end{tikzpicture}Commutative Diagram (Math)
交换图(数学用)
latex
\usetikzlibrary{positioning}
\begin{tikzpicture}[>=Stealth, node distance=2cm]
\node (A) {$A$};
\node (B) [right=of A] {$B$};
\node (C) [below=of A] {$C$};
\node (D) [right=of C] {$D$};
\draw[->] (A) -- node[above] {$f$} (B);
\draw[->] (A) -- node[left] {$g$} (C);
\draw[->] (B) -- node[right] {$h$} (D);
\draw[->] (C) -- node[below] {$k$} (D);
\end{tikzpicture}latex
\usetikzlibrary{positioning}
\begin{tikzpicture}[>=Stealth, node distance=2cm]
\node (A) {$A$};
\node (B) [right=of A] {$B$};
\node (C) [below=of A] {$C$};
\node (D) [right=of C] {$D$};
\draw[->] (A) -- node[above] {$f$} (B);
\draw[->] (A) -- node[left] {$g$} (C);
\draw[->] (B) -- node[right] {$h$} (D);
\draw[->] (C) -- node[below] {$k$} (D);
\end{tikzpicture}Plot (built-in, no pgfplots)
内置绘图(无需pgfplots)
latex
\begin{tikzpicture}[scale=0.8]
\draw[->] (-0.5,0) -- (4.5,0) node[right] {$x$};
\draw[->] (0,-0.5) -- (0,3.5) node[above] {$y$};
\draw[blue, thick, domain=0:4, samples=100]
plot (\x, {sqrt(\x)});
\foreach \x in {1,...,4}
\draw (\x, 0.1) -- (\x, -0.1) node[below] {\x};
\foreach \y in {1,2,3}
\draw (0.1, \y) -- (-0.1, \y) node[left] {\y};
\end{tikzpicture}latex
\begin{tikzpicture}[scale=0.8]
\draw[->] (-0.5,0) -- (4.5,0) node[right] {$x$};
\draw[->] (0,-0.5) -- (0,3.5) node[above] {$y$};
\draw[blue, thick, domain=0:4, samples=100]
plot (\x, {sqrt(\x)});
\foreach \x in {1,...,4}
\draw (\x, 0.1) -- (\x, -0.1) node[below] {\x};
\foreach \y in {1,2,3}
\draw (0.1, \y) -- (-0.1, \y) node[left] {\y};
\end{tikzpicture}Positioning Library
Positioning库
latex
\usetikzlibrary{positioning}
% Relative positioning (preferred over manual coordinates)
\node[right=of A] (B) {B}; % default separation
\node[right=1.5cm of A] (B) {B}; % custom distance
\node[above right=1cm and 2cm of A] (B) {B}; % separate x/y
% Available: above, below, left, right,
% above left, above right, below left, below rightlatex
\usetikzlibrary{positioning}
% 相对定位(优于手动写坐标)
\node[right=of A] (B) {B}; % 默认间距
\node[right=1.5cm of A] (B) {B}; % 自定义间距
\node[above right=1cm and 2cm of A] (B) {B}; % 分别设置x/y间距
% 可用位置:above, below, left, right,
% above left, above right, below left, below rightFit Library
Fit库
latex
\usetikzlibrary{fit}
% Draw a box around multiple nodes
\node[draw, dashed, fit=(A) (B) (C), inner sep=5pt, label=above:Group] {};latex
\usetikzlibrary{fit}
% 绘制包含多个节点的外框
\node[draw, dashed, fit=(A) (B) (C), inner sep=5pt, label=above:分组] {};Calc Library Key Operations
Calc库核心操作
latex
\usetikzlibrary{calc}
% Midpoint
($(A)!0.5!(B)$)
% Point 1cm from A toward B
($(A)!1cm!(B)$)
% Projection of C onto line AB
($(A)!(C)!(B)$)
% Rotated point: midpoint of AB rotated 90° around A
($(A)!0.5!90:(B)$)
% Vector addition
($(A) + (1,2)$)
% Scalar multiply
($2*(A) - (B)$)
% let syntax for complex calculations
\draw let \p1 = ($(B)-(A)$),
\n1 = {veclen(\x1,\y1)}
in (A) circle (\n1);latex
\usetikzlibrary{calc}
% 中点
($(A)!0.5!(B)$)
% 从A向B方向移动1cm的点
($(A)!1cm!(B)$)
% C点在直线AB上的投影
($(A)!(C)!(B)$)
% 旋转点:AB的中点绕A旋转90度
($(A)!0.5!90:(B)$)
% 向量相加
($(A) + (1,2)$)
% 标量乘法
($2*(A) - (B)$)
% let语法用于复杂计算
\draw let \p1 = ($(B)-(A)$),
\n1 = {veclen(\x1,\y1)}
in (A) circle (\n1);Externalizing (Caching) Figures
图形外部化(缓存)
latex
% In preamble — compiles each tikzpicture once, reuses PDF
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz-cache/]
% To skip a figure:
\tikzexternaldisable
\begin{tikzpicture} ... \end{tikzpicture}
\tikzexternalenableRequires flag:
--shell-escapepdflatex --shell-escape main.texlatex
% 在导言区配置——每个tikzpicture只编译一次,复用生成的PDF
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz-cache/]
% 跳过某个图形的缓存:
\tikzexternaldisable
\begin{tikzpicture} ... \end{tikzpicture}
\tikzexternalenable需要编译参数:
--shell-escapepdflatex --shell-escape main.texCommon Pitfalls
常见问题
| Problem | Cause | Fix |
|---|---|---|
| "Dimension too large" | Coordinates too big | Use |
| Missing arrow tips | Forgot | |
| Node text cut off | Node too small | Add |
| Forgot library | |
| Nodes overlap | Using | Use |
| Special chars in body | Wrap body in |
| Path not closing | Forgot | End path with |
| Slow compilation | Complex figures | Use |
| Math in nodes | | Use |
| Colors not found | Missing xcolor | TikZ loads xcolor; use |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| "Dimension too large" | 坐标数值太大 | 使用 |
| 箭头不显示 | 忘记引入 | 添加 |
| 节点文本被截断 | 节点尺寸太小 | 添加 |
| 忘记引入库 | 添加 |
| 节点重叠 | 使用 | 搭配positioning库使用 |
| 循环体包含特殊字符 | 将循环体用 |
| 路径无法闭合 | 忘记加 | 在路径末尾添加 |
| 编译速度慢 | 图形复杂度高 | 使用 |
| 节点中数学公式失效 | | 正常使用 |
| 颜色无法识别 | 缺少xcolor库 | TikZ会自动加载xcolor;如需额外颜色可在引入tikz前添加 |
Tips
使用技巧
- Use library — never hardcode coordinates for node layouts
positioning - in preamble for reusable styles across figures
\tikzset - ,
every node/.stylefor consistent appearanceevery path/.style - Use to limit option scope
\begin{scope} - +
name path=X(intersections library) to find crossing pointsname intersections - For standalone figures:
\documentclass[tikz]{standalone} - library for magnification insets
spy - for data-driven diagrams (see pgfplots skill)
pgfplotstable
- 优先使用库,不要硬编码节点布局的坐标
positioning - 在导言区使用定义可复用的样式,统一全文档图形风格
\tikzset - 使用、
every node/.style实现全局统一外观every path/.style - 使用限制样式的生效范围
\begin{scope} - 结合和
name path=X(intersections库)可计算路径交点name intersections - 生成独立图形可使用文档类:
\documentclass[tikz]{standalone} - 库可用于添加局部放大区域
spy - 数据驱动的图表可使用(见pgfplots相关技能)
pgfplotstable