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.

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.
–
SureshSep 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 LazaridesSep 13 '10 at 14:30

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.
–
mathNov 28 '13 at 8:32

Do you mean that it should look like this: \usepackage{url}[hyphens] ? This does not work for me.
–
sixtyfootersdudeOct 30 '10 at 15:08

12

actually it would be \usepackage[hyphens]{url}
–
SureshOct 31 '10 at 6:48

33

@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.
–
mooseSep 20 '12 at 4:51

2

@Suresh @Martin Scharrer Both \usepackage[hyphens]{url} and \PassOptionsToPackage{hyphens}{url}\usepackage{hyperref} do not seem to work properly. Sure, the url is broken, but this happens at the right boundary of the page instead of at where the right margin begins. How should I solve this?
–
AdriaanJan 10 '14 at 9:51

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.
–
SSilkJun 25 '12 at 13:19

\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.)

This worked beautifully for me! Thanks!
–
thedoctarJul 29 '12 at 15:41

I found that sloppypar and the url package worked well together.
–
Faheem MithaSep 22 '13 at 18:44

I have combined the suggestion by @Herbert and yours, and it looks like this method works for me. But I found one "full stop" notation below the reference entry. Is there any way to eliminate that? Thank you.
–
VijaySep 22 '14 at 5:47

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.

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.
–
SureshSep 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 SalamonSep 26 '10 at 15:59

2

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 ZychSep 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 ShulmanJul 23 '13 at 21:14