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

# 超链接

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

## 引言

让我们从一个最小可运行示例开始，只需导入 **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}
```

[在 Overleaf 中打开此 `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}{Something
Linky} 或访问下一个网址： \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}**

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

## 链接网页地址

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

```latex
如需更多参考，请看 \href{http://www.overleaf.com}{Something Linky}
或访问下一个网址： \url{http://www.overleaf.com}
```

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

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

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

这个命令有两个传入参数，第一个是链接的网址，这里是 <http://www.overleaf.com，第二个是要显示、可点击的文本，Something> Linky。

**\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}{Something Linky}
或访问下一个网址： \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 示例”。

**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   | 以 RGB 格式表示的 URL 链接周围边框的颜色。                                 |
| `pdfpagemode`     | 空       | 决定文件的打开方式。可选项有 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://overleaf-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.
