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}


amsthm” — and I am already using that. – Peter LeFanu Lumsdaine Sep 15 '23 at 16:06