6

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?

2 Answers2

5

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}
JPi
  • 13,595
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}