LaTeX - 将可点击的链接添加到 PDF 文档的部分/子部分

发布于 2024-09-10 07:05:13 字数 117 浏览 2 评论 0原文

我正在用 LaTeX 制作 PDF。我有几个部分和小节。我想在文档顶部放置一个链接,以便在 PDF 中有人可以单击它,然后它会转到该部分/小节。我知道可以使用可链接的目录来实现此目的,但我不想制作目录,我需要更多的控制。

I am making PDF with LaTeX. I have a few sections and subsections. I want to put a link towards the top of the document so that in the PDF someone can click on it and it'll go to that section/subsection. I know it's possible to have this with a linkable table of contents, but I don't want to make a table of contents, I need more control.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手长情犹 2024-09-17 07:05:13

在文档的序言中包含 \usepackage{hyperref}。为您的部分分配适当的标签,并使用 \ref{} 引用这些标签。当使用 pdflatex 创建 PDF 时,这些参考文献将被转换为可点击的链接。

Include \usepackage{hyperref} in the preamble of your document. Assign proper labels to your sections and reference these labels using \ref{}. These references will then be turned into clickable links when creating PDFs with pdflatex.

氛圍 2024-09-17 07:05:13

正如其他答案中所指出的,您可以使用 hyperref 包。但是,默认设置非常糟糕(它在每个链接周围添加了一个大多数人认为丑陋的框),因此这里是一个典型的代码片段,用于自定义最有用的设置:

\usepackage{hyperref}
\hypersetup{
  colorlinks   = true,    % Colours links instead of ugly boxes
  urlcolor     = blue,    % Colour for external hyperlinks
  linkcolor    = blue,    % Colour of internal links
  citecolor    = red      % Colour of citations
}

此外,如果您使用包 natlib (\ usepackage{natbib}),hyperref 将生成两个链接:

在此处输入图像描述

要解决此问题,添加

\usepackage{etoolbox}

\makeatletter

