4

I am wanting to include a link within some highlighted text. The highlighting is currently being performed by the soul package but the following example shows it returns a bunch of warnings and fails to render the link:

\documentclass{book}

% Package used for highlighting
\usepackage{soul}

% Link setup
\usepackage[ps2pdf]{hyperref}
\hypersetup{breaklinks=true, colorlinks=true, linkcolor=black, urlcolor=blue}
\usepackage[anythingbreaks]{breakurl}

\begin{document}

Link works here: \url{http://google.co.uk/}

Link within highlight fails: \hl{ Highlight \url{http://google.co.uk/} link }

\end{document}

Package hyperref Warning: Rerun to get /PageLabels entry.

! Undefined control sequence. l.14 Complains when link is within highlight: \hl { Highlight link \url{http...

Any idea how I can get links to appear within the \hl command?

3

The \url command is fragile. The following works.

\documentclass{book}

% Package used for highlighting
\usepackage{soul}

% Link setup
\usepackage{hyperref}
\hypersetup{breaklinks=true, colorlinks=true, linkcolor=black, urlcolor=blue}
\usepackage[anythingbreaks]{breakurl}

\begin{document}

Link works here: \url{http://google.co.uk/}

Complains when link is within highlight: \hl{ Highlight link \protect\url{http://google.co.uk/} }

\end{document}
  • Thanks but when I add text after the link the highlighting disappears. \hl{ Highlight link \protect\url{http://google.co.uk/} after } – A Dark Divided Gem Jul 29 '16 at 15:14
  • You're right; I'm out of my depth. – JPi Jul 29 '16 at 15:30
  • 2
    That makes both of us! I have updated my question to include text after the link to be more indicative of the environment the solution will be used within. – A Dark Divided Gem Jul 29 '16 at 15:38
1

It seems wrapping the robust \hbox command round the \url command prevents the fragility of the url command from interfering with the hl command. This does mean the URL cannot break between lines but this will have to do for now, unless anyone can find a better solution.

\documentclass{book}

% Package used for highlighting
\usepackage{soul}

% Link setup
\usepackage[ps2pdf]{hyperref}
\hypersetup{breaklinks=true, colorlinks=true, linkcolor=black, urlcolor=blue}
\usepackage[anythingbreaks]{breakurl}

\begin{document}

Link: \url{http://google.co.uk/}

Link within highlight: \hl{Highlight \hbox{\url{http://google.co.uk/}} link}

\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.