> 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-jian-jie-gou/09-hyperlinks.md).

# 超連結

LaTeX 是一個很棒的工具，可用來製作可列印、具專業外觀的文件，也可用來產生具有優異導覽工具的 PDF 檔案。本文說明如何在文件中建立超連結，以及如何設定 LaTeX 文件以便透過 PDF 閱讀器檢視。

## 簡介

我們先從一個最小可運作範例開始，只要匯入 **hyperref** all 套件 [交叉參照的元素](/latex/zh-tw/wen-jian-jie-gou/03-cross-referencing-sections-equations-and-floats.md) 會變成超連結。

```latex
\documentclass{book}
\usepackage{blindtext}
\usepackage{hyperref}

\title{超連結範例}
\author{Overleaf}

\begin{document}

\frontmatter
\tableofcontents
\clearpage

\addcontentsline{toc}{chapter}{Foreword}
{\huge {\bf 序言}}

\Blindtext
\clearpage

\addcontentsline{toc}{chapter}{虛擬條目}
{\huge {\bf 虛擬條目}}

\Blindtext
\mainmatter

\chapter{First Chapter}

這將會是一個空白章節

\begin{equation}
\label{eq:1}
\sum_{i=0}^{\infty} a_i x^i
\end{equation}

方程式 \ref{eq:1} 顯示了一個發散的和。此公式稍後會在第 \pageref{second} 頁使用。

\Blindtext
\clearpage

\section{第二節} \label{second}

\blindtext
\Blinddocument
\end{document}
```

[在 Overleaf 中開啟這個 `hyperref` 範例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+hyperref+example\&snip=%5Cdocumentclass%7Bbook%7D%0A%5Cusepackage%7Bblindtext%7D%0A%5Cusepackage%7Bhyperref%7D%0A%0A%5Ctitle%7BExample+of+Hyperlinks%7D%0A%5Cauthor%7BOverleaf%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cfrontmatter%0A%5Ctableofcontents%0A%5Cclearpage%0A%0A%5Caddcontentsline%7Btoc%7D%7Bchapter%7D%7BForeword%7D%0A%7B%5Chuge+%7B%5Cbf+Foreword%7D%7D%0A%0A%5CBlindtext%0A%5Cclearpage%0A%0A%5Caddcontentsline%7Btoc%7D%7Bchapter%7D%7BDummy+entry%7D%0A%7B%5Chuge+%7B%5Cbf+Dummy+entry%7D%7D%0A%0A%5CBlindtext%0A%5Cmainmatter%0A%0A%5Cchapter%7BFirst+Chapter%7D%0A%0AThis+will+be+an+empty+chapter%0A%0A%5Cbegin%7Bequation%7D%0A%5Clabel%7Beq%3A1%7D%0A%5Csum_%7Bi%3D0%7D%5E%7B%5Cinfty%7D+a_i+x%5Ei%0A%5Cend%7Bequation%7D%0A%0AThe+equation+%5Cref%7Beq%3A1%7D+shows+a+sum+that+is+divergent.+This+formula+will+be+used+later+on+page+%5Cpageref%7Bsecond%7D.%0A%0A%5CBlindtext%0A%5Cclearpage%0A%0A%5Csection%7BSecond+section%7D+%5Clabel%7Bsecond%7D%0A%0A%5Cblindtext%0A%5CBlinddocument%0A%5Cend%7Bdocument%7D)

![hyperref 範例文件](/files/4758e1d9d7bd5fb37703581e97c1f99a083e1e37)

只要在文件前言中加入以下這一行，目錄中的各行就會成為連到文件中對應頁面的連結

```latex
\usepackage{hyperref}
```

匯入時必須小心 `hyperref`：通常它必須是最後匯入的套件——但這條規則可能有一些例外。

## 樣式與顏色

連結的預設格式可以變更，讓文件中的資訊呈現得更清楚。以下可看到一個範例：

```latex
\documentclass{book}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
    pdftitle={Overleaf Example},
    pdfpagemode=FullScreen,
    }

\urlstyle{same}

\begin{document}

\tableofcontents

\chapter{First Chapter}

這將會是一個空白章節，我會在這裡放一些文字

\begin{equation}
\label{eq:1}
\sum_{i=0}^{\infty} a_i x^i
\end{equation}

方程式 \ref{eq:1} 顯示了一個發散的和。此公式
稍後會在第 \pageref{second} 頁使用。

欲了解更多參考資料，請見 \href{http://www.overleaf.com}{某個
連結}，或前往下一個網址：\url{http://www.overleaf.com}，或開啟
下一個檔案 \href{run:./file.txt}{File.txt}

也可以直接連結任何單字或
\hyperlink{thesentence}{任何句子}在你的文件中。

\end{document}
```

