# How to patch \href to look like \url?

There are at least two questions, that ask for this without mentioning it explicitly, so there we go.

Neither this nor actually answer the questions, how to make \href style the same way as \url - without additional commands in the second argument of \href. I've seen Heiko's comment in another related question, that \href is much more complicated, since it supports images and whatnot. But wouldn't patching \href to additionally include a \texttt be rather benign?

However, \href looks rather complicated; how would one patch it the least invasive way to sneak in an enforced monospaced fontstyle?

My naive solution/MWE simply redefines \href and inserts a \texttt, which works fine in the trivial test case(s), but I have no idea about consequences in more complex usages of hyperref.

\documentclass{article}

\usepackage{letltxmacro}

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

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

\bigskip
url: \url{www.yahoo.com}

% patch it - usually in the preamble of course...
\LetLtxMacro\oldhref\href
\renewcommand{\href}[3][]{%
\oldhref[#1]{#2}{\texttt{#3}}
}

\bigskip
url: \url{www.yahoo.com}

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

\medskip
yay!

\end{document}


What are possible consequences of this (test cases that would break it) and how to do it better?

• Your patch will break if the url contains special chars like # or %. – Ulrike Fischer Jul 11 '19 at 12:56

You can add \ttfamily to \href like this. I added a key option to change it locally too

\documentclass{article}

\makeatletter
\define@key{href}{font}{#1}
\makeatother
\usepackage{xpatch}
\newcommand\hrefdefaultfont{\ttfamily}
\xpatchcmd\href{\setkeys{href}{#1}}{\setkeys{href}{font=\hrefdefaultfont,#1}}{}{\fail}

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

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

some text
\href[font=\rmfamily\bfseries]{http://www.yahoo.com/}{www.yahoo.com}

\end{document}


• This solution breaks line breaking (no pun intended). – stefanct Jul 11 '19 at 19:03
• As does your texttt solution. Without an url command there are no url like breaks. – Ulrike Fischer Jul 11 '19 at 19:19
• Well, yeah, that's why I called my PoC naive and made it part of the question :) I don't get the meaning of your second sentence. Without the patch URLs in \href get wrapped - no direct \url command needed. It overshoots more but at least it does something. With your patch there is no wrapping at all. – stefanct Jul 11 '19 at 23:08
• no, there is no wrapping is www.yahoo.com, with or without the patch. When forcing the font to ttfamily you disable hyphenation as ttfamily normally doesn't have hyphenation. You can use \newcommand\hrefdefaultfont{\ttfamily\hyphenchar\font=\defaulthyphenchar} to enable it, but it will only work for normal words, not for url like stuff with periods and slashes. url don't use hyphenation but mathbreaks. – Ulrike Fischer Jul 12 '19 at 8:40
• A simple www.yahoo.com link doesn't with \url but it would break at slashes (and hyphens if enabled) at least. I didn't know that there is no hyphenation at all in tt - that probably explains a lot actually :D After more thought I think I want something different altogether anyway. Thanks for the insights! – stefanct Jul 12 '19 at 11:29

This does not answer my question but I think it might still be useful to some...
After some thought I ended up with something completely different: a \url that has two arguments with the same basic idea as \href but where the second one is optional:

\documentclass{article}

\PassOptionsToPackage{hyphens}{url}

\usepackage{xparse}
\usepackage{letltxmacro}

\LetLtxMacro\oldurl\url
\RenewDocumentCommand \url{mg}{%
\IfNoValueTF{#2}{%
\oldurl{#1}%
}{%
}
}

\begin{document}

\smallskip
\noindent\rule{\linewidth}{1ex}

\smallskip
\noindent url\hfill\hspace{0.6\linewidth}\url{https://www.yahoo.com/break/me}

\smallskip

With this you can easily create links that are formatted the same as with a plain \url but are still able to customize the displayed characters (e.g., leaving out the https://). Also the interface is much nicer and easy to remember - no need to repeat the \nolinkurl all the time as in other solutions.