# Line wrapping with underscores

This is a follow-up question to this one. There, a method is described how to "escape underscores". I would like to use that to put hyperlinks into the margin.

As you can see, with a very long link text, this overflows and doesn't break.

I would like it to break without inserting dashes. I tried \linebreak but that messes up the link URL.

MWE:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}

% <https://tex.stackexchange.com/a/20892>
\makeatletter
\DeclareRobustCommand*{\escapeus}[1]{%
\begingroup\@activeus\scantokens{#1\endinput}\endgroup}
\begingroup\lccode\~=\_\relax
\lowercase{\endgroup\def\@activeus{\catcode\_=\active \let~\_}}

\newcommand\thyrefdist[1]{\marginpar{\href{https://some.web.site/#1.html}{\texttt{\escapeus{#1}}}}}

%opening
\title{}
\author{}

\begin{document}

\maketitle

\begin{abstract}

\end{abstract}

Test\thyrefdist{Very_Long_Text_That_Does_Not_Wrap}

\end{document}


\documentclass[a4paper,10pt]{article}

\usepackage{hyperref}

\newcommand\thyrefdist[1]{\marginpar{\href{https://#1.html}{\path{#1}}}}

%opening
\title{}
\author{}

\begin{document}

\maketitle

\begin{abstract}

\end{abstract}

Test\thyrefdist{Very_Long_Text_That_Does_Not_Wrap}

\end{document}

• if it wasn't for hiding the https and .html you could have had simply \marginpar{\url{#1}} – David Carlisle Oct 19 '18 at 12:01

In an embarassing turn of events, I managed to figure it out. Another answer describes how to redefine \_ such that it also allows breaks. The key appears to be to only do that redefinition scoped to the escaping macro.

\DeclareRobustCommand*{\escapeus}[1]{%
\begingroup\renewcommand\_{\textunderscore\allowbreak}\@activeus\scantokens{#1\endinput}\endgroup}
\begingroup\lccode\~=\_\relax
\lowercase{\endgroup\def\@activeus{\catcode\_=\active \let~\_}}