[在 Overleaf 中開啟一個 `hyperref` 套件範例](https://www.overleaf.com/project/new/template/19606?id=64789057\&templateName=Another+hyperref+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=main.tex)

![HyperrefEx2OverleafVa.png](/files/cadc08b935b8015f767ec4be3c1e4ca79fabc2c0)

![HyperrefEx2OverleafVb.png](/files/a26bef6c0ae0ab67b0820ef3bc799a1db8b7e857)

這是一個完整範例，接下來的文章其餘部分會完整說明。以下是與連結顏色與樣式相關指令的說明。

**\hypersetup{ ... }**

這會設定選項來配置文件中連結的行為。每個參數都必須以逗號分隔，語法格式必須是 parameter=value。

**colorlinks=true**

連結會以顏色顯示，預設顏色為紅色。

**linkcolor=blue**

內部連結，也就是由交叉參照元素產生的連結，會以藍色顯示。

**filecolor=magenta**

連到本機檔案的連結會以洋紅色顯示（請參見連結本機檔案）。

**urlcolor=cyan**

網站連結會設為青色（請參見連結網址）。

**urlstyle{same}**

預設設定會以等寬間距字型列印連結，此指令會加以變更，讓連結以與其餘文字相同的樣式顯示。

## 連結網頁網址

可使用以下指令將網頁網址或電子郵件連結加入 LaTeX 檔案： `\url` 指令來顯示實際連結，或 `\href` 用來使用隱藏連結並改顯示一個單字／句子。

```latex
欲了解更多參考資料，請見 \href{http://www.overleaf.com}{Something Linky}
或前往下一個網址：\url{http://www.overleaf.com}
```

![HyperlinkExampleUpdatedMore.png](/files/32ebada05a090f907a1d3f344268e2472291bbae)

範例中有兩個指令會在最終文件中產生連結：

**\href{<http://www.overleaf.com}{Something> Linky}**

這個指令有兩個參數，第一個是連結的網址，在此例中為 <http://www.overleaf.com，第二個是要顯示的可點擊文字，Something> Linky。

**\url{<http://www.overleaf.com}>**

此指令會顯示作為參數傳入的網址並將其製作成連結，若你要列印文件時很有用。

[在 Overleaf 中開啟 hyperref 套件範例](https://www.overleaf.com/project/new/template/19606?id=64789057\&templateName=Hyperref+Example+2\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=main.tex)

## 連結本機檔案

這些指令 `\href` 和 `\url` 前一節所介紹的內容可用來開啟本機檔案

```latex
欲了解更多參考資料，請見 \href{http://www.overleaf.com}{Something Linky}
或前往下一個網址：\url{http://www.overleaf.com}，或開啟下一個
檔案 \href{run:./file.txt}{File.txt}
```

![HyperlinkExampleUpdatedMoreNext.png](/files/fa866760bb28d80394cc25f41f5783e6a538d023)

命令 `\href{run:./file.txt}{File.txt}` 會列印文字 `File.txt` 它會連到一個名為 `file.txt` 位於目前工作目錄中。請注意文字 `run:` 出現在檔案路徑之前。

檔案路徑遵循 UNIX 系統的慣例，使用 `.` 來表示目前目錄，而 `..` 則表示上一層目錄。

命令 `\url{}` 也可以使用，其語法與前述路徑相同，但據報有一些問題。

[在 Overleaf 中開啟 hyperref 套件範例](https://www.overleaf.com/project/new/template/19606?id=64789057\&templateName=Hyperref+Example+2\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=main.tex)

## 手動插入連結

前面提到，所有交叉參照的元素一旦 **hyperref** 被匯入之後就會變成連結，因此我們可以使用 `\label` 在文件中的任何位置，之後再引用那些標籤來建立連結。這並不是手動插入超連結的唯一方式。

```latex
也可以直接連結任何單字
或 \hyperlink{thesentence}{任何句子}在你的文件中。

如果你讀這段文字，你會得不到任何資訊。真的嗎？
真的沒有資訊嗎？

例如 \hypertarget{thesentence}{這句話}。
```

![HyperlinkExampleUpdatedMoreNextAgain.png](/files/4f2670ff64c267ab287932e43f2d803fa47ea513)

有兩個指令可建立使用者自訂的連結。

**\hypertarget{thesentence}{這句話}**

傳入此指令大括號中的第一個參數是這句話的唯一識別碼。第二個參數是文字 "this sentence"，會正常列印（視 anchorcolor 的值而定，請參閱參考指南），但當按下指向識別碼 "thesentence" 的連結時，PDF 檔案會捲動到這個位置。

**\hyperlink{thesentence}{any sentence}**

此指令會將文字 "any sentence" 列印為可點擊元素，並重新導向到識別碼為 "thesentence" 的點。

[在 Overleaf 中開啟 hyperref 套件範例](https://www.overleaf.com/project/new/template/19606?id=64789057\&templateName=Hyperref+Example+2\&latexEngine=pdflatex\&texImage=\&mainFile=main.tex)

## PDF 專用選項

文件中的連結是以將以 PDF 格式閱讀的文件為前提而建立的。PDF 檔案還可以進一步個人化，加入額外資訊並改變 PDF 檢視器的顯示方式。以下是一個範例：

```latex
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
    pdftitle={Overleaf Example},
    pdfpagemode=FullScreen,
}
```

![Pdftitle.png](/files/cc76269edbee43168392e91ef2ee38a210bf2f2e)

使用指令 `\hypersetup`，如同在章節 [樣式與顏色](#styles-and-colours)所描述的一樣，可接受額外參數來設定最終的 PDF 檔案。

**pdftitle={Overleaf Example}**

是 PDF 輸出檔案的標題，會顯示在視窗標題列中。在範例中是 "Overleaf Example"。

**pdfpagemode=FullScreen**

文件將由 PDF 閱讀器以全螢幕模式開啟。

請參閱 [參考指南](#reference-guide) 可傳遞給 `\hypersetup`.

[在 Overleaf 中開啟 hyperref 套件範例](https://www.overleaf.com/project/new/template/19606?id=64789057\&templateName=Hyperref+Example+2\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=main.tex)

## 參考指南

**連結樣式選項**

| 選項            | 預設值     | 說明                       |
| ------------- | ------- | ------------------------ |
| `hyperindex`  | true    | 將索引項目的頁碼製作成超連結           |
| `linktocpage` | false   | 讓目錄中的頁碼而不是文字成為連結。        |
| `breaklinks`  | false   | 允許連結換行成多行。               |
| `colorlinks`  | false   | 為連結與錨點文字著色，這些顏色會出現在列印版本中 |
| `linkcolor`   | red     | 一般內部連結的顏色                |
| `anchorcolor` | black   | 錨點（目標）文字的顏色              |
| `citecolor`   | green   | 書目引用的顏色                  |
| `filecolor`   | cyan    | 開啟本機檔案的連結顏色              |
| `urlcolor`    | magenta | 連結網址的顏色                  |
| `frenchlinks` | false   | 連結使用小型大寫字母而非顏色           |

**PDF 專用選項**

| 選項                | 預設值     | 說明                                                         |
| ----------------- | ------- | ---------------------------------------------------------- |
| `bookmarks`       | true    | 會寫入 Acrobat 書籤，類似於目錄。                                      |
| `bookmarksopen`   | false   | 書籤會展開所有子樹顯示。                                               |
| `citebordercolor` | 0 1 0   | 引用周圍方框的顏色，採 RGB 格式。                                        |
| `filebordercolor` | 0 .5 .5 | 檔案連結周圍方框的顏色，採 RGB 格式。                                      |
| `linkbordercolor` | 1 0 0   | 一般連結周圍方框的顏色，採 RGB 格式。                                      |
| `menubordercolor` | 1 0 0   | 選單連結周圍方框的顏色，採 RGB 格式。                                      |
| `urlbordercolor`  | 0 1 1   | 網址連結周圍方框的顏色，採 RGB 格式。                                      |
| `pdfpagemode`     | empty   | 決定檔案如何開啟。可用選項有 UseThumbs（縮圖）、UseOutlines（書籤）以及 FullScreen。 |
| `pdftitle`        |         | 設定文件標題。                                                    |
| `pdfauthor`       |         | 設定文件作者。                                                    |
| `pdfstartpage`    | 1       | 決定 PDF 檔案會從哪一頁開啟。                                          |

## 延伸閱讀

更多資訊請參見

* [目錄](/latex/zh-tw/wen-jian-jie-gou/02-table-of-contents.md)
* [章節](/latex/zh-tw/wen-jian-jie-gou/01-sections-and-chapters.md)
* [章節與方程式交叉參照](/latex/zh-tw/wen-jian-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [在 LaTeX 中使用顏色](/latex/zh-tw/ge-shi-hua/13-using-colors-in-latex.md)
* [大型專案中的管理](/latex/zh-tw/wen-jian-jie-gou/07-management-in-a-large-project.md)
* [Hyperref 手冊](http://repositorios.cpai.unb.br/ctan/macros/latex/contrib/hyperref/doc/manual.pdf)
* [wikibooks 上的超連結文章](http://en.wikibooks.org/wiki/LaTeX/Hyperlinks)


---

# 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-jian-jie-gou/09-hyperlinks.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.
