6

This question already has an answer here:

I would like the output of \url{} and \href{} displayed identically. I have changed the color for both to be blue, but it appears as if a different for is being used as well:

enter image description here

Code:

\documentclass{book}
\usepackage{xcolor}
\usepackage{hyperref}

\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=blue}


\begin{document}
url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com}{www.yahoo.com}
\end{document}

marked as duplicate by Werner, user13907, Jesse, cfr, Malipivo Sep 20 '14 at 1:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @Manuel I don't quite understand your comment. Wasn't that the question? That is, why they are using different fonts? – cfr Sep 20 '14 at 0:29
  • @Manuel Oh. I see. I read 'different for' as a typo for 'different font'. – cfr Sep 20 '14 at 0:47
11

url changes the font but you can set it to default to serif to match \href:

\documentclass{book}
\usepackage{xcolor}
\usepackage{hyperref}

\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=blue}
\urlstyle{rm}

\begin{document}
url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com}{www.yahoo.com}
\end{document}

url styling

Note that \urlstyle{} is from url which hyperref is loading. (You can use url without hyperref - something I often do, in fact.) I can't see anywhere this is really explained in hyperref's documentation but I only searched the PDF manual and there is a lot more of it so no doubt it is covered somewhere.

Update

If you would prefer the style produced by \href to match the \url default - or if you would like both of them to use a different style, such as sans serif - you can use \url{} to format the link text produced by \href{}:

\documentclass{book}
\usepackage{xcolor}
\usepackage{hyperref}

\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=blue}
\urlstyle{sf}% sans for a change

\begin{document}
url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com}{\url{www.yahoo.com}}

\urlstyle{tt}% back to url's default

url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com}{\url{www.yahoo.com}}

\end{document}

More url styles...

  • 1
    Aswesome. Just wondering is there a \hrefstyle{} equivalent? Interestingly \urlstyle is not mentioned in the hyperref documentation. – Peter Grill Sep 20 '14 at 0:31
  • @PeterGrill Honestly, I have no idea. I just happened to know that url changes the font to the default tt family. I have generally used \url to style the text within the \href command or wrapper. Although I assume I originally did that because I couldn't do it with hyperref alone, that was quite a while ago. – cfr Sep 20 '14 at 0:34
  • @PeterGrill Add \listfiles and look at the output. I am pretty sure you will find url listed among the packages. hyperref.sty includes \RequirePackage{url} but it is buried in code I don't understand. EDIT: Yes, I get url.sty 2013/09/16 ver 3.4 Verb mode for urls, etc.. \urlstyle is from that package. – cfr Sep 20 '14 at 0:38
  • 2
    \urlstyle is a feature of package url, which is loaded by hyperref. There is no \hrefstyle, because the second argument of \href can contain arbitrary stuff including images. – Heiko Oberdiek Sep 20 '14 at 1:11
  • 1
    @cfr I should have been more specific. Downside of using \url inside \href in a beamer context is that the link dies, at least for me it did. Which is why I needed \nolinkurl to keep them working. Your mileage may vary. – Joris Meys Mar 20 '18 at 10:12
10

The second argument of \href is arbitrary text. If the text should appear as in \url, then \nolinkurl can be used, that acts as \url without the link part, which is already provided by \href.

The font can be changed via \urlstyle, see the documentation of package url

Example:

\documentclass{book}
\usepackage{xcolor}
\usepackage{hyperref}

\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=blue}


\begin{document}
url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com/}{\nolinkurl{www.yahoo.com}}

\medskip
\urlstyle{sf}
url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com/}{\nolinkurl{www.yahoo.com}}

\medskip
\urlstyle{rm}
url: \url{www.yahoo.com}

href: \href{http://www.yahoo.com/}{\nolinkurl{www.yahoo.com}}

\end{document}

Result

1

Check out this solution: https://tex.stackexchange.com/a/201952/62165. It might solve your problem. It is based on \href but you could use it as a \url replacement by making the two input parameters the same, ie, \click{www.yahoo.com}{www.yahoo.com}. Aternatively you could modify the \click command to take only one parameter. It doesn't change the background font, which is probably what you are trying to achieve. It ignores all \hyperref defaults.

Not the answer you're looking for? Browse other questions tagged or ask your own question.