# Long URL extends into margin - it breaks at underscore, but too late

In my dissertation I have a long URL containing many underscore characters as word separators (Wiki software did this). These URLs break at an underscore (good!), but too late, causing the line of text to extend into the margin. There is an earlier underscore, but the break doesn't happen there. I'd rather have one line with shorter text, than one line that extends into the margin.

I'm using a document class particular to my school, derived from amsbook, which uses hyperref. I've read a lot of forum posts on similar issues, but (a) did not find one that exactly matched my problem (underscores breaking, but not at the best one), and (b) the solutions others suggested did not work for me: I tried passing the [hyphens] option, to no effect (as expected). I also tried the \breakurl package, but my system uses pdflatex, which is not supported by \breakurl.

I'd be very grateful for your advice - the school flagged my dissertation because these URLs protrude into the required margin, and I've run out of things to try. Last resort would be to show the URL as plain text and force other formatting - I'd like to avoid that.

My attempt at a MWE:

\documentclass[hidelinks,12pt]{amsbook}
\RequirePackage[bookmarks, bookmarksopen=true, plainpages=false, pdfpagelabels, pdfpagelayout=SinglePage, breaklinks = true]{hyperref}

\begin{document}
\chapter{Troubleshooting long URLs}

\section{Test 1}
A complete list of participants and presentations can be found on the conference web site: \url{http://ruraro.rintint.edu/events/shipshops/index.php/Perplexity_and_Action_-_an_Interdisciplinary_Approach_to_Rationale_Synchro_Theory}

\end{document}


After compiling with pdflatex (actually, I'm using Sublime Text CTRL-B), the URL breaks twice: first after events/ and second after Interdisciplinary_ although I'd prefer if the second break occurred after an_ instead.

EDIT: Naively, to me the crux seems to be how LaTeX (and all the packages involved) choose at which underscore to break the URL! Why not at the last underscore before the end of the line (margin-wise)? Currently it breaks at the first underscore after the paragraph.

• How about using \href{http://ruraro.rintint.edu/events/shipshops/index.php/Perplexity_and_Action_-_an_Interdisciplinary_Approach_to_Rationale_Synchro_Theory}{the conference web site} instead... – Werner Dec 11 '14 at 23:25
• Use \renewcommand{\UrlBreaks}{\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\do\1\do\2\do\3\do\4\do\5\do\6\do\7\‌​do\8\do\9\do\/\do\-\do\_\do\:\do\.} in the preamble. – user11232 Dec 11 '14 at 23:35
• Werner - the dissertation is formatted for print, so all URLs must be spelled out (your solution would work well for electronic PDF consumption, though). – Ben Dec 11 '14 at 23:46
• Harish - I tried your suggestion and get errors for most or all of my URLs, such as: "Command \k unavailable in encoding OT1." The URLs don't render properly. – Ben Dec 11 '14 at 23:48
• How about show the URL in a foot note? – xuhdev Dec 27 '15 at 21:05

\documentclass[hidelinks,12pt]{amsbook}
\RequirePackage[bookmarks, bookmarksopen=true, plainpages=false, pdfpagelabels, pdfpagelayout=SinglePage, breaklinks = true]{hyperref}

\begin{document}
\chapter{Troubleshooting long URLs}

\section{Test 1}
A complete list of participants and presentations can be found on the conference web site: \href
{http://ruraro.rintint.edu/events/shipshops/index.php/Perplexity_and_Action_-_an_Interdisciplinary_Approach_to_Rationale_Synchro_Theory}%
{\texttt{http://ruraro.rintint.edu/events/ship\linebreak shops/index.php/Perplexity\string_and\string_Action\string_-\string_an\string_Interdisciplina\linebreak ry\string_Approach\string_to\string_Rationale\string_Synchro\string_Theory}}

\end{document}

• This solution results in the best formatting for me - in that the document appears very similar to how it looked before, but with full (manual) control of the line breaks within the URL. Thank you very much! PS: Specifying the exact linebreak position seems rather fragile to me, so I'm still interested in a solution that would intelligently break at underscores while respecting the margins. But for now I am glad that I can address my administrator's concerns! – Ben Dec 12 '14 at 7:46
• @Ben the URL is set monospace with no space so there is no flexibility at all in that part so the only way it can break at _ is to massively stretch the inter-word white space in the rest of the paragraph. \sloppy will do that but it will look horrible (but is as you say a possibility of you need a fully automatic solution, but I assume you are not typesetting 10s of theses a week....) – David Carlisle Dec 12 '14 at 8:33

Put the url on a separate paragraph and use a \parbox. It also works within a list:

\documentclass[hidelinks,12pt]{amsbook}
\RequirePackage[bookmarks, bookmarksopen=true, plainpages=false, pdfpagelabels, pdfpagelayout=SinglePage, breaklinks = true]{hyperref}

\usepackage{enumitem}

\begin{document}
\chapter{Troubleshooting long URLs}

\section{Test 1}
A complete list of participants and presentations can be found on the conference web site: \medskip

\noindent%
\parbox{\linewidth}{\url{http://ruraro.rintint.edu/events/shipshops/index.php/Perplexity_and_Action_-_an_Interdisciplinary_Approach_to_Rationale_Synchro_Theory}}

\begin{itemize}[leftmargin = 1.6\parindent, before = \bigskip]
\item\parbox[t]{\dimexpr\linewidth-\leftmargin\relax}{\url{http://ruraro.rintint.edu/events/shipshops/index.php/Perplexity_and_Action_-_an_Interdisciplinary_Approach_to_Rationale_Synchro_Theory}}

\end{itemize}

\end{document}


• Unfortunately this did not work for me. It does work in the MWE, but in my full dissertation, the URL appears "sandwiched" between the paragraphs above and below - the vertical spacing is not correct. Thank you for the suggestion. – Ben Dec 11 '14 at 23:54
• Ypu can add some vertical spacing manually (\medskip, smallskip or whatever you please). – Bernard Dec 12 '14 at 0:03
• I will try this, thanks. I also have long URLs as items in a list - would your approach work for that as well? I'm using \begin{itemize} and then \item. – Ben Dec 12 '14 at 0:15
• @Ben: it also works within a list. See my updated answer. – Bernard Dec 12 '14 at 2:17
• Thank you for testing with a list. Unfortunately, I wasn't able to achieve quite the formatting that I want with this approach (probably due to my unfamiliarity with the other vertical formatting commands). Your solution looks very good in the MWE that I provided, but within my full dissertation it didn't quite work well enough (given my abilities). Thank you. – Ben Dec 12 '14 at 7:50

You can use the \href function twice (or as many times as you link), both linking to the same URL, but the first with the text you want to appear on the first line, and the second with the text you want to appear on the second line. In other words, you decide where the line break occurs. A bit of a bodge but works well for me:

(\href{Full URL}{\underline{Text up to the end of line 1}} \href{Full URL}{\underline{Text for the start of line 2}})