53

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?

52

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

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

  • 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 7:52
1

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

  • 1
    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 at 14:10

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.