\pretocmd{\NAT@citex}{%
  \let\NAT@hyper@\NAT@hyper@citex
  \def\NAT@postnote{#2}%
  \setcounter{NAT@total@cites}{0}%
  \setcounter{NAT@count@cites}{0}%
  \forcsvlist{\stepcounter{NAT@total@cites}\@gobble}{#3}}{}{}
\newcounter{NAT@total@cites}
\newcounter{NAT@count@cites}
\def\NAT@postnote{}

% include postnote and \citet closing bracket in hyperlink
\def\NAT@hyper@citex#1{%
  \stepcounter{NAT@count@cites}%
  \hyper@natlinkstart{\@citeb\@extra@b@citeb}#1%
  \ifnumequal{\value{NAT@count@cites}}{\value{NAT@total@cites}}
    {\ifNAT@swa\else\if*\NAT@postnote*\else%
     \NAT@cmt\NAT@postnote\global\def\NAT@postnote{}\fi\fi}{}%
  \ifNAT@swa\else\if\relax\NAT@date\relax
  \else\NAT@@close\global\let\NAT@nm\@empty\fi\fi% avoid compact citations
  \hyper@natlinkend}
\renewcommand\hyper@natlinkbreak[2]{#1}

% avoid extraneous postnotes, closing brackets
\patchcmd{\NAT@citex}
  {\ifNAT@swa\else\if*#2*\else\NAT@cmt#2\fi
   \if\relax\NAT@date\relax\else\NAT@@close\fi\fi}{}{}{}
\patchcmd{\NAT@citex}
  {\if\relax\NAT@date\relax\NAT@def@citea\else\NAT@def@citea@close\fi}
  {\if\relax\NAT@date\relax\NAT@def@citea\else\NAT@def@citea@space\fi}{}{}

\makeatother

As pointed to in the other answers, you can use the hyperref package. However, the default settings are pretty bad (it adds a box that most consider ugly around each link), so here is a typical code snippet to customize the most useful settings:

\usepackage{hyperref}
\hypersetup{
  colorlinks   = true,    % Colours links instead of ugly boxes
  urlcolor     = blue,    % Colour for external hyperlinks
  linkcolor    = blue,    % Colour of internal links
  citecolor    = red      % Colour of citations
}

Also, in case you use the package natlib (\usepackage{natbib}), hyperref will produce two links:

enter image description here

To fix this, add:

\usepackage{etoolbox}

\makeatletter

\pretocmd{\NAT@citex}{%
  \let\NAT@hyper@\NAT@hyper@citex
  \def\NAT@postnote{#2}%
  \setcounter{NAT@total@cites}{0}%
  \setcounter{NAT@count@cites}{0}%
  \forcsvlist{\stepcounter{NAT@total@cites}\@gobble}{#3}}{}{}
\newcounter{NAT@total@cites}
\newcounter{NAT@count@cites}
\def\NAT@postnote{}

% include postnote and \citet closing bracket in hyperlink
\def\NAT@hyper@citex#1{%
  \stepcounter{NAT@count@cites}%
  \hyper@natlinkstart{\@citeb\@extra@b@citeb}#1%
  \ifnumequal{\value{NAT@count@cites}}{\value{NAT@total@cites}}
    {\ifNAT@swa\else\if*\NAT@postnote*\else%
     \NAT@cmt\NAT@postnote\global\def\NAT@postnote{}\fi\fi}{}%
  \ifNAT@swa\else\if\relax\NAT@date\relax
  \else\NAT@@close\global\let\NAT@nm\@empty\fi\fi% avoid compact citations
  \hyper@natlinkend}
\renewcommand\hyper@natlinkbreak[2]{#1}

% avoid extraneous postnotes, closing brackets
\patchcmd{\NAT@citex}
  {\ifNAT@swa\else\if*#2*\else\NAT@cmt#2\fi
   \if\relax\NAT@date\relax\else\NAT@@close\fi\fi}{}{}{}
\patchcmd{\NAT@citex}
  {\if\relax\NAT@date\relax\NAT@def@citea\else\NAT@def@citea@close\fi}
  {\if\relax\NAT@date\relax\NAT@def@citea\else\NAT@def@citea@space\fi}{}{}

\makeatother

enter image description here

南七夏 2024-09-17 07:05:13

hyperref 包对此类事情提供了广泛的支持(如之前的答案中所述)。

注意和建议:hyperref 是一个大包,并且(必然)它利用 LaTeX 的本质玩了一些非常肮脏的把戏。最后加载 hyperref 包,如果你的文档突然变得奇怪,那么注释掉该包,从你的目录中删除 .out.aux 文件,然后尝试再次查看问题是否消失。如果确实如此,那么……想想办法。

hypertex 包可以做一些相同的事情,并且更加轻量级。但我的记忆是它有点脆弱,可能已经不再维护了。

您可以使用 PDF 特辑来做一些这样的事情(请参阅 pdftex 手册),但这有点硬核,并且需要您对 PDF 有相当多的了解。

The hyperref package has extensive support for this sort of thing (as noted in an earlier answer).

Notes and advice: hyperref is a big package, and (by necessity) it plays some pretty dirty tricks with the guts of LaTeX. Load the hyperref package last, and if your document suddenly becomes weird, then comment that package out, get rid of the .out and .aux files from your directory, and try again to see if the problem disappears. If it does, then ... think of something.

The hypertex package can do some of the same things, and is a little more lightweight. But my recollection is that it's a little fragile, and may not be much maintained any more.

You can do some of this stuff with PDF specials (see the pdftex manual), but that's getting a little hardcore, and requires you to know quite a bit about PDF.

溺渁∝ 2024-09-17 07:05:13

除了 Franck 关于超链接样式的评论之外,还可以通过添加 hidelinks 选项来删除所有标记。

\usepackage[hidelinks]{hyperref} 

In addition to Franck's comment about hyperlink styles, it is possible to remove all markup by adding the hidelinks option.

\usepackage[hidelinks]{hyperref} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文