27

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

\url{ipython.org}

instead of

\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!

3 Answers 3

24

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

\documentclass{article}
\usepackage{xcolor}
\usepackage[
  colorlinks,
  linkcolor=magenta
]{hyperref}
\newcommand\rurl[1]{%
  \href{http://#1}{\nolinkurl{#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.

enter image description here

If you have long URLs, load xurl.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xurl}
\usepackage[
  colorlinks,
  linkcolor=magenta
]{hyperref}

\newcommand\rurl[1]{%
  \href{http://#1}{\nolinkurl{#1}}%
}

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

This is a very long link \rurl{tex.stackexchange.com/questions/139921/how-to-avoid-the-http-part-in-hyperrefs-url/139934?noredirect=1\#comment1699916_139934}

\end{document}

enter image description here

11
  • 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, 2019 at 10:40
  • @Tobi Not from the top of my head.
    – egreg
    Jan 19, 2019 at 10:48
  • 1
    An obvious comment: nowadays, you will often want to use "https" instead of "http" in the command.
    – a3nm
    Sep 15, 2020 at 10:13
  • @a3nm Of course; one should use the right URL. Seven years ago it was very different.
    – egreg
    Sep 15, 2020 at 10:21
  • That's not a solution because the link from href is messing up the hyphenation of the url and I can't figure out for the life of me how to make it work.
    – Sibylse
    Feb 2, 2023 at 15:31
9

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:

enter image description here


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

\documentclass{article}
\usepackage[colorlinks]{hyperref}

\usepackage{xcolor,url}

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

\begin{document}

\url{local-link}

\shorturl{ipython.org}

\end{document} 

Output:

enter image description here

2
  • 3
    uh, I think the request was for a printed version without the visible http://. Oct 21, 2013 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... Oct 23, 2013 at 14:30
2

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
  \DeclareRobustCommand*\my@url@[2][]{\endgroup\href{#1#2}{\nolinkurl{#2}}}
  \DeclareRobustCommand*\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}

MWE output

You must log in to answer this question.

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