81

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.

  • 1
    Something I've discovered that I didn't know about PDF links. Unlike HTML links, the link text is not a child or property of the link. The link is just a rectangular area of the page that is mouse-aware. The link's text is separate. – Matthew Leingang Aug 18 '11 at 22:32
  • Color hyperlinks has been discussed, just recently, here – DJP Aug 18 '11 at 23:02
  • @DJP: Thanks for pointing that one out. But it doesn't really answer my question how to get colored and underlined links. – Matthew Leingang Aug 19 '11 at 3:01
  • Related Question: Switch between box and color links with hyperref. – Peter Grill Jun 23 '13 at 21:37
62

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

  • 5
    Works! Thank you. (Don't worry; my actual hyperlinks won't have green text and red underlines!) – Matthew Leingang Aug 19 '11 at 3:45
  • 1
    I wonder if there is a way to patch hyperref so that the links use soul's \ul and no border-underlining? – doncherry Aug 19 '11 at 15:53
  • 4
    Curiously, the pdfborderstyle specifications have effect only if the file is opened by Adobe Reader. They do not work e.g. with Apple's Preview. – Jairo Bochi Sep 11 '13 at 15:27
  • 3
    @J.B.: Viewer-specific support has been something of an issue in general when it comes to PDF usage. The same goes for security settings that is ignored by some non-Adobe viewers. – Werner Sep 11 '13 at 15:32
  • 1
    @zyy: Probably not. Consider using some third-party software for viewing PDFs. – Werner Aug 8 '18 at 16:07
14

I know you're probably looking for a solution that uses hyperref's own underlining just for the heck of it, but I'm adding this answer just in case anybody ever actually wants to use colored and underlined links (against which I advise). hyperref's underlining isn't particularly pretty, in fact, it's just the bottom line of a box. soul's underlining is prettier and much more customizable (see its documentation).

\documentclass{article}
\usepackage{xcolor,soul,lipsum}
\usepackage[hidelinks]{hyperref}
\newcommand{\myhy}[2]{\hyperref[#1]{\color{green}\setulcolor{red}\ul{#2}}}

\begin{document}
\section{To See}\label{tosee}
\vskip2cm
This is \myhy{tosee}{just to see} what it looks like. \lipsum[1]
\end{document}

underlining with soul

Here's the hyperref underlining, as a comparison:

underlining with hyperref

  • Good point. I'm not interested in replacing \hyperref because the links I want to decorate this way are automatically generated (such as in the TOC). And don't worry, I don't actually want green text and red underlines; that was just to test. In my actual manuscript I want the text and underline color to be the same. – Matthew Leingang Aug 19 '11 at 11:29
  • @Matthew: Aah, I hadn't even thought of the automatically generated links. In this case, we'd probably need to patch some of hyperref.sty. I had taken a quick peek into it, but I didn't understand a word. – doncherry Aug 19 '11 at 11:39
  • It's my fault; my MWE didn't have that in it. Like you, I was planning to delve into source, and like you, I found it hard to read. Luckily Werner did it for me! – Matthew Leingang Aug 19 '11 at 15:42
0

Another solution that does not require a new package, nor overriding hyperref parameters is to define a new command, e.g., slink:

\documentclass{article}

%minimum packages
\usepackage[colorlinks=true, linkcolor=blue]{hyperref} 

%custom link command
\newcommand{\slink}[2]{\hyperref[#1]{\underline{\smash{#2}}}} 

\begin{document}
    \section{Section Title\label{sect}}
    \slink{sect}{Here} is an \texttt{slink} to this very section 
\end{document}
  • 1
    Unfortunately, this will prevent longer links from wrapping around line ends. – AlexG Jan 29 '18 at 11:55
  • This is very similar to doncherry's answer, and my comments on that answer apply here. I wanted to style automatically generated hyperlinks. They all use \hyperref alone. A new user command wrapping around \hyperref doesn't address this use case. – Matthew Leingang Jan 29 '18 at 14:26
  • @MatthewLeingang: Ah, I didn't understand those comments, my mistake. Although, this addressed my use case and perhaps those of others who find this thread. Trying to figure out how to color and underline links with hyperref was how I found this thread: "How can I have colored and underlined links with hyperref?". This is a (possibly inelegant) way to do that. – k-dubs Jan 30 '18 at 6:36
  • @AlexG: I noticed that. Do you know how to avoid it? – k-dubs Jan 30 '18 at 6:37
  • 1
    Well, @Werner 's solution (under the heading "Automatic") does exactly what was requested: \hypersetup{...} configures the link text to be coloured while keeping it line-breakable and the code between \makeatletter and \makeatother patches hyperref to implement the PDF specification for setting the link border style to "Underline". That's all what is needed to fulfill the requirements. – AlexG Jan 30 '18 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.