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:
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:
The style same sets the font to that of the surrounding text. If we use:
\urlstyle{same}
in the above example, we’ll get:
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:
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:
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:
[](/wp-content/uploads/sites/4/2022/09/img_6332792a114fa.svg)
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:
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:
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.