> 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/tu-xing-he-biao-ge/05-tikz-package.md).

# TikZ 宏包

## 引言

[Ti*k*Z](https://en.wikipedia.org/wiki/PGF/TikZ) 可能是 LaTeX 中用于创建图形元素最复杂、最强大的工具。本文从一个简单示例开始，介绍一些基本概念：绘制线条、点、曲线、圆形、矩形等。

首先，加载 `tikz` 宏包，只需加入以下代码行 `\usepackage{tikz}` 到文档导言区，然后使用以下命令绘制图形： `tikzpicture` 环境中。

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[gray, thick] (-1,2) -- (2,-4);
\draw[gray, thick] (-1,-1) -- (2,2);
\filldraw[black] (0,0) circle (2pt) node[anchor=west]{Intersection point};
\end{tikzpicture}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=First+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cdraw%5Bgray%2C+thick%5D+%28-1%2C2%29+--+%282%2C-4%29%3B%0A%5Cdraw%5Bgray%2C+thick%5D+%28-1%2C-1%29+--+%282%2C2%29%3B%0A%5Cfilldraw%5Bblack%5D+%280%2C0%29+circle+%282pt%29+node%5Banchor%3Dwest%5D%7BIntersection+point%7D%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![OLV2tikz1.png](/files/4dd0dd09e1deb3f1bccb1b8737d32a791eb6bad8)

在这个示例中，绘制了两条线和一个点。要添加一条线，使用命令 `\draw[gray, thick]` 定义一个图形元素，其 *颜色* 是 `gray` 并且使用 `粗` 线宽。实际上，这条线由两个端点定义， `(-1,2)` 和 `(2,-4)`，由 `--`.

该 *点* 实际上是一个 `圆` 由 `\filldraw[black]`，此命令不仅会画出圆，还会用黑色填充它。在此命令中，中心点 `(0,0)` 和半径 `(2pt)` 被指定。点旁边是一个 `节点`，它实际上是一个包含文本的框 `交点`，并锚定在该点的 `西侧` 。

需要注意分号 `;` 用于每个 `draw` 该命令。

注意： `tikzfigure` 环境可以嵌套在 figure 或类似环境中。请参阅 [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md) 一文以了解有关此主题的更多信息。

## 基本元素：点、线和路径

在本节中，我们提供一些示例，展示如何创建一些基本图形元素，这些元素可以组合起来创建更复杂的图形。

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\draw (-2,0) -- (2,0);
\filldraw [gray] (0,0) circle (2pt);
\draw (-2,-2) .. controls (0,0) .. (2,-2);
\draw (-2,2) .. controls (-1,0) and (1,0) .. (2,2);

\end{tikzpicture}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Second+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%0A%5Cdraw+%28-2%2C0%29+--+%282%2C0%29%3B%0A%5Cfilldraw+%5Bgray%5D+%280%2C0%29+circle+%282pt%29%3B%0A%5Cdraw+%28-2%2C-2%29+..+controls+%280%2C0%29+..+%282%2C-2%29%3B%0A%5Cdraw+%28-2%2C2%29+..+controls+%28-1%2C0%29+and+%281%2C0%29+..+%282%2C2%29%3B%0A%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![OLV2tikz2.png](/files/5a70d88935b53a77aadd3c496a7cba2aafdbe514)

在这个示例中有三个基本命令：

* **`\draw (-2,0) -- (2,0);`**：这定义了一条线，其端点是 `(-2,0)` 和 `(2,0)`.
* **`\filldraw [gray] (0,0) circle (2pt);`**：该点被创建为一个非常小的 `gray` `圆` ，中心位于 `(0,0)` ，其半径为 `(2pt)`。 `\filldraw` 命令用于绘制元素并用特定颜色填充它们。更多示例见下一节。
* **`\draw (-2,2) .. controls (-1,0) and (1,0) .. (2,2);`**：绘制一个 [贝塞尔曲线](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)。它由 4 个点定义： `(-2,2)` 和 `(2,2)` 是它的端点， `(-1,0)` 和 `(1,0)` 已 [*控制点*](https://en.wikipedia.org/wiki/Control_point_\(mathematics\)) ，它们决定其“弯曲程度”。你可以把这两个点看作“吸引点”。

## 基本几何形状：圆、椭圆和多边形

几何图形可以由更简单的元素构建，因此我们先从圆、椭圆和弧开始。

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[color=red!60, fill=red!5, very thick](-1,0) circle (1.5);
\fill[blue!50] (2.5,0) ellipse (1.5 and 0.5);
\draw[ultra thick, ->] (6.5,0) arc (0:220:1);
\end{tikzpicture}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Third+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cfilldraw%5Bcolor%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%5D%28-1%2C0%29+circle+%281.5%29%3B%0A%5Cfill%5Bblue%2150%5D+%282.5%2C0%29+ellipse+%281.5+and+0.5%29%3B%0A%5Cdraw%5Bultra+thick%2C+-%3E%5D+%286.5%2C0%29+arc+%280%3A220%3A1%29%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![TikzEx3.png](/files/0712e3e48d4eed244a222f9a8bc1eb95ebce8a46)

* **`\filldraw[color=red!60, fill=red!5, very thick](-1,0) circle (1.5);`**：该命令在上一节中用于绘制一个 *点*，但在此例中，方括号内还有一些额外参数。下面将对其进行说明：
  * **`color=red!60`**：圆周围环的颜色设置为 60% 红色（比“纯”红色更浅）。请参阅 [参考指南](#reference-guide) 以获取 LaTeX 中可用的默认颜色列表；另请参阅 [在 LaTeX 中使用颜色](/latex/zh-cn/ge-shi-hua/13-using-colors-in-latex.md) ，了解如何创建你自己的颜色。
  * **`fill=red!5`**：圆被填充为更浅的红色。
  * **`很粗`**：此参数定义线条粗细。请参阅 [参考指南](#reference-guide) 以查看完整的取值列表。
* **`\fill[blue!50] (2.5,0) ellipse (1.5 and 0.5);`**：要创建椭圆，你需要提供一个中心点 `(2.5,0)`，以及两个半径：水平和垂直（`1.5` 和 `0.5` ，分别对应）。另请注意命令 `fill` 而不是 `draw` 或 filldraw，因为在这种情况下，不需要控制外部和内部颜色。
* **`\draw[ultra thick, ->] (6.5,0) arc (0:220:1);`**：此命令将绘制一段弧线，起始于 `(6.5,0)`。额外参数 `->` 表示弧线末端带有一个箭头。除了起始点之外，你还必须提供三个额外值：起始角、终止角和半径；这里，这三个参数值采用以下格式提供 `(0:220:1)`.

除了曲线几何形状外，你也可以使用类似的语法创建使用直线的元素：

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[blue, very thick] (0,0) rectangle (3,2);
\draw[orange, ultra thick] (4,0) -- (6,0) -- (5.7,2) -- cycle;
\end{tikzpicture}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Fourth+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cdraw%5Bblue%2C+very+thick%5D+%280%2C0%29+rectangle+%283%2C2%29%3B%0A%5Cdraw%5Borange%2C+ultra+thick%5D+%284%2C0%29+--+%286%2C0%29+--+%285.7%2C2%29+--+cycle%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![OLV2tikz4.png](/files/fb0e333f6e3b3e65db8a7a4a6dbf2f1b35645861)

* **`\draw[blue, very thick] (0,0) rectangle (3,2);`**：矩形由特殊命令 `rectangle`。你必须提供两个点，第一个是“铅笔”开始绘制矩形的位置，第二个是对角的另一角点。
* **`\draw[orange, ultra thick] (4,0) -- (6,0) -- (5.7,2) -- cycle;`**：要绘制多边形，我们绘制一条 *闭合的* 由直线组成的路径：一条从 `(4,0)` 更改为 `(6,0)` 和一条从 `(6,0)` 更改为 `(5.7,2)`。 `cycle` 指令表示起点和终点应重合，从而创建一个“闭合”路径（形状），这将生成最后一段线段。

## 图示

节点可能是 TikZ 中用途最广泛的元素。我们已经在导言中使用过一个节点——为图形添加一些文本。下一个示例使用节点来创建一个图示。

```latex
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm},
squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick, minimum size=5mm},
]
%Nodes
\node[squarednode]      (maintopic)                              {2};
\node[roundnode]        (uppercircle)       [above=of maintopic] {1};
\node[squarednode]      (rightsquare)       [right=of maintopic] {3};
\node[roundnode]        (lowercircle)       [below=of maintopic] {4};

%Lines
\draw[->] (uppercircle.south) -- (maintopic.north);
\draw[->] (maintopic.east) -- (rightsquare.west);
\draw[->] (rightsquare.south) .. controls +(down:7mm) and +(right:7mm) .. (lowercircle.east);
\end{tikzpicture}
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=More+advanced+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cusetikzlibrary%7Bpositioning%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%5B%0Aroundnode%2F.style%3D%7Bcircle%2C+draw%3Dgreen%2160%2C+fill%3Dgreen%215%2C+very+thick%2C+minimum+size%3D7mm%7D%2C%0Asquarednode%2F.style%3D%7Brectangle%2C+draw%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%2C+minimum+size%3D5mm%7D%2C%0A%5D%0A%25Nodes%0A%5Cnode%5Bsquarednode%5D++++++%28maintopic%29++++++++++++++++++++++++++++++%7B2%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28uppercircle%29+++++++%5Babove%3Dof+maintopic%5D+%7B1%7D%3B%0A%5Cnode%5Bsquarednode%5D++++++%28rightsquare%29+++++++%5Bright%3Dof+maintopic%5D+%7B3%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28lowercircle%29+++++++%5Bbelow%3Dof+maintopic%5D+%7B4%7D%3B%0A%0A%25Lines%0A%5Cdraw%5B-%3E%5D+%28uppercircle.south%29+--+%28maintopic.north%29%3B%0A%5Cdraw%5B-%3E%5D+%28maintopic.east%29+--+%28rightsquare.west%29%3B%0A%5Cdraw%5B-%3E%5D+%28rightsquare.south%29+..+controls+%2B%28down%3A7mm%29+and+%2B%28right%3A7mm%29+..+%28lowercircle.east%29%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此示例生成如下输出：

![OLV2tikz5.png](/files/dc60347079ab41d7f99fe03e5f000996eb3c68e9)

这个图中本质上有三个命令：一个节点定义、一个节点声明，以及连接两个节点的线。

* **`roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm}`**：作为参数传给 `tikzpicture` 环境。它定义了一个节点，该节点将被引用为 `roundnode`：这个节点将是一个圆，其外圈将使用颜色 `green!60` 绘制，并使用 `green!5`进行填充。描边将为 `很粗` ，其 `最小尺寸` 是 `7mm`。下面这一行定义了第二个矩形节点，称为 `squarednode`，使用类似的参数。
* **`\node[squarednode] (maintopic) {2};`**：这将创建一个 `squarednode`，如前一个命令所定义。该节点的id 将为 `maintopic` ，并包含数字 `2`。如果在花括号中留空，将不会显示任何文本。
* **`[above=of maintopic]`**：请注意，除第一个节点外，其他所有节点都有一个额外参数，用于确定它相对于其他节点的位置。例如， `[above=of maintopic]` 表示该节点应出现在名为 `maintopic`的节点上方。要使此定位系统生效，你必须在导言区添加 `\usetikzlibrary{positioning}` 。如果没有 `positioning` 宏包，你可以使用语法 `above of=maintopic` 代替，但 `positioning` 这种语法更灵活且更强大：你可以扩展它，写成 `above=**3cm** of maintopic` 即控制与 `maintopic`.
* **`\draw[->] (uppercircle.south) -- (maintopic.north);`**：将绘制一条类似箭头的直线。其语法已在 [基本元素](#basic-elements-points-lines-and-paths) 一节中解释过。唯一的不同是我们书写线段端点的方式：通过引用一个节点（这就是我们给它们命名的原因）以及相对于该节点的位置。

## 参考指南

在以下内容中可能使用的颜色和粗细参数 `tikz` 宏包的示例代码：

| 参数 | 取值                      | picture                                                              |
| -- | ----------------------- | -------------------------------------------------------------------- |
| 颜色 | 白色、黑色、红色、绿色、蓝色、青色、品红、黄色 | ![BasicColours.png](/files/86019131fa63a3b03f41efd8eb1b88213c15b09e) |
| 粗细 | 超细、很细、细、粗、很粗、超粗         | ![Thickness.png](/files/a81629b2a59ec4544a496b1ab05262433822658c)    |

你的 LaTeX 发行版中可能还提供更多颜色。请参阅 [在 LaTeX 中使用颜色](/latex/zh-cn/ge-shi-hua/13-using-colors-in-latex.md)

## 进一步阅读

更多信息请参见：

* [在 LaTeX 中使用颜色](/latex/zh-cn/ge-shi-hua/13-using-colors-in-latex.md)
* [Pgfplots 宏包](/latex/zh-cn/te-ding-ling-yu/08-pgfplots-package.md)
* [插入图片](/latex/zh-cn/geng-duo-zhu-ti/27-inserting-images.md)
* [表格和图形列表](/latex/zh-cn/tu-xing-he-biao-ge/03-lists-of-tables-and-figures.md)
* [图片和表格的定位](/latex/zh-cn/tu-xing-he-biao-ge/02-positioning-images-and-tables.md)
* [在 LaTeX 中直接绘制图表](/latex/zh-cn/tu-xing-he-biao-ge/04-picture-environment.md)
* [《TikZ 和 PGF 宏包手册》](http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf)
* [TeXample.net 上的 TikZ 和 PGF 示例](http://www.texample.net/tikz/examples/all/)


---

# 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/tu-xing-he-biao-ge/05-tikz-package.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.
