> For the complete documentation index, see [llms.txt](https://ayakaleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-wen-zhang/26-how-to-draw-vector-graphics-using-tikz-in-latex.md).

# 如何在 LaTeX 中使用 TikZ 绘制矢量图形

**作者：Cody Herring（2013年5月）**

### LaTeX 中的程序化图形：基础

传统上，图形以位图表示，其位深可以从 1 位（二值）到 24 位（真彩色）不等。这使得在传统（矩形）电脑屏幕上简单显示图形成为可能。然而，要生成能准确表示文档或纸上应显示内容的位图，可能既困难又繁琐。

程序化图形通过允许使用更明确、更易理解的系统（例如可缩放矢量图形，SVG）来帮助解决这一问题。通过定义向量、线条和形状，而不是单个像素，可以轻松生成适用于不同尺寸、长宽比、像素密度、打印质量等的图像。本教程将简要介绍使用可移植图形格式和 TikZ 的程序化图形：

* **可移植图形格式** （PGF）是一个基础工具集，可用于生成简单的图形和插图；
* **TikZ** 是 PGF 的扩展，它允许使用大量预先构建的图形。

关于 TikZ 和 PGF 的详尽文档可在 [PGF 手册](http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf).

### 使用 PGF 和 TikZ

要使用 TikZ 宏包，必须先导入它：

```latex
\usepackage{tikz}
```

要绘制图片，必须使用 `tikzpicture` 环境：

```latex
\begin{tikzpicture}
命令写在这里
\end{tikzpicture}
```

### 基础绘图

TikZ 允许绘制许多不同的形状和路径。这里将展示一些基本绘图技术。这些技术可以组合起来形成更复杂的图形。

#### 直线路径

可以使用 `\draw` 命令，按照 X/Y（水平/垂直）坐标系，从一点绘制到另一点，如下所示：

```latex
\begin{tikzpicture}
\draw (5,0) -- (-5, 0);
\end{tikzpicture}
```

多个 `\draw` 调用也可以串联起来。这会在 X 方向从 -5 到 +5、Y 方向从 -2 到 +2 绘制一组坐标轴。

```latex
\begin{tikzpicture}
\draw   (5,0) -- (-5, 0)
        (0,2) -- (0,-2);
\end{tikzpicture}
```

![TikZFigure 1.png](/files/c47118ac24f28e4c1cb7524c3d456750e9bc0fcd)

通过添加额外参数，可以将样式应用到这些线条上。下面是同一图示，但使用了绿色、虚线、超粗线条，并逆时针旋转 15 度：

```latex
\begin{tikzpicture}
\draw[green, dotted, ultra thick, rotate=15]
        (5,0) -- (-5, 0)
        (0,2) -- (0,-2);
\end{tikzpicture}
```

![TiKZFigure 2.png](/files/4b3245d3fa11ff5584d6548d499a88e210f4c279)

#### 改变图示的大小

有时图示的默认大小过大或过小。使用 `scale` 参数在声明 `tikzpicture` 环境时，可以在保持相同比例的同时使用不同大小：

```latex
\begin{tikzpicture}[scale=0.3]
\draw   (5,0) -- (-5, 0)
        (0,2) -- (0,-2);
\end{tikzpicture}
```

下图展示了应用 `[scale=0.3]`:

![TiKZFigure 3.png](/files/e4491854cdaad288e40858c9124751da419dfec2)

#### 添加箭头

箭头对于图表和示意图很有用，而且在 TikZ 图中加入它们很简单。前面演示过的样式都可以使用，也可以使用几种不同的箭头样式。

```latex
\begin{tikzpicture}
\draw[->](0,0) -- (5,0);
\end{tikzpicture}

\begin{tikzpicture}
\draw[<<->>](0,0) -- (5,0);
\end{tikzpicture}

\begin{tikzpicture}
\draw[<<->>, ultra thick, blue](0,0) -- (5,0);
\end{tikzpicture}

\begin{tikzpicture}
\draw[|->>, thick, red](0,0) -- (5,0);
\end{tikzpicture}
```

![TiKZFigure 4.png](/files/910ed770d5cc84eb0d507f2683991199d556a09f)

### 绘制曲线

使用 TikZ 绘制曲线最简单的方法，是指定起点和终点，以及从起点离开和到达终点时所需的角度。

```latex
\begin{tikzpicture}
\draw[ultra thick]
(0,0) to [out=75,in=135](3,4);
\end{tikzpicture}
```

![TiKZFigure 5.png](/files/1a62b6b9f3b20dfc16b60a6b9734037d143636db)

也可以使用链式调用创建复杂得多的图示。PGF 会尝试根据所提供的方向绘制尽可能平滑的线条。

```latex
\begin{tikzpicture}
\draw[ultra thick]
(0,0)
to [out=90,in=180] (2,2)
to [out=270,in=0](0,0)
to [out=0,in=90](2,-2)
to [out=180,in=270](0,0)
to [out=270,in=0](-2,-2)
to [out=90,in=180](0,0)
to [out=180,in=270](-2,2)
to [out=0,in=90](0,0);
\end{tikzpicture}
```

![TIkZFigure 6.png](/files/1f9629b4ce53afa9e166a957cb36700b2e28982b)

### 结论

使用矢量图形可以绘制的内容没有尽头，而它们在支持可缩放、易编辑的图示和图片方面的价值，对寻找简单方式使用图形的 LaTeX 用户来说是一大福音。这篇文章只是触及了可能性的表面，但应当是了解矢量图形以及 TikZ/PGF 环境能力的一个不错入门。

其他资源：

* [TikZ 的最简入门](http://cremeronline.com/LaTeX/minimaltikz.pdf);
* [TikZ 与 PGF 的 CTAN 文档](http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf);
* [Overleaf 学习 Wiki 页面](/latex/zh-cn/tu-xing-he-biao-ge/05-tikz-package.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ayakaleaf-pro.ayaka.space/latex/zh-cn/shen-du-wen-zhang/26-how-to-draw-vector-graphics-using-tikz-in-latex.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
