# How to allow line break in a long hyperlink in a PDF compiled by latex-dvips-ps2pdf?

I am using latex-dvips-ps2pdf steps with -Ppdf option passed to dvips. The following code snippet

\documentclass{article}

\usepackage{breakurl}
\hypersetup{pdfborder={0 0 100}}

\def\emailA{\url{abcdefghi.jklmn.opqrs.tuvwxyz@tuvwxyz.com}\ }
\def\emailB{\url{jklmn.opqrs.tuvwxyz.abcdefghi@tuvwxyz.com}\ }
\begin{document}
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\end{document}


produces the output as follows.

Is there any trick to fix it?

• Please don't create new "command" tags like {href} unless absolutely necessary. Also, {hyperlink} should be covered by {hyperref}. Instead, I added {line-breaking} for you. ;-) – lockstep Jun 14 '11 at 22:06
• @Lockstep: OK. Sometimes a bit difficult to know what tags I should you. :-) – xport Jun 14 '11 at 22:10
• Did you try this with pdfLaTeX: it also fails to break all of the lines 'nicely'. There are cases where it's simply not possible for TeX to break lines automatically. – Joseph Wright Jun 21 '11 at 15:34

Even with the help of the breakurl package, your example will produce overfull boxes. This is because breakurl will only break urls after one of the following symbols :/.?#&_,;! and before any ocurrence of the symbol %.

To overcome this you have two possibilities: to use \sloppy (which will make the spacing look really awful) or to manually insert some spaces in the appropriate places to help the package find a proper break-point for the urls.

In the following example I show a reduced version of your example with three variants: the first one shows the default output obtained with breakurl; in the second one, I manually inserted some \hspaces, and in the third one I used \sloppy. It's up to you to decide which of the two last approaches looks less ugly and is more practical.

I used draft to see the overfull boxes more clearly. The code must be compiled through latex.

\documentclass[draft,10pt]{article}

\usepackage{breakurl}
\hypersetup{pdfborder={0 0 100}}

\def\emailA{\url{abcdefghi.jklmn.opqrs.tuvwxyz@tuvwxyz.com}\ }
\def\emailB{\url{jklmn.opqrs.tuvwxyz.abcdefghi@tuvwxyz.com}\ }
\begin{document}

The example:

\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB

\vspace{1cm}

The example with \verb+\hspace+s manually inserted:

\hspace*{-4pt}\emailA or \emailB or \emailB or \emailA\hspace{3pt} or\hspace{4pt}
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB\hspace{3pt} or\hspace{3pt} \emailB

\vspace{1cm}

The example with \verb+\sloppy+:

\begingroup
\sloppy
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB or \emailA or
\emailA or \emailB or \emailB\par
\endgroup

\end{document}


Try this.

\documentclass{article}
\begin{document}
If you have any questions, please don't hesitate to contact me
at \url{mailto:xport@tex.stackexchange.com}.
\end{document}


With \href, you need to use \nolinkurl in the second argument.

\href{mailto:xport@tex.stackexchange.com}{\nolinkurl{xport@tex.stackexchange.com}}

• @xport --- Can you use pdflatex instead? – Ian Thompson Jun 14 '11 at 22:36
• It seems \nolinkurl does the trick without even using breaklines=true. Maybe the latter is default now? – Jonathan W. Jul 21 '16 at 6:04
• Also, nolinkurl changes the font of the text displayed to the document's monospaced font (at least when using fontspec and XeLaTeX). – Jonathan W. Jul 21 '16 at 6:04
• Saved my life! Many thanks for that simple cool hint. – Matthias Mar 15 '17 at 14:47
• This doesn't really work (unless you use pdflatex in which case you don't need this at all); see this question. – ShreevatsaR Jul 4 '17 at 21:25

I finally found a solution, that might be of interest to all the ones having problems with displaying long URLs in LaTeX: I had a little chat with Vilar (the maintainer of the breakurl-package) and he was able to extend the breaking possibilities of his package they way I suggested.

Therefore the new option anythingbreaks in the newest version does the links look good as they keep in range of the textborder.

See for yourself:

Completely without:

Standard behavior:

Anythingbreaks:

Look also at my question here: Biblatex: URL-breaking not working in DVI-mode

• Note that this answer is not a duplicate: it discusses the new anythingbreaks option. – Charles Stewart Apr 11 '13 at 7:20
• Hi, could you update the answer with the correct way to use the option anything breaks? – otmezger Jul 27 '14 at 22:24
• @otmezger Just use \usepackage[anythingbreaks]{breakurl} in your preamble. That's it. – MrD Jul 28 '14 at 9:24
• Simply including that line throws errors for me: ! Undefined control sequence. <argument> \headerps@ {/burl@stx null def /BU.S { /burl@stx null def }de...l.311 } – Andy Ray Mar 28 '17 at 0:30

I found 2 solutions for breaking long URLs.

The first one unfortunately doesn't work with hyperref.

\usepackage[hyphens]{url}


This will break long URLs after hyphens, which is mostly ok. But since hyperrefuses urlsomehow, there's a conflict activating the hyphens-option

! LaTeX Error: Option clash for package url.


If somebody has an explanation (or solution) for me, I'd be glad.

The second solution works fine. I haven't researched it intensely, yet. But it should be a clean solution as only the characterlist for linebreaking is manipulated an neither measures nor mechanisms are influenced. (I'd appreciate it, if someone could verify this.)

\usepackage{hyperref}
%or \usepackage{url}
\usepackage{etoolbox}
\appto\UrlBreaks{\do\-}


The last line adds the hyphen to the list of characters after which an URL is allowed to break. You can expand the list by adding "dos" like {\do\a\do\b\do\c} (This would allow URLs to break after a, b, and c, too.)

There's also a list \UrlBigBreaks. But I haven't checked it out, yet.

• The first solution is also discussed here. The solution is to pass the option to url using PassOptionsToPackage before that package is loaded, which may be before documentclass. – ingomueller.net Jul 5 at 14:06

Have a look at the breakurl package -- page 1 of the documentation includes the answer to your question.

• @xport: it doesn't work because the package can't find a valid character to break some urls. You can use \sloppy (it will work now but the document will look awful) or simply insert some convenient blank space before the problematic urls. – Gonzalo Medina Jun 20 '11 at 4:49
• @Gonzalo: Could you make an example as the answer? I will try it. – xport Jun 21 '11 at 2:48

\\


Lazy answer, but it's working for me, and I'm new in latex:

BEFORE:

\url{http://www.mma.gov.br/port/conama/processos/C1CB3034/DECRETO_SAO_PAULO_59113_2013.pdf}


AFTER:

\url{http://www.mma.gov.br/port/conama/processos/C1CB3034\\/DECRETO_SAO_PAULO_59113_2013.pdf}

• This breaks the link! Hence you would be redirected to a different URL... – m13r May 11 '16 at 13:28
• you are right, but i have no other way for my two column paper :/ – Sergio May 18 '16 at 20:26