71

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?

1

3 Answers 3

68

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}
2
  • 3
    The package option urlcolor in the package hyperref does the same thing, i.e. \usepackage[urlcolor=red]{hyperref}.
    – Ronny
    Oct 25, 2012 at 13:02
  • 10
    You can simply use \urlstyle{rm}.
    – Javad
    Jul 16, 2015 at 20:00
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

1
  • Also, it seems to not change the font into that typewriter one but keeps happily running with my ttf font. :)
    – jonalv
    Oct 26, 2012 at 7: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

5
  • 1
    Why? Just change the urlcolor setting for hyperref, then you can just use \url no need for href
    – daleif
    May 28, 2018 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. May 28, 2018 at 17:13
  • 1
    Which confuses users. {\hypersetup{linkcolor=blue}\url{#1}} , btw does \href hyphenate urls correctly?
    – daleif
    May 28, 2018 at 17:17
  • 1
    It will work great until you have an URL with # or % or _ or & or $ or... Apr 25, 2019 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, 2019 at 19:39

You must log in to answer this question.

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