URL

Overview

URLs can be typeset in various ways. You may specify a URL for later reuse via \useURL:

\useURL[aurl]       [http://xkcd.com/149/] [] [I prefer hot dogs.]


where #1 is the identifier, #2 is the URL you want to point to, and #4 the text to be displayed where the URL is used. #3 can be used for the 'file path' portion of the URL, if you want. This dates back to when hyperlink support in DVI viewers required that split.

Alternatively, you can use only two arguments and the URL will be used as its own text.

\useURL[anotherurl] [http://xkcd.com/224/]


(That way any character may appear inside the URL string without breaking things under certain circumstances, which can happen when you specify the URL itself as the fourth argument.)

Now that you have defined some URLs you are ready to dereference them by their identifier wherever you please. Don't forget to enable interaction for clickable WWW-look-and-feel.

\setupinteraction[state=start]
\starttext
\from[aurl]         % typesets the URL description in color
\from[anotherurl]   % typesets the URL in color
\stoptext


You can use

\url[aurl]


as well, which behaves like \from[#1].

E-Mail mailto

\setupinteraction[state=start]
\useurl [mymail] [mailto:nobody@example.zzz] [] [visible@mailaddress.zzz]

\starttext
Send your spam to the \from[mymail]!
\stoptext


Hyphenation

URLs tend to become large monsters under many circumstances but you may have a good reason not to conceal them from the reader. This is where hyphenation comes in handy. ConTeXt provides a dedicated mechanism for chopping them into pieces:

\hyphenatedurl{http://bits.blogs.nytimes.com/2010/03/05/a-former-book-designer-says-good-riddance-to-print/?hpw}


This has some characters predefined where Hans “likes” URLs to break (see the list at the beginning of lang-url.lua, cf. a message on ntg-context). If you prefer other characters you can add them via:

\sethyphenatedurlnormal{:=?&}
\sethyphenatedurlbefore{?&}
\sethyphenatedurlafter {:=}


URL hyphenation can, of course, be used wherever you need them.

\useURL[yaurl]      [http://xkcd.com/638/] [] [\hyphenatedurl{http://xkcd.com/638/}]
\starttext
\from[yaurl]
\stoptext


Note: the # sign should be escaped, that is, replaced by \#.

Other ways

Another way of typesetting URLs is the \goto{#1}[#2] command. This expects the description text as first argument and the actual URL as the second one; note that it has to be wrapped in url(#2) to create a clickable link:

\goto{In Lua, array indices start from one.}[url(http://www.xkcd.com/163/)]


Setting color and style

For \url:

\setupurl
[color=blue,
style=\tf]


For \goto and \from:

\setupinteraction
[state=start,
color=blue,
style=\tf]


Tipps and Tricks

If you need many hyperrefs in your document these definitions may be handy for you

\def\href#1#2{\useURL[#2][{#2}][][{#1}]\goto{\url[#2]}[url(#1)]}