> 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-tw/tu-biao-yu-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]{交點};
\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/a68ae415c51a8c0d7a69df9be63fc30be9765c5e)

在這個範例中，畫出了兩條線和一個點。若要新增一條線，可使用命令 `\draw[gray, thick]` 定義一個圖形元素，其 *顏色* 是 `gray` 以及具有 `粗` 的線條。這條線其實是由兩個端點定義的， `(-1,2)` 以及 `(2,-4)`，並由 `--`.

該 *點* 其實是一個 `圓形` 由以下命令繪製： `\filldraw[black]`，這個命令不僅會畫出圓形，還會以黑色填滿它。在這個命令中，圓心 `(0,0)` 與半徑 `(2pt)` 被指定。點旁邊有一個 `節點`，它其實是一個包含文字的方框 `交點`，並錨定在該點的 `西側` 。

要注意分號 `;` 用在每個 `draw 指令` 來完成的。

注意： `tikzfigure` 環境可以包在 figure 或類似環境中。請參閱 [插入圖片](/latex/zh-tw/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/bdc4f709d38beb3262ecc26fff6ebb9960579c4d)

這個範例中有三個基本命令：

* **`\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/e8e9bf7969f54499c67d5ec272fbc73c6dbf077e)

* **`\filldraw[color=red!60, fill=red!5, very thick](-1,0) circle (1.5);`**：這個命令在前一節中用來繪製 *點*，但在這個例子中，方括號內還有一些額外參數。以下說明：
  * **`color=red!60`**：圓周外框的顏色設定為 60% 紅色（比「純」紅更淺）。請參閱 [參考指南](#reference-guide) 以查看 LaTeX 可用的預設顏色清單；另請參閱 [在 LaTeX 中使用顏色](/latex/zh-tw/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/6a7b52144e3e2971b74ea24324c6d4c9854eabc7)

* **`\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/006f2269caf207b4e0b5c302c1c9d898aa5bcf3e)

這個圖中基本上有三個命令：一個節點定義、一個節點宣告，以及連接兩個節點的線條。

* **`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` 套件中可用的顏色與粗細參數：

| 參數 | 值                       | 圖片                                                                   |
| -- | ----------------------- | -------------------------------------------------------------------- |
| 顏色 | 白色、黑色、紅色、綠色、藍色、青色、洋紅、黃色 | ![BasicColours.png](/files/e9b779ba74359b4135c16a702801d3a947243850) |
| 粗細 | 極細、很細、細、粗、很粗、極粗         | ![Thickness.png](/files/75d5d6f14f2a27dd9f2be003bccecdc3814c9138)    |

你的 LaTeX 發行版中可能還有更多顏色。請參閱 [在 LaTeX 中使用顏色](/latex/zh-tw/ge-shi-hua/13-using-colors-in-latex.md)

## 延伸閱讀

更多資訊請參見：

* [在 LaTeX 中使用顏色](/latex/zh-tw/ge-shi-hua/13-using-colors-in-latex.md)
* [Pgfplots 套件](/latex/zh-tw/te-ding-ling-yu/08-pgfplots-package.md)
* [插入圖片](/latex/zh-tw/geng-duo-zhu-ti/27-inserting-images.md)
* [表格與圖形清單](/latex/zh-tw/tu-biao-yu-biao-ge/03-lists-of-tables-and-figures.md)
* [圖片與表格的定位](/latex/zh-tw/tu-biao-yu-biao-ge/02-positioning-images-and-tables.md)
* [直接在 LaTeX 中繪製圖表](/latex/zh-tw/tu-biao-yu-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-tw/tu-biao-yu-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.
