7. 绘图功能 #
7.1 绘图语言简介 #
LaTeX 的原始 picture 环境能绘制基本图形,但功能有限。可用的扩展工具包括:
| 工具 | 特点 |
|---|---|
| PSTricks | 基于 PostScript,使用时要特别留意编译流程。 |
| TikZ/PGF | 用路径和结点描述图形,适用于多种 LaTeX 引擎。 |
| METAPOST | 独立的绘图语言,可生成图形后插入文档。 |
| Asymptote | 支持程序化绘图和三维图形,也有独立编译流程。 |
本章聚焦 TikZ 的基本用法,不替代完整的 PGF/TikZ 手册。它适合结构图、示意图和少量函数图像;大量数据图表通常应交给专门的绘图工具,再导入 PDF 或图片。
7.2 TikZ 绘图语言 #
下面是一个完整文档:
\documentclass[UTF8,fontset=fandol]{ctexart}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (1,1) -- (2,0) -- cycle;
\end{tikzpicture}
\end{document}
后续片段默认已加载 tikz。每条绘图命令通常用半角分号结束,坐标使用半角逗号和括号。tikzpicture 不会自动成为浮动图,需要时放入 figure 并添加标题。
行内小图可用 \tikz{\draw (0,0) circle[radius=0.7ex];};较复杂图形使用独立环境更清晰。
7.2.1 TikZ 坐标和路径 #
直角坐标写作 (x,y),单位省略时默认按厘米坐标理解;极坐标写作 (角度:半径),角度单位为度。也可明确使用 (10mm,20pt)。
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (30:2);
\draw (A) -- (B);
\end{tikzpicture}
\coordinate 给位置命名。(A -| B) 取 B 的横坐标、A 的纵坐标,(A |- B) 则相反;这与连接两点的直角路径语法有关,但不是同一层面的用法。
| 路径语法 | 作用 |
|---|---|
(A) -- (B) |
直线。 |
(A) -| (B)、(A) |- (B) |
先水平后垂直、先垂直后水平。 |
-- cycle |
回到路径起点,闭合图形。 |
rectangle |
两个对角点确定矩形。 |
circle[radius=...] |
圆。 |
ellipse[x radius=...,y radius=...] |
椭圆。 |
arc[start angle=...,end angle=...,radius=...] |
从当前点绘制圆弧。 |
.. controls ... and ... .. |
Bézier 曲线。 |
下面对比直线、折线和曲线:
\begin{tikzpicture}
\draw (0,0) -- (1.5,1);
\node[above] at (0.75,1.15) {直线};
\draw (2.5,0) -| (4,1);
\node[above] at (3.25,1.15) {直角路径};
\draw[blue,thick] (5,0)
.. controls (5.3,1.4) and (6.2,1.4) .. (6.5,0);
\node[above] at (5.75,1.15) {曲线};
\end{tikzpicture}
圆弧的当前点是弧的起点,不是圆心。需要验证复杂路径时,可以先画控制点和辅助线。
sin、cos 绘制四分之一周期的正弦、余弦片段;parabola bend 指定抛物线顶点。函数图像和网格的例子:
\begin{tikzpicture}
\draw[help lines,step=0.5] (-1,-1) grid (1,1);
\draw[->] (-1.5,0) -- (1.5,0);
\draw[->] (0,-1.5) -- (0,1.5);
\draw[domain=-1:1,samples=60,blue]
plot (\x,{2*\x*\x-1});
\end{tikzpicture}
domain 指定取值范围,samples 控制采样点数。表达式中的逗号、运算等可能影响解析,适当用花括号包裹。
7.2.2 TikZ 绘图命令和参数 #
\draw 描边,\fill 填充,\filldraw 同时填充和描边。填充区域应使用明确闭合的路径:
\begin{tikzpicture}
\draw[blue,thick] (0,0) rectangle (1.5,1);
\filldraw[draw=red,fill=yellow!30,line width=0.8pt]
(3,0.5) circle[radius=0.5cm];
\end{tikzpicture}
常见选项:
| 类别 | 例子 |
|---|---|
| 颜色 | draw=blue、fill=gray!20、text=black。 |
| 粗细 | thin、thick、very thick;指定数值用 line width=0.8pt。 |
| 线型 | solid、dashed、dotted、dash dot。 |
| 箭头 | ->、<-、<->。 |
| 转角 | rounded corners=3pt、sharp corners。 |
| 变换 | scale=1.2、xshift=1cm、rotate=30。 |
具体线宽用 line width,不要写成 thick=0.8pt。高级箭头可通过 \usetikzlibrary{arrows.meta} 加载。
样式把一组参数取名,方便复用:
\begin{tikzpicture}[
myarrow/.style={blue,thick,->}
]
\draw[myarrow] (0,0) -- (2,1);
\draw[myarrow,dashed] (0,0) -- (2,0);
\end{tikzpicture}
环境选项影响整个图,单条命令上的选项只影响当前路径。scope 将变换和样式限制在局部:
\begin{tikzpicture}
\draw (0,0) rectangle (2,1);
\begin{scope}[xshift=3cm,rotate=20]
\draw[blue] (0,0) rectangle (2,1);
\end{scope}
\end{tikzpicture}
坐标缩放通常不会同时缩放结点文字;需要整体缩放结点形状和文字时,才使用 transform shape。
7.2.3 TikZ 文字结点 #
结点可以包含文字或数学公式:
\begin{tikzpicture}
\node[draw,rounded corners] (input) at (0,0) {输入};
\node[draw,rounded corners] (output) at (3,0) {输出};
\draw[->] (input) -- node[above] {处理} (output);
\end{tikzpicture}
(input) 是名称,不会作为文字输出。连接命名结点时,线段通常停在结点边界,而不是穿过文字。
| 选项 | 作用 |
|---|---|
anchor=south |
把结点南侧锚点放在指定坐标上。 |
above、below、left、right |
将文字放在坐标的相应侧。 |
circle、rectangle |
结点形状。 |
inner sep、outer sep |
内部留白、外部间隔。 |
minimum width、minimum height |
最小尺寸。 |
font=\bfseries、text=blue |
字体和文字颜色。 |
(input.east)、(output.west) 可以指定东西侧锚点。text width 与 align 可用于多行结点;额外形状需加载相应 TikZ 库。
下面的积分示意图综合使用坐标投影、曲线、填充和公式结点:
\begin{tikzpicture}
\draw[->] (-0.5,0) -- (4.5,0);
\draw[->] (0,-0.5) -- (0,3);
\coordinate (a) at (0.5,1.9);
\coordinate (b) at (4,1.2);
\coordinate (a0) at (a |- 0,0);
\coordinate (b0) at (b |- 0,0);
\filldraw[fill=blue!10,draw=blue!60!black,thick]
(a0) -- (a)
.. controls (1,2.8) and (2.7,0.4) .. (b)
-- (b0) -- cycle;
\node[below] at (a0) {$a$};
\node[below] at (b0) {$b$};
\node[above,fill=green!10,rounded corners,inner sep=6pt]
at (2.4,2.5)
{$\displaystyle\int_a^b f(x)\,\mathrm{d}x=F(b)-F(a)$};
\end{tikzpicture}
7.2.4 在 TikZ 中使用循环 #
\foreach 对列表中的每个值执行一组命令:
\begin{tikzpicture}
\draw (0,0) -- (5,0);
\foreach \x in {0,0.1,...,5}
\draw[very thin] (\x,0) -- (\x,0.15);
\foreach \x in {0,1,...,5}
\draw (\x,0) -- (\x,0.3) node[above] {\x};
\end{tikzpicture}
变量对可以同时提供位置和标签:
\begin{tikzpicture}
\foreach \x/\name in {0/A,1/B,2/C}
\node[circle,draw,fill=blue!10] at (\x,0) {\name};
\end{tikzpicture}
重复图形尽量写成循环或样式。缺分号、拼错环境名、把半角符号写成全角,都是 TikZ 入门时常见的错误。