跳到正文

用 TikZ 绘图

在 tikzpicture 中写路径命令,例如 \draw (0,0) -- (2,1);、\draw (1,1) circle (0.5); 或 \fill[blue] (0,0) rectangle (1,1);,每条命令都以分号结尾。

直线和坐标

用 \usepackage{tikz} 加载 TikZ。绘图写在 tikzpicture 中。\draw 描绘一条路径,-- 用直线连接各点。像 (2,1) 这样的普通坐标以厘米为单位。

折线、闭合三角形和网格

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (5,3);
  \draw[thick] (0,0) -- (1,2) -- (2,1) -- (3,2.5);
  \draw[blue, fill=blue!15] (3.5,0.5) -- (5,0.5) -- (4.25,2) -- cycle;
\end{tikzpicture}

-- cycle 让路径回到起点并闭合,这样填充时就有一个干净的拐角。

图形

路径绘制
(0,0) rectangle (2,1)从一个角到对角的矩形
(1,1) circle (0.5) 或 circle[radius=0.5]以某点为圆心的圆
(1,1) ellipse (1 and 0.5)有两个半径的椭圆
(1,0) arc (0:90:1) 或 arc[start angle=0, end angle=90, radius=1]从当前点开始的圆弧
(0,0) grid (3,2)间隔 1 cm 的网格线(其他间隔用 step=0.5)
(0,0) .. controls (1,1) and (2,1) .. (3,0)贝塞尔曲线
(0,0) to[out=90, in=180] (2,2)按指定角度离开和进入的曲线

矩形、圆、椭圆、圆弧和曲线

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \draw[rounded corners, fill=yellow!30] (0,0) rectangle (2,1.2);
  \draw[red, fill=red!20] (3,0.6) circle (0.6);
  \draw[teal, thick] (5,0.6) ellipse (0.9 and 0.5);
  \draw[->, thick] (7.2,0) arc (0:90:1);
  \draw[purple, thick] (0,-1.5) .. controls (1,0) and (2,-3) .. (3,-1.5);
  \draw[orange, thick] (4,-2) to[out=90, in=180] (6,-1);
\end{tikzpicture}

颜色、线宽和虚线

方括号中的选项设置路径的样式。单独的颜色名设置描边颜色;fill= 填充图形,\fill 只填充不描边,\filldraw 两者兼有。

线宽、虚线样式、颜色和不透明度

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \draw[ultra thin]  (0,2) -- (3,2);
  \draw[thick]       (0,1.6) -- (3,1.6);
  \draw[line width=3pt, blue!60] (0,1.2) -- (3,1.2);
  \draw[dashed]      (0,0.8) -- (3,0.8);
  \draw[dotted, thick] (0,0.4) -- (3,0.4);
  \draw[dash pattern=on 6pt off 2pt on 1pt off 2pt] (0,0) -- (3,0);
  \fill[green!50!black] (4,1) circle (0.5);
  \filldraw[fill=orange, draw=black, opacity=0.6] (4.5,0.6) rectangle (5.5,1.6);
  \draw[<->, red, thick] (4,-0.2) -- (6,-0.2);
\end{tikzpicture}

颜色可以是任意 xcolor 颜色名(red、teal、violet ……),可以是 blue!30!white 这样的混合色,也可以是你用 \definecolor{mine}{HTML}{0072B2} 定义的颜色。

渐变

\shade 用颜色渐变填充图形:线性渐变用 left color 和 right color(或 top color 和 bottom color),径向渐变用 inner color 和 outer color,带光照的球体用 ball color。

线性、径向和球形渐变

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \shade[left color=blue!60, right color=white] (0,0) rectangle (2.5,1);
  \shade[top color=orange, bottom color=yellow] (3,0) rectangle (4,1);
  \shade[inner color=white, outer color=teal] (5.2,0.5) circle (0.5);
  \shade[ball color=red] (6.7,0.5) circle (0.5);
\end{tikzpicture}

用 foreach 重复

\foreach 对列表中的每个值重复执行一条命令。{0,1,...,5} 会自动补全中间的步长。循环变量可以用在坐标、颜色和文字中。

用循环生成的圆和标签

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
\begin{tikzpicture}
  \foreach \i in {0,1,...,5} {
    \draw[fill=blue!\i0] (1.2*\i,0) circle (0.45);
    \node at (1.2*\i,-0.8) {$\i$};
  }
  \foreach \a in {0,45,...,315}
    \draw[->] (9,0) -- ++(\a:0.9);
\end{tikzpicture}

(\a:0.9) 是极坐标:角度和距离。++ 让坐标相对于前一个点。

作用域和变换

scope 把选项应用到其中的所有内容:颜色、线型,以及 shift、rotate、scale、xscale 和 yscale 等变换。

同一个正方形:平移、旋转和缩放

LaTeX导言区: \usepackage{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}
  \begin{scope}[shift={(4,0)}, scale=0.5, blue, thick]
    \draw (0,0) rectangle (1,1);
  \end{scope}
  \begin{scope}[shift={(5.5,0)}, xscale=2, dashed]
    \draw (0,0) rectangle (1,1);
  \end{scope}
\end{tikzpicture}

正文中的小图

\tikz 在段落中绘制一个小图,例如单词旁边的一个彩色圆点。紧接着写出路径,并以分号结尾。

句子中的 \tikz

LaTeX导言区: \usepackage{tikz}在编辑器中打开 (在新标签页中打开)
Status: \tikz \fill[green!60!black] (0,0) circle (3pt); running,
\tikz \fill[red] (0,0) circle (3pt); stopped.

与 TeX 发行版的不同之处

TikZ 手册是 CTAN 上 PGF 的一部分:ctan.org/pkg/pgf。绘制带坐标轴的图表,请使用 pgfplots。

常见问题

如何在 LaTeX 中画圆?

加载 TikZ,写 \begin{tikzpicture} \draw (0,0) circle (1); \end{tikzpicture}。圆括号中的值是以厘米为单位的半径。

TikZ 坐标使用什么单位?

普通数字以厘米为单位。你也可以写明单位,例如 (2pt,1mm) 或 (1in,0)。

如何给图形填充颜色?

用 \fill[blue] ...; 填充且不画轮廓,或用 \draw[fill=blue!20] ...; 同时填充和描边。

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