241

How do you write a URL in Latex?

The subscripts and everything else make the font look very strange when it compiles.

1
  • 11
    Plus if there are any _s in your URLs, Latex will not be nice to you. May 24, 2010 at 13:14

4 Answers 4

318

You can use \url

\usepackage{hyperref}
\url{http://stackoverflow.com/}
3
  • 10
    Is there a way to escape special characters like & or _ automatically, when its part of the url? Those characters are often getting used in URLs as separator for dynamic values.
    – gies0r
    Mar 12, 2019 at 23:56
  • 51
    Much better: \href{https://example.com}{links label} Apr 12, 2020 at 4:56
  • it doesnt show the second tag \url, what to do?
    – Gaurav
    Jul 12, 2020 at 22:59
58

You just need to escape characters that have special meaning: # $ % & ~ _ ^ \ { }

So

http://stack_overflow.com/~foo%20bar#link

would be

http://stack\_overflow.com/\~foo\%20bar\#link

4
  • 3
    The \verb command is much less work than that! May 24, 2010 at 13:21
  • 3
    The only problem with \verb is that it uses a typewriter font. I would have suggested \verbatim if that's what he asked for.
    – Gabe
    May 24, 2010 at 13:52
  • As Gabe and Charles point out, the hyperref package is very good at wrapping URLs that would otherwise cause unsightly overfull hboxes. Jul 14, 2015 at 0:00
  • 1
    This does not work correctly for me. For example, \url{https://asdfg.com\#123} produces # in text, however, the actual link it produces is https://asdfg.com%23123.
    – MaxPowers
    Aug 2, 2020 at 13:23
49

Here is all the information you need in order to format clickable hyperlinks in LaTeX:

http://en.wikibooks.org/wiki/LaTeX/Hyperlinks

Essentially, you use the hyperref package and use the \url or \href tag depending on what you're trying to achieve.

28

A minimalist implementation of the \url macro that uses only Tex primitives:

\def\url#1{\expandafter\string\csname #1\endcsname}

This url absolutely won't break over lines, though; the hypperef package is better for that.

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