1

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.

link text overflows to the right

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}
larsrh
  • 123

2 Answers2

2

enter image description here

\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}
David Carlisle
  • 757,742
1

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~\_}}
larsrh
  • 123