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

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

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 2 days ago
@otmezger Just use \usepackage[anythingbreaks]{breakurl} in your preamble. That's it. –  DL6ER 2 days ago