16
votes

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.

4
A link at the top of the document is very precisely what a table of contents is about. How would your link differ from a ToC?Philipp
Philipp, I want a more custom layout instead a list like the default ToC. I have that layout done now, and I think adding links to it would be easier than making a ToC look how I want.Amandasaurus

4 Answers

37
votes

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.

13
votes

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.

12
votes

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

1
votes

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

\usepackage[hidelinks]{hyperref}