Authors Top

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.


1. Introduction

In this tutorial, we’ll show how to write URLs (Uniform Resource Locators) in a LaTeX document using the url package.

We’ll focus on correctly displaying URLs and not on inserting clickable links, which are handled by the href package. Here, we cover the case where we prepare a document for printing so it isn’t necessary to have clickable links.

2. The url Package

Let’s see how we can use the url package with a simple example. We need to declare url at the top of our document, thus:

\usepackage{url}

We can then use it in our document as follows:

Three popular news websites are \url{www.cnn.com}, \url{www.bbc.com}, and \url{news.google.com}.

This yields the following output:

A simple example of the use of \url

3. Changing Fonts

We can change the font in which the URLs are displayed  by setting the style in the preamble:

\urlstyle{style}

The predefined styles are tt, rm, sf, and same.

The style tt refers to the typewriter font, rm denotes the roman font, and sf represents the sans-serif font. For instance, this is what we get with sf:

Using \urlstyle{sf} to change font to san-serif

The style same sets the font to that of the surrounding text. If we use:

\urlstyle{same}

in the above example, we’ll get:

using \urlstyle{same} to put urls in the same style as surrounding text

The URLs are now in the same font as the text we inserted them in.

4. Different URL Styles for Different Purposes

We may wish to have different font styles for different purposes. For instance, let’s say that we want to differentiate between email, website, and directory links. We can do that with \DeclareUrlCommand:

\usepackage{url}
\DeclareUrlCommand\email{\urlstyle{tt}}
\DeclareUrlCommand\website{\urlstyle{sf}}
\DeclareUrlCommand\directory{\urlstyle{same}}
\begin{document}
You can contact me at \email{[email protected]}. My website is \website{www.circus.com}.
All files referred to in this article are available in \directory{/home/circus/utilities}.
\end{document}

This gives us:

Example of different url styles for different purposes.

As we see, all the URL types follow different styles.

5. Multiline URLs

The url package never hyphenates a URL, as that would create ambiguity about whether the hyphen is a part of the URL.

Let’s take a look at the following example:

The website for Cormen, Leiserson, Rivest, and Stein's famous book is 
\url{mitpress.mit.edu/9780262046305/introduction-to-algorithms/}. 
The title of the book is {\em Introduction to Algorithms, Fourth Edition}.

LaTeX renders it like this:

The package url normally never breaks URLs over lines.

That’s a problem since the URL makes the text wider than the document. By specifying the hyphens option, we allow breaks at the hyphens that are already in the URL:

\usepackage[hyphens]{url}

This gives us:

\usepackage[hyphens]{url} will break gracefully over lines.

As we see, the URL is broken at the second hyphen to maintain the width of the document.

6. Greater Flexibility With the xurl Package

We often desire to have breaks at characters other than hyphens. To do so with the url package, we need to redefine or extend its internal commands, which may be too complicated for an ordinary user.

However, we can use the xurl package to permit breaks at any alphanumeric character (plus /.:*-'"~). This package can break URLs at any character it deems convenient, as we can see in the following examples:

\usepackage{xurl}
\begin{document}
Xxxxx yyyy zzzz wwwww ppppp qqqq rrrrrrrr aaaa bbbbb ccccc
\url{www.theshop.biz/The-Best-Speaker/ms/G0AQ69421N?ref"=Jan.xnum=DR9DZXAPHRMGW}.
\end{document}

This gives us:

xurl will break a URL at any location whether punctuation or not

The URL is broken at “=” simply by chance, because of the length of the preceding string.

For a slightly different input string:

Xxxxx yyyy zzzz wwwww ppppp qqq
\url{www.theshop.biz/The-Best-Speaker/ms/G0AQ69421N?ref"=Jan.xnum=DR9DZXAPHRMGW}.

we obtain:

xurl may break a URL in the middle of a word

There, we see that the URL string is broken in the middle of a word. This may or may not be acceptable in our application.

7. Conclusion

In this article, we showed how to URLs into a LaTeX document using the url and xurl packages. The former breaks URLs only at existing hyphens, whereas the latter can break a long URL at any alphanumerical character and some special characters.

Authors Bottom

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!