> 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/tu-biao-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]` 定义一个图形元素，其 *颜色* 是 `灰色` 并具有 `粗` 描边。直线实际上由它的两个端点定义， `(-1,2)` 和 `(2,-4)`，由以下内容连接 `--`.

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

请注意分号 `;` 用于每个命令的末尾 `绘制` 命令。

注意： `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);`**：该点被创建为一个非常小的 `灰色` `圆` 其中心位于 `(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` 而不是 `绘制` 或 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},
]
%节点
\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};

%线条
\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-biao-he-biao-ge/03-lists-of-tables-and-figures.md)
* [图片和表格的位置](/latex/zh-cn/tu-biao-he-biao-ge/02-positioning-images-and-tables.md)
* [在 LaTeX 中直接绘制图表](/latex/zh-cn/tu-biao-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://overleaf-pro.ayaka.space/latex/zh-cn/tu-biao-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.
