7

Let's take the following MWE:

\documentclass{article}

\usepackage{hyperref,xcolor}

\begin{document}

\href{http://any-URL}{\color{blue}{\underline{Some URL}}}

\end{document}

Let's assume I would like to have every second argument of every \href in the document styled within \color{blue}{\underline{...}.

How would that be possible, via a definition?

2
  • Using \underline would not be optimal as it does not allow line-breaking.
    – Werner
    May 24, 2016 at 6:09
  • \hypersetup{allbordercolors=0 0 1, pdfborderstyle={/S/U/W 1}}. That's for all border colors. There are also: citebordercolor, filebordercolor, linkbordercolor, menubordercolor, urlbordercolor, and runbordercolor for a more fine-grained approach.
    – jon
    May 24, 2016 at 6:11

5 Answers 5

5

\href has to do quite a lot \catcode-magic to handle all the special chars (like #) in urls, so all commands that take an argument and so fix the \catcodes are difficult to insert. You can try the following. But

  • Imho underlining doesn't look good.
  • It will only work for \href (I hope ...)
  • Normal text will break over lines, urls probably not.
  • \ul from soul will not work instead of \uline.

Code

\documentclass{article}
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[normalem]{ulem}
\usepackage{xcolor}


\makeatletter
\begingroup
  \catcode`\$=6 %
  \catcode`\#=12 %
  \gdef\href@split$1#$2#$3\\$4{%
    \hyper@@link{$1}{$2}{\uline{$4}}% or \underline
    \endgroup
  }%
\endgroup

\begin{document}

\href{http://any-URL}{Some URL}

\href{http://any-URL-with#hash}{Some URL}

\href{http://any-URL-with#hash}{\nolinkurl{http://any-URL-with\#}}

\end{document}

enter image description here

5

A simpler variant of Ulrike's solution, based on my answer to How to be able to use the number sign (#) in the URL of an underlined href

\documentclass{article}
\usepackage{xcolor,soul}
\usepackage{etoolbox}
\usepackage{hyperref}

\hypersetup{colorlinks,urlcolor=blue}

\makeatletter
\patchcmd{\hyper@link@}
  {{\Hy@tempb}{#4}}
  {{\Hy@tempb}{\ul{#4}}}
  {}{}
\makeatother

\begin{document}

\href{https://tex.stackexchange.com/#/!!!}{URL with a number sign}

A hyperlink \href{https://tex.stackexchange.com/#/!!!}{URL with a number sign
and text long enough to trigger a line break} with something following.

\end{document}

enter image description here

4

Adapting Werner's Lorem ipsum example, another (internal-to-hyperref) possibility is this:

\documentclass{article}

\usepackage{hyperref}
\hypersetup{
  allbordercolors=0 0 1,
  pdfborderstyle={/S/U/W 1}
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut
\href{http://any-URL}{pellentesque augue} est, id ornare nisi
fringilla eu. Nulla euismod sollicitudin lacus, et porta lectus
accumsan ut. Vestibulum quis interdum lorem. Sed sodales fermentum
neque eu auctor. Vestibulum vitae eros nec massa ultricies
\href{http://any-URL}{sodales vel vitae} justo. Integer a mauris
lectus. Aliquam eu diam vehicula velit lacinia congue. Sed ac mollis
arcu, eu viverra dui. Nunc at elit mi. Duis elementum pulvinar
placerat. Phasellus vel massa varius quam mattis fermentum sed mollis
arcu. \href{http://any-URL}{Vestibulum ante ipsum primis in} faucibus
orci luctus et ultrices posuere cubilia Curae; Aenean dolor elit,
consequat nec tellus luctus, vestibulum fermentum orci. Nullam nec leo
eros. Nullam at quam a mauris luctus euismod ac eu dolor.

\end{document}
4
  • I can not get this to work. Instead, I get blue frames around the href's, not under-lines; or is that a .PDF-viewer-specific effect?
    – O0123
    May 24, 2016 at 6:29
  • Yes. I see only a blue line with: evince, xpdf, and zathura (and with xdvi for the .dvi file). With Ghostscript, I see a blue box; with qpdfview, I see a red box; and with mupdf, I see no line or box. Note that one advantage of this approach is that you can print the file and not have the lines or boxes appear; but the downside is, I guess, that not all PDF viewers implement the PDF standard in the same way (or to the same degree).
    – jon
    May 24, 2016 at 6:42
  • Thank you very much for digging more into the limitations. That's very useful for readers. Vote up +1 for that. -- Would you know how to be able to use number signs (#) in URL's in Werner's answer please, as I personally prefer a .PDF-viewer-independent answer, since the lay-out is most important in my use case.
    – O0123
    May 24, 2016 at 6:49
  • Not off-hand, I'm afraid. If the hyperref developer stops by this question, however, I'm sure you'll get a very detailed and accurate answer to your question.
    – jon
    May 24, 2016 at 7:02
3

You can set a specific style using something like this:

enter image description here

\documentclass{article}

\usepackage{hyperref,xcolor,soul}

\let\oldhref\href
\renewcommand{\href}[2]{\oldhref{#1}{\hrefstyle{#2}}}
\newcommand{\hrefstyle}[1]{\color{blue}\ul{#1}}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut \href{http://any-URL}{pellentesque augue} est, 
id ornare nisi fringilla eu. Nulla euismod sollicitudin lacus, et porta lectus accumsan 
ut. Vestibulum quis interdum lorem. Sed sodales fermentum neque eu auctor. Vestibulum 
vitae eros nec massa ultricies \href{http://any-URL}{sodales vel vitae} justo. Integer a mauris lectus. Aliquam 
eu diam vehicula velit lacinia congue. Sed ac mollis arcu, eu viverra dui. Nunc at elit 
mi. Duis elementum pulvinar placerat. Phasellus vel massa varius quam mattis fermentum 
sed mollis arcu. \href{http://any-URL}{Vestibulum ante ipsum primis in} faucibus orci luctus et ultrices 
posuere cubilia Curae; Aenean dolor elit, consequat nec tellus luctus, vestibulum 
fermentum orci. Nullam nec leo eros. Nullam at quam a mauris luctus euismod ac eu dolor.

\end{document}

Opting for \ul (from soul) instead of \underline. See Why does underlined text not get wrapped once it hits the end of a line ?.

2
0

Here is the code to add at the start of your document for reference in darkgreen.

\usepackage{xcolor}
\usepackage{hyperref}
\definecolor{darkgreen}{rgb}{0.06, 0.78, 0.3}
\hypersetup{            % reference colors
colorlinks=true,
linkcolor=darkgreen,
pdfborder = {0 0 0},
filecolor=magenta,
urlcolor=cyan,
}
\hypersetup{linkcolor=black}
4
  • The colouring doesn't seem to be working.
    – O0123
    May 24, 2016 at 11:27
  • Just tested it now, and it worked. However, you need to build one time only with the new packages before building with the hypersetup. Don't know why but it's working for me, and my links are all in the color they are supposed to be.
    – Nakrule
    May 25, 2016 at 8:50
  • Sorry mate, I can't replicate it.
    – O0123
    May 25, 2016 at 8:54
  • Try this code, work for me : \documentclass[french]{article} \usepackage{xcolor} \usepackage{hyperref} \definecolor{darkgreen}{rgb}{0.06, 0.78, 0.3} \hypersetup{ % reference colors colorlinks=true, linkcolor=darkgreen, pdfborder = {0 0 0}, filecolor=magenta, urlcolor=cyan, } \hypersetup{linkcolor=black} \begin{document} Here is my reference: \href{www.google.ch}{Google} \end{document}
    – Nakrule
    May 25, 2016 at 8:58

You must log in to answer this question.

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