119

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.

2

5 Answers 5

90

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

8
  • 2
    I wonder if there is a way to patch hyperref so that the links use soul's \ul and no border-underlining?
    – doncherry
    Commented Aug 19, 2011 at 15:53
  • 5
    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. Commented Sep 11, 2013 at 15:27
  • @Werner As Jairo Bochi said, this does not work for macOS preview, is there a way for it to work in preview? Thanks!
    – zyy
    Commented Aug 8, 2018 at 16:03
  • Is it possible to raise the underline up a bit higher? It's so low that it bumps against the next line of text Commented Sep 28, 2021 at 0:15
  • 1
    @Adam_G: linkbordercolor=blue
    – Werner
    Commented Jan 12 at 19:41
20

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

2
  • 1
    @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
    Commented Aug 19, 2011 at 11:39
  • 1
    This worked for me to get underlined clickable links: \newcommand{\myhy}[2]{\href{#1}{\color{blue}\setulcolor{blue}\ul{#2}}} ` Commented Nov 19, 2019 at 15:23
3

If you only need it a couple times you don't need a new command or any extra packages. Just add underline and color to the link label:

\usepackage{hyperref}
\usepackage{xcolor}
\href{https://www.example.com}{[{\underline{\textcolor{blue}{link}}}]}
4
  • 1
    This, like the other answer by k-dubs prevents longer links from wrapping. But still, thanks for your answer. Commented Jul 12, 2022 at 20:25
  • -1 The question is about styling the internal hyperlinks generated by hyperref automatically. This answer shows how to style external hyperlinks manually. Commented Aug 2, 2022 at 12:55
  • @MatthewLeingang well then by all means downvote it so that it won't even be visible to the people who would've found it helpful because if it wasn't exactly what YOU needed
    – Ben Wex
    Commented Aug 6, 2022 at 20:53
  • Nothing personal, just pointing out that the answer is orthogonal to the question. This is a Q&A site, not a wiki. If you want to write an answer about something else (perhaps for the benefit of googlers who landed here in search of something else), go ahead, but the answer should say that it's addressing a related-but-different question. Then I'll retract the downvote. Commented Aug 6, 2022 at 21:20
1

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}
5
  • 2
    Unfortunately, this will prevent longer links from wrapping around line ends.
    – AlexG
    Commented Jan 29, 2018 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. Commented Jan 29, 2018 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
    Commented Jan 30, 2018 at 6:36
  • @AlexG: I noticed that. Do you know how to avoid it?
    – k-dubs
    Commented Jan 30, 2018 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
    Commented Jan 30, 2018 at 10:02
1

None of the solutions up to this point have worked in my case. What I tried to resolve? Display all external links (URLs) colored black and underlined with 1px line (resume). There's a single requirement, no custom styles for href needed.

The original answer (https://tex.stackexchange.com/a/26085/125035) has almost everything what I need except one small thing. It does not work with actual URLs leading to external web resources, e.g. link on a twitter account.

Be careful and double check what exactly you want to color & underline. The official hyperref documentation helps me a lot to figure out. In example, check out section '5.6 Extension options' and read more about the difference between linkcolor, urlcolor and other *color parameters.

I also found the solution in another question. You can use the code from the answer, it works just okay. IMO the \AtBeginDocument{\hypersetup{pdfborderstyle={/S/S/W 1}}} thing is kind of workaround in my simple case. My solution is:

\usepackage{hyperref}
\hypersetup{
    urlbordercolor = black,
    pdfborderstyle={/S/U/W 1},
}

As a result, \href{mailto:[email protected]}{[email protected]} external URL displays the right way.

.pdf resume example

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .