13

I need to use boxed links and ragged right text. The problem is that when a link spans two lines, though the link is broken correctly (when using pdflatex) the box around the linked text extends until the right margin. Consider the following example:

\documentclass{article}
\usepackage[textwidth=45mm]{geometry}
\usepackage[colorlinks=false]{hyperref}

\begin{document}
\raggedright
\href{http://www.ctan.org/}{The Comprehensive TeX Archive Network}
\end{document}

In the output, the link box extends beyond the end of 'TeX' and up to the right margin, thus enclosing some white space. Is there a way to make the box around the link extend only up to the end of the text and not the right margin?

  • Related: Adjust box around hyperref footnote links (Compiling with XeLaTeX actually solves your problem, too.) – Qrrbrbirlbel Nov 6 '12 at 2:23
  • Manual way: \href{http://www.ctan.org/}{The Comprehensive TeX} \href{http://www.ctan.org/}{Archive Network} – Qrrbrbirlbel Nov 6 '12 at 2:32
  • @Qrrbrbirlbel: both solutions indeed work, but I was looking for a solution which works a) automatically and b) in pdflatex. – ctef Nov 7 '12 at 14:55
5

Simplest is just to link each word separately. Probably if necessary one could prevent the acrobat link boxes from drawing the vertical lines between words, but perhaps this is enough?

enter image description here

\documentclass{article}
\usepackage[textwidth=45mm]{geometry}
\usepackage[colorlinks=false]{hyperref}

\makeatletter
\let\oldhref\href
\def\href#1#2{%
\def\hrlink{\oldhref{#1}}%
\hrrsp#2 \relax}

\def\hrrsp#1 #2{%
\hrlink{#1}%
\ifx\relax#2%
\expandafter\@gobbletwo
\else
\ %
\fi
\hrrsp#2}
\begin{document}
\raggedright
\href{http://www.ctan.org/}{The Comprehensive TeX Archive Network}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.