· 3 min read

借助 hyperlink 宏包优雅地在 LaTeX 文档中插入超链接

借助 hyperlink 宏包在 LaTeX 文档中插入超链接, 避免网址中特殊符号导致的美观和编译错误

偶尔需要在 LaTeX 文档中插入网址. 但是 URL 编码往往很长而且复杂, 并带有各种特殊符号, 直接放在文档里既不美观, 也经常在编译时导致错误. 借助 hyperlink 宏包的帮助可以在 LaTeX 文档中插入超链接, 避免直接放 URL 带来的种种问题.

基本配置

导入宏包并设置链接样式:

\usepackage{hyperref} % 使用宏包

% 设置链接样式
\hypersetup{
    colorlinks = true,
    linkcolor=blue,
    filecolor=blue,      
    urlcolor=blue,
    citecolor=cyan,
}

如果不想让超链接显示为其他颜色可以将 colorlinks 设置为 false.

链接样式有很多项目可以自定义, 参考下表:

OptionDefault valueDescription
hyperindextrueMakes the page numbers of index entries into hyperlinks
linktocpagefalseMakes the page numbers instead of the text to be link in the Table of contents.
breaklinksfalseAllows links to be broken into multiple lines.
colorlinksfalseColours the text for links and anchors, these colours will appear in the printed version
linkcolorredColour for normal internal links
anchorcolorblackColour for anchor (target) text
citecolorgreenColour for bibliographical citations
filecolorcyanColour for links that open local files
urlcolormagentaColour for linked URLs
frenchlinksfalseUse small caps instead of colours for links

针对 PDF 输出也有很多定义选项:

OptionDefault valueDescription
bookmarkstrueAcrobat bookmarks are written, similar to the table of contents.
bookmarksopenfalseBookmarks are shown with all sub-trees expanded.
citebordercolor0 1 0Colour of the box around citations in RGB format.
filebordercolor0 .5 .5Colour of the box around links to files in RGB format.
linkbordercolor1 0 0Colour of the box around normal links in RGB format.
menubordercolor1 0 0Colour of the box around menu links in RGB format.
urlbordercolor0 1 1Colour of the box around links to URLs in RGB format.
pdfpagemodeemptyDetermines how the file is opened. Possibilities are UseThumbs (Thumbnails), UseOutlines (Bookmarks) and FullScreen.
pdftitleSets the document title.
pdfauthorSets the document Author.
pdfstartpage1Determines on which page the PDF file is opened.

注意: hyperlink 宏包包含对很多 LaTeX 命令的重新定义, 因此在导入顺序上应当尽可能晚, 防止其中重新定义的命令又被其他宏包覆盖. 不过也有一些宏包需要在 hyperlink 之后导入:

Which packages should be loaded after hyperref instead of before? - TeX - LaTeX Stack Exchange

插入链接

两种方式:

\href{https://www.google.com}{Google}

\url{https://www.google.com}

\href{mailto:[email protected]}{[email protected]}

对于邮箱地址, 可以在前面加上 mailto: , 这样点击该链接可以直接打开系统默认邮件软件发送邮件.

上面的代码排版效果如下:

正文链接排版效果

在参考文献中插入超链接:

% file: main.tex
Introduction to LaTeX\cite{enwiki:1023081178}.

% file: ref.bib
@misc{ enwiki:1023081178,
    author = "{Wikipedia contributors}",
    title = "LaTeX --- {Wikipedia}{,} The Free Encyclopedia",
    year = "2021",
    url = "https://en.wikipedia.org/w/index.php?title=LaTeX&oldid=1023081178",
    note = "[Online; accessed 10-July-2021]. \url{https://en.wikipedia.org/w/index.php?title=LaTeX&oldid=1023081178}"
}

排版效果如下:

参考文献链接排版效果

参考

Hyperlinks - Overleaf, Online LaTeX Editor

Share:
Back to Blog

Related Posts

View All Posts »

再次重构站点小记

再次重构和迁移博客, 切换了主题、框架, 切换了部署平台, 重新寻找图床解决方案, 撰写博客的方式也发生了变化. 此外, 还为博客新增了基于 giscus 的讨论区和基于 Umami 的访客统计

Linux 本地化 - locale 变量与设置方法

Linux 中通过 locales 调整语言, 编码以及其他本地化设置, 如果设置不当可能导致乱码等问题. 本文介绍 Linux 中 locales 变量的作用以及 locales 的设置方法