> For the complete documentation index, see [llms.txt](https://overleaf-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://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-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)

#### 更改图形大小

有时图形的默认大小过大或过小。使用 `缩放` 参数来声明 `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);
* [CTAN 上关于 TikZ 和 PGF 的文档](http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf);
* [Overleaf 的学习 Wiki 页面](/latex/zh-cn/tu-biao-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://overleaf-pro.ayaka.space/latex/zh-cn/shen-ru-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.
