> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/wen-dang-jie-gou/09-hyperlinks.md).

# 超链接

LaTeX 是一种很棒的工具，可用于生成适合打印的专业文档，但也可用于生成带有出色导航工具的 PDF 文件。本文介绍如何在文档中创建超链接，以及如何设置可使用 PDF 阅读器查看的 LaTeX 文档。

## 引言

让我们从一个最小可用示例开始，只需简单导入 **hyperref** 所有宏包 [相互引用的元素](/latex/zh-cn/wen-dang-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}{序言}
{\huge {\bf 序言}}

\Blindtext
\clearpage

\addcontentsline{toc}{chapter}{虚拟条目}
{\huge {\bf 虚拟条目}}

\Blindtext
\mainmatter

\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}
```

[打开此 `hyperref` Overleaf 中的示例](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/27d86c7b4b76aec69647c4460538817a6d16cf73)

只需在文档导言区添加下面这一行，目录中的各行就会变成指向文档中对应页面的链接

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

导入时必须小心 `hyperref`：通常它必须是最后一个导入的宏包——不过这条规则也可能有一些例外。

## 样式与颜色

可以更改链接的默认格式，以便更清晰地呈现文档中的信息。下面是一个示例：

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

\urlstyle{same}

\begin{document}

\tableofcontents

\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}
```

[打开一个……的示例 `hyperref` Overleaf 中的宏包](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/68453274770faa48cca13a3fed94d8816daa263d)

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

这是一个完整示例，本文其余部分将对其进行全面解释。下面是与链接颜色和样式相关命令的说明。

**\hypersetup{ ... }**

这将设置用于配置文档内链接行为的选项。每个参数都必须以逗号分隔，语法格式必须为 parameter=value。

**colorlinks=true**

链接将以彩色显示，默认颜色为红色。

**linkcolor=blue**

内部链接，也就是由相互引用元素生成的链接，将以蓝色显示。

**filecolor=magenta**

指向本地文件的链接将以洋红色显示（参见链接本地文件）。

**urlcolor=cyan**

指向网站的链接将设置为青色（参见链接网址）。

**urlstyle{same}**

默认设置会以等宽间隔字体打印链接，此命令会更改这一点，并以与正文其余部分相同的样式显示链接。

## 链接网址

可以使用以下命令将网页地址或电子邮件链接添加到 LaTeX 文件中： `\url` 命令来直接显示实际链接，或者 `\href` 来使用隐藏链接，并改为显示一个单词/句子。

```latex
更多参考请见 \href{http://www.overleaf.com}{某些链接}
或者访问下一个网址：\url{http://www.overleaf.com}
```

![HyperlinkExampleUpdatedMore.png](/files/00943422624f6d189d3619985218031ff647c9f5)

示例中有两个命令会在最终文档中生成链接：

**\href{<http://www.overleaf.com}{某些链接}>**

该命令传入两个参数，第一个是链接的 URL，这里是 <http://www.overleaf.com；第二个是要显示的可点击文本，即某些链接。>

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

该命令会显示作为参数传入的 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)

## 链接本地文件

这些命令 `\href` 和 `\url` 上一节中介绍的方法可用于打开本地文件

```latex
更多参考请见 \href{http://www.overleaf.com}{某些链接}
或者访问下一个网址：\url{http://www.overleaf.com}，或者打开下一个
文件 \href{run:./file.txt}{File.txt}
```

![HyperlinkExampleUpdatedMoreNext.png](/files/4a995e595da93786483ce843ebf021dbb6b3fd11)

命令 `\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/270b95be7b3264fcd70b65dc35211007ec4b77df)

有两个命令可用于创建用户定义的链接。

**\hypertarget{thesentence}{这句话}**

传入该命令花括号中的第一个参数是这句话的唯一标识符。第二个参数是文本“这句话”，它会正常打印出来（取决于 anchorcolor 的值，参见参考指南），但当点击指向标识符“thesentence”的链接时，PDF 文件会滚动到这一位置。

**\hyperlink{thesentence}{任意句子}**

该命令会将文本“任意句子”打印为可点击元素，并将其重定向到标识符为“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 示例},
    pdfpagemode=FullScreen,
}
```

![Pdftitle.png](/files/91688c0a5613e840f47dbb37b8576925aab8e420)

使用命令 `\hypersetup`，在 [样式与颜色](#styles-and-colours)部分中介绍的该命令可接受额外参数来设置最终的 PDF 文件。

**pdftitle={Overleaf 示例}**

这是 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 | 链接 URL 的颜色                |
| `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   | URL 链接周围方框的 RGB 颜色。                                        |
| `pdfpagemode`     | empty   | 确定文件的打开方式。可选项有 UseThumbs（缩略图）、UseOutlines（书签）和 FullScreen。 |
| `pdftitle`        |         | 设置文档标题。                                                    |
| `pdfauthor`       |         | 设置文档作者。                                                    |
| `pdfstartpage`    | 1       | 确定 PDF 文件打开时所在的页码。                                         |

## 进一步阅读

更多信息请参见

* [目录](/latex/zh-cn/wen-dang-jie-gou/02-table-of-contents.md)
* [节与章](/latex/zh-cn/wen-dang-jie-gou/01-sections-and-chapters.md)
* [章节和公式交叉引用](/latex/zh-cn/wen-dang-jie-gou/03-cross-referencing-sections-equations-and-floats.md)
* [在 LaTeX 中使用颜色](/latex/zh-cn/ge-shi-hua/13-using-colors-in-latex.md)
* [大型项目中的管理](/latex/zh-cn/wen-dang-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/wen-dang-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.
