75

I want to set color, font and similar style attributes for the text in a \url from the hyperref package. How do I go about doing it? All I have found are comments saying that I probably don't want to do that. I want to because I am using ttf fonts for my texts and I want my urls to be in that ttf and not the default (typewriter font?) that the hyperref package gives me. I started with just trying to get it in italics but didn't even succeed with that:

\documentclass{memoir}
\usepackage{hyperref}
\begin{document}
Lala \url{\itshape www.example.com}.
\end{document}

Figure showing it not working

I am guessing there is some hook or something but I just can't find it. Maybe I have gone blind?

jonalv
  • 11,466

3 Answers3

71

Internally \url uses \UrlFont. You can change it with \urlstyle (see the documentation of url in url.sty) or by redefining \UrlFont:

\documentclass{memoir}
\usepackage{hyperref,xcolor}


\begin{document}
Lala \url{www.example.com}.

\urlstyle{sf}
Lala \url{www.example.com}.

\renewcommand\UrlFont{\color{red}\rmfamily\itshape}
Lala \url{www.example.com}.
\end{document}
Ulrike Fischer
  • 327,261
45

Use \href instead:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\href{http://www.example.com}{\textit{www.example.com}}.
\end{document}

This takes two arguments: the first is the actual url, the second is the link text. The above code produces

Sample output

Andrew Swann
  • 95,762
  • Also, it seems to not change the font into that typewriter one but keeps happily running with my ttf font. :) – jonalv Oct 26 '12 at 07:52
3

In my case I wanted to provide link colored in blue and to be clickable, also I wanted to display the url what I did what to setup a custom command my mixxing commands xcolor and hyperref packages:

\usepackage{hyperref}
\usepackage{xcolor}
\newcommand{\link}[1]{{\color{blue}\href{#1}{#1}}}

So using the snippet:

\documentclass[a4paper]{article}

\usepackage{hyperref}
\usepackage{xcolor}
\newcommand{\link}[1]{{\color{blue}\href{#1}{#1}}}

\begin{document}
\begin{itemize}
    \item \textbf{Desktop APP}: \link{https://github.com/signalapp/Signal-Desktop}
    \item \textbf{Android APP}: \link{https://github.com/signalapp/Signal-Android}
    \item \textbf{iOS APP}: \link{https://github.com/signalapp/Signal-iOS}
\end{itemize}
\end{document}

I have the following result:

Sample Result

  • 2
    Why? Just change the urlcolor setting for hyperref, then you can just use \url no need for href – daleif May 28 '18 at 16:42
  • In case you want speficic or multiple url formats (eg. Red for important stuff to read blue for others) I think my approach is by far better. – Dimitrios Desyllas May 28 '18 at 17:13
  • 1
    Which confuses users. {\hypersetup{linkcolor=blue}\url{#1}}, btw does \href hyphenate urls correctly? – daleif May 28 '18 at 17:17
  • 1
    It will work great until you have an URL with # or % or _ or & or $ or... – Phelype Oleinik Apr 25 '19 at 14:10
  • 1
    To make the link text customizable, you can use \newcommand{\link}[2]{{\color{blue}\href{#1}{#2}}}. Then you can do \link{https://www.google.com}{Google}. – erwaman Sep 24 '19 at 19:39