> 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/shen-ru-wen-zhang/26-how-to-draw-vector-graphics-using-tikz-in-latex.md).

# 如何在 LaTeX 中使用 TikZ 繪製向量圖形

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

### LaTeX 中的程序式圖形：基礎

傳統上，圖形會以點陣圖表示，位元數可從 1 位元（黑白）到 24 位元（真彩色）不等。這使得在傳統（矩形）電腦螢幕上簡單顯示圖形成為可能。然而，要產生能準確表示文件或紙上內容的點陣圖，可能既困難又繁瑣。

程序式圖形可藉由讓更明確且更容易理解的系統（例如可縮放向量圖形，Scalable Vector Graphics，SVG）來協助達成這一點。透過定義向量、線條與形狀，而不是單獨的像素，就能輕鬆為多種尺寸、長寬比、像素密度、列印品質等產生圖像。本教學將使用 Portable Graphics Format 與 TikZ 簡要介紹程序式圖形：

* **Portable Graphics Format** （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/293efa169f0cf20d7835cc91b78b46056a7131a7)

只要加入額外參數，就可以將樣式套用到這些線條上。以下是同一張圖，但改為綠色、點狀、超粗的線條，並逆時針旋轉 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/c201989a551acc62c179ddda0799275a88f6fefc)

#### 變更圖表大小

有時候，圖表的預設大小太大或太小。使用 `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/772edc714c67906a571e7b330df5938c82d7c250)

#### 加入箭頭端點

箭頭對於圖表與示意圖很有用，而且在 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/7a43cdf206f0ebbf9210586160d3ed749d3233c3)

### 繪製曲線

使用 TikZ 繪製曲線最簡單的方法，是指定起點與終點，以及從起點離開和到達終點時所需的角度。

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

![TiKZFigure 5.png](/files/0d1ddd3630ce4cd4289de27fc6a2e2c668d40292)

也可以使用串接呼叫建立更複雜的圖表。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/0cf1c3cfb0081c4052ef265da3920e589107a306)

### 結論

向量圖形能繪製的內容沒有盡頭，而它們能夠提供可縮放、易於編輯的圖表與圖片，對於尋找簡單方式使用圖形的 LaTeX 使用者來說是一大福音。這篇文章只觸及了可能性的表面，但應該足以作為向量圖形與 TikZ/PGF 環境能力的良好入門。

其他資源：

* [TikZ 入門簡介](http://cremeronline.com/LaTeX/minimaltikz.pdf);
* [CTAN 上的 TikZ 與 PGF 文件](http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf);
* [Overleaf 的 learn Wiki 頁面](/latex/zh-tw/tu-biao-yu-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-tw/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.
