22

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!

19

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

4
  • 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
10

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
  • 2
    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
0

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
  \NewDocumentCommand\my@url@{O{} m}{\endgroup\href{#1#2}{\nolinkurl{#2}}}
  \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}

MWE output

0

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

\renewcommand\url[1]{
    \href{https://#1}{\nolinkurl{#1}}
}

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

enter image description here

and clicking on it will open https://www.google.com

But unfortunately this will break links in the references section.

2
  • That would be why the accepted answer from 7 years ago does exactly what you've proposed, but suggests a differently named function. – Teepeemm Apr 23 at 11:58
  • @Teepeemm Yes I know. – ruohola Apr 23 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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