[Tex/LaTex] Long URL extends into margin – it breaks at underscore, but too late

hyperrefline-breaking

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.

Best Answer

enter image description here

\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}
Related Question