# How to avoid the “http:” part in hyperref's \url?

is it possible to use the \url command from the hyperref package with "shorthand" URLs, e.g.

\url{ipython.org}


\url{http://ipython.org} ?


It seems that the former would only link to local files on my computer, instead of retrieving the web address. (I suppose I could be using \href, but \url is shorter and I'd like to keep its formatting features.)

Edit in response to karlkoeller's answer:

Sorry for not being sufficiently precise. I don't mind typing the 'http' part so much, but I'd rather not see it printed.

egreg's answer does what I want to do - many thanks!

It's better to use a different command, so that its usage is clear:

\documentclass{article}
\usepackage{xcolor}
\usepackage[
]{hyperref}
\newcommand\rurl[1]{%
}

\begin{document}
This is a link \rurl{ipython.org} written in abbreviated form.
\end{document}


Of course you lose the tricks \url and \href are able to do with complex URI, for handling special characters, but short URI like this one should not have this problem.

The color in the image shows that it's indeed a link to a Web page.

• Is there a way to make this work with urls like http://domain.tld?a=b&c=12_23#target, i.e. containing special characters? – Tobi Jan 19 '19 at 10:40
• @Tobi Not from the top of my head. – egreg Jan 19 '19 at 10:48
• An obvious comment: nowadays, you will often want to use "https" instead of "http" in the command. – a3nm Sep 15 '20 at 10:13
• @a3nm Of course; one should use the right URL. Seven years ago it was very different. – egreg Sep 15 '20 at 10:21

You can load the package url and redefine the command \url as follows:

\DeclareUrlCommand\url{\color{magenta}\def\UrlLeft{http://}\urlstyle{tt}}


Note that the xcolor package is required, too, if you want to give a color to the link.

Now you can simply write

\url{ipython.org}


and http:// is added automatically.

Complete example

\documentclass{article}
\usepackage{hyperref}

\usepackage{xcolor,url}

\DeclareUrlCommand\url{\color{magenta}\def\UrlLeft{http://}\urlstyle{tt}}

\begin{document}

\url{ipython.org}

\end{document}


Output:

Of course, you can leave the \url command as is and define a new command \shorturl so to have

\documentclass{article}

\usepackage{xcolor,url}

\DeclareUrlCommand\shorturl{\color{magenta}\def\UrlLeft{http://}\urlstyle{tt}}

\begin{document}

\shorturl{ipython.org}

\end{document}


Output:

• uh, I think the request was for a printed version without the visible http://. – barbara beeton Oct 21 '13 at 23:04
• @barbarabeeton you're right, I'm seeing it now. But the question wasn't so clear to me before the OP edited the original post. And the only way I know to achieve this is already in egreg's answer... – karlkoeller Oct 23 '13 at 14:30

I use the follwing patch to add an optional argument to \url that is included in the link address but not printed. You can use "special" characters like _, $, & and % in both parts of the url (as in the original \url), but there must not be any linebreaks in the arguments, between the arguments or between the macro and the arguments. (This could be sorted out – the original macro does this –, but it has to be done with great care and for me it's just not worth the effort, as I don't deem this a relevant restriction.) \documentclass{article} \usepackage{etoolbox} \usepackage{xparse} \usepackage[colorlinks]{hyperref} \makeatletter % \url[no-print-prefix]{url} \begingroup \endlinechar=-1 % \catcode\^^A=14 % \catcode\^^M\active \catcode\%\active \catcode\#\active \catcode\_\active \catcode\$\active
\catcode\&\active
\let\my@url\hyper@normalise
\patchcmd\my@url{\hyper@n@rmalise}{\my@url@}{}{}^^A
\global\let\my@url\my@url
\endgroup
\def\url{\my@url}
\makeatother

\begin{document}

This is a link written in abbreviated form: \url[https://]{tex.stackexchange.com}

Special characters work, too: \url[https://]{en.wikipedia.org/w/index.php?search=LaTeX&title=Special%3ASearch}

\end{document}


If you simply want change the default behavior of \url, you can use:

\renewcommand\url[1]{

This will make it so that when you use \url{www.google.com}` it will be rendered as: