Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I wish to typeset some relatively long URLs in a piece of text, and when I use \url{..}, the resulting text does not respect the margin boundaries that govern the main text body, instead going all the way to the edge of the paper before wrapping around.

For reference, my preamble looks like this:

\documentclass[12pt]{article}
\usepackage{mathptmx}
\usepackage{fullpage}
\usepackage[pdftex]{hyperref}

and example of the url I'm including is:

\begin{itemize}
\item Some text

\url{very-long-url}
...
\end{itemize}

I'm compiling using pdflatex, although I doubt this makes a difference.

share|improve this question
4  
Alas, a quick google search found my answer (use the [hyphens] option to the url package). Not sure if I should leave the question up here for future reference or just delete it. –  Suresh Sep 13 '10 at 4:01
2  
It seems like a reasonable question. You should make your comment an answer. Wait a bit, if no one else has a good answer, just accept your own comment. I think there's even a badge for it. –  TH. Sep 13 '10 at 5:01
    
You could also perhaps consider a service such as tinyurl to shorten it. Long urls are plain ugly in a publication. –  Yiannis Lazarides Sep 13 '10 at 14:30
2  
... but URL shortening (a) makes the destination opaque and (b) adds another indirection layer vulnerable to linkrot. –  Reid Sep 16 '13 at 20:09
    
I also suggest to consider not solving the problem but use short URLs instead. There are several services like goo.gl which will help you here. If the document is printed, then its even easier to transfer the URL to Browser. –  math Nov 28 '13 at 8:32

9 Answers 9

up vote 55 down vote accepted

(converting a previous comment to an answer)

A quick google search (alas, too quick) reveals one solution. Use the [hyphens] option with the url package:

\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}

Some recommendations also include inserting \sloppy in case latex is trying too hard to align things.

share|improve this answer
3  
Do you mean that it should look like this: \usepackage{url}[hyphens] ? This does not work for me. –  sixtyfootersdude Oct 30 '10 at 15:08
10  
actually it would be \usepackage[hyphens]{url} –  Suresh Oct 31 '10 at 6:48
1  
Did you use both hyperref and url packages? I got an error saying a package conflict. –  xport Jun 14 '11 at 22:20
29  
@xport: hyperref loads the url package internally. Use \PassOptionsToPackage{hyphens}{url}\usepackage{hyperref} to pass the option to the url package when it is loaded by hyperref. This avoids any package option clashes. –  Martin Scharrer Jul 12 '11 at 11:26
2  
@Suresh A minimal usage-example with some blind text would be nice. –  moose Sep 20 '12 at 4:51

I carefully read the documentation of the url package (I'm using PDFLaTeX) and found the following to work quite nice to let LaTeX add line breaks after every normal alphabetic character:

\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks%  save the current one
  \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
  \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
  \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
  \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
  \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
  \do\Y\do\Z}
share|improve this answer
    
None of the previous (higer-rated) suggestions worked for me with a troublesome URL I was dealing with. This option did work. Are there any potential problems with this approach? All I see is that it may not always break at a "visually appealing" part of a URL. –  SSilk Jun 25 '12 at 13:19
    
Great! Works for me too, thank you. –  Aubergine Dec 31 '12 at 15:09
4  
This is a bad solution, because it removes all characters predefined within \UrlBreaksin the package. This can mislead other users, see e.g. Dots are smaller when UrlBreaks defined. –  Speravir Feb 25 '13 at 17:51
    
I used the same solution, but with UrlBigBreaks. Does anybody know whether this has any adverse effects? –  user1202136 Jan 20 at 11:03

The package url defines the command \def\UrlOrds{\do\*\do\-\do\~\do\'\do\"\do\-}% which can be added to the default url break characters at which a line can be broken:

\documentclass{article}
\textwidth=8cm
\parindent=0pt

\usepackage{url}
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\makeatother
\begin{document}
\rule{\linewidth}{1pt}

foo bar baz \url{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-}

\end{document}

enter image description here

share|improve this answer
7  
a few explaining words might have been nice in this answer... –  xubuntix Nov 1 '12 at 10:15
1  
woohoo! This is the only one that worked for me. The other similar answer by @xamde blew TeX's capacity. –  Joe Corneli May 22 '13 at 13:28

Using the sloppypar environment as in

\begin{sloppypar}
  Figure \ref{fig:example_instant_lumi}:
  Public result available from \url{https://twiki.cern.ch/twiki/bin/view/AtlasPublic/LuminosityPublicResults}, version of 16.12.2011.
\end{sloppypar}

helps in my case. Without sloppypar the AtlasPublic part runs over the right margin, with sloppypar LaTeX breaks after view/ and everything's fine. (Doesn't need the url package.)

share|improve this answer
1  
This worked beautifully for me! Thanks! –  thedoctar Jul 29 '12 at 15:41
    
I found that sloppypar and the url package worked well together. –  Faheem Mitha Sep 22 '13 at 18:44

If it's not necessary for the url to appear verbatim, you could use \href{url}{text}. That way you could give the url a human-readable form. If your document is used electronically you just use it as an hyperlink and if the document is printed you could (or should) question the relevance of long (and probably cryptic) url's.

share|improve this answer
2  
URLs often appear in bibliographies. For an online resource, they should not be omitted. –  TH. Sep 13 '10 at 10:02
    
I could have reformatted the text as a bibliography, but in my particular setting this would have been too clunky. –  Suresh Sep 14 '10 at 7:17
1  
For some ungainly long URLs one can set up URL-shortened links via tinyurl.com, bit.ly, or other similar sites. Then the printed link can be the readable short link but the underlying link in the PDF can go to the original URL. –  András Salamon Sep 26 '10 at 15:59
1  
You can also use \href with the text set to the url, using \texttt, and putting the line breaks in explicitly. You gain flexibility at the price of a little more work. –  Tom Zych Sep 15 '12 at 0:00
1  
Instead of \texttt with manual line breaks, you can use the \path command from the url package, as mentioned in this question. This is the only way I was able to get sensible display of URLs in a bibliography on the arXiv. –  Mike Shulman Jul 23 '13 at 21:14

You may also want to look at the breakurl package.

share|improve this answer
7  
this is a package for latex->dvips->ps2pdf and not for pdflatex –  Herbert Feb 6 '11 at 20:33

I have a similar problem with itemize environment. When a word (or an url) is too long to respect the margin it does not starts a newline. Using \sloppy works but is very ugly.

My LaTeX code is generated by rst2latex.py and I use pdflatex on it.

I found that much better solution on the latex-community.org forum :

\tolerance 1414
\hbadness 1414
\emergencystretch 1.5em
\hfuzz 0.3pt
\widowpenalty=10000
\vfuzz \hfuzz
\raggedbottom

So I add this snippet in my docutils.tex and everything is just fine !

share|improve this answer
    
I think it is ok to post this here, but please have a look, if this works for the original problem too. –  canaaerus Sep 14 '12 at 11:59
    
I have checked with urls and it works. I can't remember why I was talking about the abstract context, maybe my brain was still in another post. –  Stan Sep 14 '12 at 15:39

I troubles I had with url breaking in an itemize environment could be solved by adding \Urlmuskip = 0mu plus 1mu in the preamble.

share|improve this answer
1  
Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. –  mafp Jun 19 '13 at 8:02

Not using \url, but \href, the URL is not visible anyway, so the suggestion in regard to employing tinyurl is relevant as it does avoid the ugliness of the URL in the text.

Much better, I find, to make an appropriate part of the text active, as in \href{URL}{active in context text}.

This way, the URL is not visible in the print out either.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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