# Is there a hyperref-compatible package for DOIs allowing linebreaks?

I am aware of the packages url allowing to typeset hyperlinked URLs with sensible linebreaks, and of the package doi providing correct hyperlinks for Digital Object Identifiers (DOIs). However, the latter package does not insert linebreaks for the DOIs and produces awful overful hboxes all around.

Is there a package for hyperlinked DOIs with some sensible linebreaks, like a combination of DOI and url?

Here is a workaround that requires some manual intervention. First the code:

\documentclass{article}
\usepackage{doi}

\makeatletter
\def\@doi#1{%
\let\#\relax
\let\_\relax
\let\textless\relax
\let\textgreater\relax
\edef\x{\toks0={{#1}}}%
\x
\edef\#{\@percentchar23}%
\edef\_{_}%
\edef\textless{\@percentchar3C}% instead of {\string<} for Apple
\edef\textgreater{\@percentchar3E}% instead of {\sting>} for Apple
\begingroup      %%% <----- begin modification
\edef\allowbreak{}
\edef\x{\endgroup\toks2={\noexpand\href{\doiurl#1}}}%   <----- end modification
\x
\edef\x{\endgroup\doitext\the\toks2 \the\toks0}%
\x
}
\makeatother

\begin{document}

\begin{minipage}{2in}
\doi{really.long.line.#really_\allowbreak .long.line<test>.line/10.29345}
\end{minipage}
\end{document}


The \@doi command is from the internals of the doi package. What I did is to add one line of additional logic: that when generating the sanitized URL for the hyperlink the code strips any calls to \allowbreak. This allows you to manually insert allowed breakpoints for the displayed URL as part of your DOI string.

This could potentially be automated if you know where you want the breaks to be: the code from xurl or breakurl or just url can be borrowed to insert break points at specific characters. I'll leave that to others.