> 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/wen-yu-da/60-i-have-a-lot-of-tikz-matlab2tikz-or-pgfplots-figures-so-i-m-getting-a-compilation-timeout.-can-i.md).

# 我有很多 tikz、matlab2tikz 或 pgfplots 圖形，所以編譯時會逾時。我可以 e

## 如何將 TikZ 圖形外部化

使用 TikZ [名為 `external`](https://tikz.dev/library-external) 來建立並儲存（快取）TikZ 繪圖為 PDF 檔案——這個過程通常稱為「*外部化*」。

請透過以下方式載入該函式庫： `\usetikzlibrary{external}`，然後使用指令啟用外部化： `\tikzexternalize`:

```latex
\usetikzlibrary{external}
\tikzexternalize[<options>]
```

其中 `<options>` 是一個可選的參數清單（鍵值對），用於設定 `external` 該函式庫。

* **注意**：若要讓 TikZ 外部化在 Overleaf 上運作，則 `\tikzexternalize` 指令 ***必須*** 寫在專案的主 `.tex` 檔案中使用的指令。

我們來看一個基本範例，使用一個取自 [TikZ 文件](https://tikz.dev/library-external):

```latex
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz/]

\begin{document}
% 這個 TikZ 圖形將會寫入
% 名為 'output-figure0.pdf' 的檔案
% 位於 'tikz' 資料夾中
\begin{tikzpicture}

  \node {root}
    child {node {left}}
    child {node {right}
      child {node {child}}
      child {node {child}}
    };

\end{tikzpicture}

\end{document}
```

若要探索這個範例，您可以 [在 Overleaf 上開啟它。](https://www.overleaf.com/project/new/template/27519?id=1174669946\&templateName=Example+to+externalize+a+TikZ+graphic\&latexEngine=pdflatex\&texImage=texlive-full%3A2022.1\&mainFile=)

我們範例中的程式碼使用了選項 `prefix=tikz/` 與 `\tikzexternalize`:

```latex
\tikzexternalize[prefix=tikz/]
```

此選項 `prefix=tikz/` 提供一個資料夾名稱（此處為 `tikz`）用於快取已產生（外部化）的圖形——您需要在專案中手動建立該資料夾。不過，在 LaTeX（TikZ 程式碼）能寫入該資料夾之前，該資料夾必須包含一個檔案——任何（虛擬）檔案都可以；例如，您可以新增一個空白文字檔 `foo.txt` 在 `tikz` 資料夾中。

Overleaf 會將外部化的 TikZ PDF 圖形視為已產生的輸出檔案，因此它們不會出現在專案可見的檔案清單中——它們確實存在於專案內，只是被隱藏了——您可以查看 [產生的檔案](/latex/zh-tw/zhi-shi-ku/150-view-generated-files.md) 清單來看到它們。

下方這段（簡短的）影片摘要了上述範例的編譯過程，並示範如何存取由 TikZ 產生的檔案清單：

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/6VZVoHzhpNzOWSTvsWYm08/80d178255345da83bcd82b5fc64e5c6b/TikZExternalize.mp4>" %}

## 一些注意事項

將 TikZ 圖形外部化對底層 TeX 引擎（pdfTeX、XeTeX、LuaTeX 等）而言可能相當耗費運算資源，因此 `external` 該函式庫提供一個可「最佳化」此流程的選項。若啟用最佳化，TikZ 會嘗試透過「忽略」一些複雜（昂貴），但對 TikZ 並非必要的指令來減少工作量——方法是動態重新定義它們。此外，來自其他套件的某些指令在外部化模式下可能會造成錯誤；例如， `\includepdf` 來自 `pdfpages` 套件。為了解決這個問題，您需要將這類指令「最佳化移除」：

```latex
\tikzexternalize[prefix=tikz/,optimize command away=\includepdf]
```

## 要外部化的複雜圖形太多了！

如果您的圖形很複雜和／或數量很多，您很可能會在 Overleaf 上遇到 [編譯逾時](/latex/zh-tw/zhi-shi-ku/038-fixing-and-preventing-compile-timeouts.md#compiletimeout) 在初次編譯時，因為正在產生外部化檔案。這一節會列出一些可能的解決方法，以避免編譯逾時。

### 逐步建置

透過 [註解掉](https://ctan.org/pkg/comment?lang=en) 專案中不同區塊來逐步建置專案可能會有幫助，但請記住，所有產生的檔案都會在伺服器上以固定間隔被垃圾回收。這樣一來，您下次登入時專案可能就無法編譯，而且您必須再一次逐步重新產生全部檔案。

### 在本機產生圖形

* **注意**：本節假設您的專案使用 `\tikzexternalize[prefix=tikz/]` 來將已產生的圖形快取到一個名為 `tikz`.

作為替代方法， [下載您的 Overleaf 專案](/latex/zh-tw/zhi-shi-ku/036-exporting-your-work-from-overleaf.md) 並在本機電腦上編譯，以外部化（產生）圖形檔案。編譯完成後， `tikz` 資料夾中應包含每個產生圖形的數種檔案類型，包括 `*somegraphic*.pdf`, `*somegraphic*.md5` 和 `*somegraphic*.dpth`。 `tikz` 如果您將這些檔案上傳到 Overleaf 專案的

* **注意（圖形檔名）**：Overleaf 會設定 `\jobname` 改為 `輸出`，這會影響外部化（產生）的 TikZ 圖形名稱：預設情況下，產生的圖形檔案會命名為 `output-figureN.EXT` 其中
  * `N` 是根據圖形出現的順序／次序所推導出的整數（`N`=0, 1, 2, ...);
  * `EXT` 是 TikZ 使用的副檔名之一，包括 `pdf`, `md5` 和 `dpth` （以及 `log`).
* **一個注意事項**：如果在上傳圖形檔案後，您之後又修改了專案中的 TikZ 程式碼，可能需要刪除 `tikz` 資料夾中的對應圖形檔案，讓 Overleaf 能產生並使用新版本。或者，您也可以在本機重新產生檔案並重新上傳到 Overleaf。請記得，您需要替換所有 `.pdf`, `.md5` 和 `.dpth` 檔案，針對該特定 TikZ 繪圖。

### 在另一個專案中產生圖形

另一個選項是建立一個僅含圖形的獨立專案，只包含 TikZ 圖形。當這些圖形完成後，您可以依下列方式將該獨立專案中的 PDF 上傳到您的主專案：

* 點擊上傳檔案圖示（![Overleaf file upload icon](/files/2075dd0c85fb9a0117e1063a51727ac5878c7463)）在檔案清單面板上方；
* 選擇 **從另一個專案**;
* 從下拉選單中選取包含 TikZ 圖形的專案；
* 選擇 **從輸出檔案中**;
* 選擇 **output.pdf** ，並（可選）為上傳的檔案命名。

上傳後，您可以透過選取上傳 PDF 的適當頁面來存取個別圖形；例如，透過如下指令：

```latex
\includegraphics[page=1]{tikzpics.pdf}
```

該 [本文末尾的影片](#videodemo) 示範如何從另一個專案上傳 PDF。

#### 如何建立獨立的圖形專案

請使用 `standalone` 或 `preview` 套件來建立一個包含 TikZ 圖形的獨立專案——輸出的 PDF 會包含緊密裁切、與圖形大小相符的頁面。

**範例：standalone 套件**

這裡有一些使用 `standalone` 套件的範例程式碼：

```latex
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[fill=yellow]{Hello!};
\end{tikzpicture}

\begin{tikzpicture}
\node[fill=pink]{World!};
\end{tikzpicture}
\end{document}
```

[在 Overleaf 中開啟這個範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+standalone+package+and+TikZ\&snip=%5Cdocumentclass%5Btikz%5D%7Bstandalone%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cnode%5Bfill%3Dyellow%5D%7BHello%21%7D%3B%0A%5Cend%7Btikzpicture%7D%0A%0A%5Cbegin%7Btikzpicture%7D%0A%5Cnode%5Bfill%3Dpink%5D%7BWorld%21%7D%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![standalone LaTeX 套件範例](/files/a264862ecdd9dafded0553ece8744d43a5c4e1d0)

**範例：preview 套件**

這裡有一些使用 `preview` 套件的範例程式碼：

```latex
\documentclass{article}
\usepackage[active,tightpage]{preview}
\setlength{\PreviewBorder}{0pt}
\usepackage{tikz}
\PreviewEnvironment[{[]}]{tikzpicture}
\begin{document}
\begin{tikzpicture}
\node[fill=yellow]{Hello!};
\end{tikzpicture}

\begin{tikzpicture}
\node[fill=pink]{World!};
\end{tikzpicture}
\end{document}
```

[在 Overleaf 中開啟這個範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+the+preview+package+and+TikZ\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%5Bactive%2Ctightpage%5D%7Bpreview%7D%0A%5Csetlength%7B%5CPreviewBorder%7D%7B0pt%7D%0A%5Cusepackage%7Btikz%7D%0A%5CPreviewEnvironment%5B%7B%5B%5D%7D%5D%7Btikzpicture%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cnode%5Bfill%3Dyellow%5D%7BHello%21%7D%3B%0A%5Cend%7Btikzpicture%7D%0A%0A%5Cbegin%7Btikzpicture%7D%0A%5Cnode%5Bfill%3Dpink%5D%7BWorld%21%7D%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

此範例會產生以下輸出：

![preview LaTeX 套件範例](/files/09eca1a656c5cc1745f65be6c0871817eaac8ef2)

### 影片示範：從另一個 Overleaf 專案上傳檔案

以下影片示範如何上傳一個僅包含圖形的 PDF 檔（來自另一個 Overleaf 專案）——這裡我們使用 `standalone` 套件來建立包含以 TikZ 為基礎插圖的 PDF。

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/6l4dDatlnqzIymJLofSnQP/61f8ad4b872f5a82f3cd45c487e2f101/TikzSeparateProjects.mp4>" %}


---

# 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/wen-yu-da/60-i-have-a-lot-of-tikz-matlab2tikz-or-pgfplots-figures-so-i-m-getting-a-compilation-timeout.-can-i.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.
