How to insert url, link and directory in LaTeX?

When working with different types of documents in LaTeX, the need of putting an email address, a website link or directory path etc may arise.

In LaTeX serious attention is needed because putting various types of information mentioned above may entail that characters as , #, &, { or } etc be used.

Another need is that the information has to completely appear on a line without breaks and if there must be breaks, they need to be treated with special attention so that the break can only happen in a particular portion.

An example of a practical problem is when the website address breaks around a hyphen.

This makes it difficult to identify whether the hyphen(-) was inserted because of a break or is part of the website address.

An immediate solution would be to use the \verb command, however, if the address does not fit the current line, it will stick out into the right margin.

Solution to such problems is provided by the url package in the \url{...} command.

URL with url package

url package developed by Donald Arseneau helps to properly insert email addresses and websites into a document.

This package takes an optional argument, hyphens which allows addresses or websites to be broken at non-letters between words without a dash or hyphen (-).

In addition, the package prints out all characters just as they are given, including special characters.

Another reason that makes the url package stand out is its ability to conform to a logical markup making it possible to understand the purpose of its arguments. Syntax will be

\url{.....}
\url!.....!
\url=....=
\documentclass{article}
\usepackage{url}
\begin{document}
  Visit our site at \url{https://www.physicsread.com}
  Visit our site at \url!https://www.physicsread.com!
  Visit our site at \url=https://www.physicsread.com=
  Use my email \url{[email protected]}
\end{document}

Output :

In this case url command is used to insert the link. Which is implemented in three ways.

Examples above demonstrate how to use \url command to typeset websites or emails or just common links. By default the websites are formatted in typewriter font.

\documentclass{article}
\usepackage[hyphens]{url}
\begin{document}
  We have an article that demonstrates the number of valence electrons contained in carbon. 
  Read this article by visiting \url{https://www.mysite.com/how-many-valence-electrons-does-carbon-have/}
\end{document}

Output :

As shown in this figure, the link will break where the hyphen is present.

There is equally a package by name xurl that makes use of \url command that handles the website breaking with even much flexibility. Below is same example with this package.

\documentclass{article}
\usepackage{xurl}
\begin{document}
  We have an article that demonstrates the number of valence electrons contained in carbon. 
  Read this article by visiting \url{https://www.physicsread.com/how-many-valence-electrons-does-carbon-have/}
\end{document}

Output :

Anywhere the url breaks, will be placed on the next line. This package does not break after some special characters.

While url package is very selective to which point it breaks the url, xurl package breaks url just at any point.

Changing url fonts

The url package provides \urlstyle{...} command which takes one mandatory which is the name of font.

Font can either be typewriter denoted tt (default), roman font denoted rm, or sans serif denoted sf or same which simply maintains url to have same font as that of text.

\documentclass{article}
\usepackage{url}
\urlstyle{tt}
\begin{document}
  Visit our site at \url{https://www.physicsread.com}
\end{document}

Output :

Of course, with Latex you can change the font of the link. Which is represented in this figure.

Multiple url styles

A need might arise where you desire to have multiple font styles for different purposes within your document.

There is a way achieve multiple styles in LaTeX by making use of \DeclareUrlCommand

\documentclass{article}
\usepackage{url}
\DeclareUrlCommand\email{\urlstyle{rm}}
\DeclareUrlCommand\website{\urlstyle{sf}}
\begin{document}
  Feel free to send the mails to me at \email{[email protected]}. 
  Do well to always check the latest LaTeX tutorials at \website{www.physicsread.com}
\end{document}

Output :

You can add multiple fonts at once.

Two fonts are very visible and easy to differentiate from output

Defining custom url

Given that you desire to print out a particular url in multiple instances in your document.

LaTeX provides a means to custom define command for convenience sake. Commands to do this is:

\urldef{command}{url-command}{text}
\urldef{command}{url-command}=text=

The two commands do the same thing

\documentclass{article}
\usepackage{url}
\urldef{\mysite}\url{https://www.physicsread.com}
\begin{document}
  Visit \mysite to read the Latest articles based on \LaTeX{}
\end{document}

Output :

LaTeX's most flexible feature is defining custom commands. You can do it easily in this case too.

You can henceforth, use \mysite anytime you want to print https://www.physicsread.com

URL with hyperref package

hyperref package enhances urls to be printed in documents as links. hyperref package simply using some few parameters.

This makes the url to be clickable taking you to a specific location. Some of the parameter settings provided by hyperref package as optional arguments are:

linkcolor
urlcolor
colorlinks
\documentclass{article}
\usepackage{url}
\usepackage[colorlinks=true,urlcolor=teal,linkcolor=blue]{hyperref}
\begin{document}
   Visit \url{https://www.physicsread.com} to read the Latest articles on \LaTeX{}
\end{document}

Output :

Links are always highlighted by different colors. It is done here with blue color.

Conclusion

This tutorial guides you on how to properly insert URLs in your document with all the different ways to enhance your workflow and achieve beautiful looking link and directory path in your work.

Continuous practice will give you proper mastery on “how to properly deal with URLs in LaTeX”.

Md Jidan Mondal

LaTeX expert with over 10 years of experience in document preparation and typesetting. Specializes in creating professional documents, reports, and presentations using LaTeX.

Leave a Comment

Your email address will not be published. Required fields are marked *