2

In most situations, the xurl package (presented here) ensures that long urls break across lines appropriately. However, inside the optional “caption” argument of a theorem, long urls stay unbroken, as seen in the MWE below. Is there a way to get them breaking automatically even there?

My document uses amsthm and hyperref; the MWE uses xurl, but that’s not otherwise required in my document. The question Enable line breaks in captions of theorem environment is similar, but doesn’t cover urls (it’s for breaking long theorem captions of ordinary text, and is solved by using amsthm).

\documentclass{amsart}

\usepackage{geometry}\geometry{textwidth=100mm}
\usepackage{blindtext}

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}

\usepackage[colorlinks]{hyperref}
\usepackage{xurl}

\begin{document}

\blindtext[1]
\url{https://tex.stackexchange.com/questions/696129/long-url-doesn-t-break-in-theorem-optional-argument-under-amsthm-hyperref-xur}.
\blindtext[1]

\begin{theorem}[\url{https://tex.stackexchange.com/questions/696129/long-url-doesn-t-break-in-theorem-optional-argument-under-amsthm-hyperref-xur}]
  \blindtext[1]
\end{theorem}

\end{document}
2
  • Did you try with the solution in the other answer? Sep 15 at 15:53
  • @UlrikeFischer: Which other answer do you mean in particular? The first linked question has many answers — I tried most of them, all the ones that looked applicable. The second linked question has just one answer — “use amsthm” — and I am already using that. Sep 15 at 16:06

2 Answers 2

3

The problem is that the url is math. The theorem code stores the optional argument in a box that it then unwraps later but TeX performs some optimization in this case and omits the implicite penalties. You can try the setouterhbox package:

\documentclass{amsart}

\usepackage{geometry}\geometry{textwidth=100mm}
\usepackage{blindtext}

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\usepackage[colorlinks]{hyperref}
\usepackage{xurl}
\usepackage{setouterhbox}
\usepackage{etoolbox}

\begin{document}
\section{unpatched}

$a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=$

\begin{theorem}[$a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=$]
 some text
\end{theorem}


\begin{theorem}[\url{https://tex.stackexchange.com/questions/696129/long-url-doesn-t-break-in-theorem-optional-argument-under-amsthm-hyperref-xur}]
 some text
\end{theorem}

\section{patched}
\makeatletter
\patchcmd\deferred@thm@head{\sbox\@labels{\normalfont#1}}{\setouterhbox\@labels \normalfont#1\endsetouterhbox}{}{\fail}
\makeatother

\begin{theorem}[$a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=a=b=c=d=$]
 some text
\end{theorem}


\begin{theorem}[\url{https://tex.stackexchange.com/questions/696129/long-url-doesn-t-break-in-theorem-optional-argument-under-amsthm-hyperref-xur}]
 some text
\end{theorem}

\end{document}

enter image description here

1
3

You are under no obligation to use the optional argument of the theorem environment to place the URL string.

enter image description here

This approach will have to be modified if the URL string contains certain TeX-special characters, such as % or #. In such cases, I'd recommend going the \urldef route, which is explained in sectin 2 of the user guide of the url package, "Defining a defined-url". That, or just replace \myurl{...} with \begingroup\upshape(\url{...})\endgroup.


\documentclass{amsart}

\usepackage[textwidth=100mm]{geometry}
\usepackage{lipsum}
% \usepackage{amsthm} % 'amsthm' is loaded automatically by 'amsart' doc. class
\newtheorem{theorem}{Theorem}
\usepackage{xurl}
\usepackage[colorlinks,allcolors=blue]{hyperref}
\newcommand\myurl[1]{\begingroup\upshape(\url{#1})\endgroup}

\begin{document}

\begin{theorem} 
\myurl{https://tex.stackexchange.com/questions/696129/long-url-doesn-t-break-in-theorem-optional-argument-under-amsthm-hyperref-xur}
\lipsum[1][1-5] % filler text
\end{theorem}

\end{document}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .