开发者

LaTeX - Add clickable links to a section/subsection with a PDF document

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开发者_StackOverflow中文版'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.


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.


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:

LaTeX - Add clickable links to a section/subsection with a PDF document

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

LaTeX - Add clickable links to a section/subsection with a PDF document


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.


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

\usepackage[hidelinks]{hyperref} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