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!

hrbjoern
  • 401

3 Answers3

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

egreg
  • 1,121,712
  • 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
  • 1
    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
  • 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 02 '23 at 15:31
  • @Sibylse Sorry, but that's quite unclear. Can you please open a new question with the needed details to reproduce your issue? I don't think it's something solvable in comments. – egreg Feb 02 '23 at 15:35
  • @Sibylse Twice sorry for the gratuitous downvote without giving any hint of what is being wrong with your code. – egreg Feb 02 '23 at 16:41
  • I'm saying that there is no linebreak happening in your URL, it just goes over the page width. – Sibylse May 09 '23 at 13:41
  • @Sibylse That's a common problem with URLs. Try loading xurl. Anyway, I consider your behavior quite rude: you dowvoted more than three months ago and I replied a few minutes later. Now you're being a little more precise about the issue, which would have warranted a followup question, rather than a gratuitous downvote. – egreg May 09 '23 at 15:15
  • @Sibylse Anyway, I added the code for long URLs, so you can retract your downvote and repair your rudeness. – egreg May 09 '23 at 15:21
  • With url there was no problem with the linebreak but when I used your code it was a problem. So your answer didn't provide all the functionality that URL had. Don't get why it's then supposedly rude to downvote when it doesn't solve the problem in its entirety, only for specific cases. Thanks for the addition. – Sibylse May 10 '23 at 16:04
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

karlkoeller
  • 124,410
  • 3
    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
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

schtandard
  • 14,892