[Tex/LaTex] How to have colored and underlined links with hyperref

colorhyperreflinkstext-decorations

I would like the internal hyperlinks generated by hyperref to be colored and underlined.

Reading some of the package documentation, I know that these \hypersetup keys exist:

  • colorlinks: when true, color the link; when false, draw a border around the link and color that.
  • linkcolor: the color of the link (requires colorlinks=true to have any effect)
  • linkbordercolor: the color of the link border color (requires colorlinks=false to have any effect)
  • pdfborderstyle: keys for the pdf borderstyle dictionary. I don't know what keys and values exist for this dictionary, but I do know that pdfborderstyle={/S/U} or pdfborderstyle={/S/U/W 1} can change the link border from a box to an underline.

But it seems impossible with this key structure to have colored links (requiring colorlinks=true) and colored borders (requiring colorlinks=false).

Here is a minimal (non-)working example (via):

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}

\hypersetup{colorlinks=false,%
            linkbordercolor=red,linkcolor=green,pdfborderstyle={/S/U/W 1}}

\begin{document}
\section{To See}\label{tosee}
\vskip2cm
\hyperref[tosee]{just to see}
\end{document}

With colorlinks=false (the default) the text is black and the border is red.

With colorlinks=true the text is green and there is no border.

Best Answer

I assume this was done by design, seeing as the introduction of hyperlinks may clutter the user's view of the actual text. Moreover, not all hyperlink typesetting is printable - as you've mentioned, the PDF hyperlink is merely "a rectangular area of the page that is mouse-aware". However, if you want to do this, there are two options available


Manual

You deactivate the colorlinks option so that hyperref sets the link border:

\hypersetup{%
  colorlinks=false,% hyperlinks will be black
  linkbordercolor=red,% hyperlink borders will be red
  pdfborderstyle={/S/U/W 1}% border style will be underline of width 1pt
}

and typeset the text manually using \color{<color>}. For example:

...
\begin{document}
  \section{To See}\label{tosee}
  \hyperref[tosee]{\color{green}just to see}
\end{document}

Colorlinks=false with manual colour setting of link

Note that this is virtually the same as what hyperref does internally, since the text colour is modified and will typeset this way even if the hyperlink is removed via printing to PDF (or flattening).

The advantage behind this approach (motivating to include it here) is that you can specify different colours for each hyperlink, if you so wish.


Automatic

You activate the colorlinks option so that hyperref sets the link colour in the text

\hypersetup{%
  colorlinks=true,% hyperlinks will be coloured
  linkcolor=green,% hyperlink text will be green
  linkbordercolor=red,% hyperlink border will be red
}

and then add the following after the above \hypersetup{...}:

\makeatletter
\Hy@AtBeginDocument{%
  \def\@pdfborder{0 0 1}% Overrides border definition set with colorlinks=true
  \def\@pdfborderstyle{/S/U/W 1}% Overrides border style set with colorlinks=true
                                % Hyperlink border style will be underline of width 1pt
}
\makeatother

Colorlinks=true with linkborder active


Here is the pdfborderstyle specification from Adobe:

PDF border specification

Related Question