# Problems with \ref when using soul for highlighting

Here is a minimal example showing the problem.

\documentclass[10pt]{article}
\usepackage[usenames,dvipsnames]{color}
\usepackage{soul}
%\soulregister{\ref}{1}
\begin{document}

\section*{x}\label{x}

\section*{y}
\hl{ Some text.
Section~\ref{x}.
}
\end{document}


I'm getting the error

LaTeX Warning: Reference {x}' on page 0 undefined on input line 12.

! Argument of \ref has an extra }.


The problem is with \ref. Commenting out the line beginning with "Subsection" makes the error disappear. \soulregister is intended for font commands. I don't know if \ref is a font command, but I could not get \soulregister to fix the problem (see the commented line above).

Enclosing the \ref{x} in { } works. soul then takes it as one element instead of breaking it up, I guess. It seems to me that it reads both \ref and {x} in two steps and sets it together again while adding another set of braces, i.e. the result is \ref{{x}}. This doesn't matter much for font commands but \ref takes the inner braces as part of the label name. This causes the warning about the unknown label. The error is caused because \ref is then also expanded by soul inside a { } group, i.e. {\ref}. Don't ask me why.

Note that you can't label a \section* because it doesn't has a number.

\documentclass[10pt]{article}
\usepackage[usenames,dvipsnames]{color}
\usepackage{soul}
%\soulregister{\ref}{1}
\begin{document}

\section{x}\label{x}

\section{y}
\hl{ Some text.
Section~{\ref{x}}.
}
\end{document}

• Is it worth reporting this as a bug? Jul 17, 2011 at 19:00
• @FaheemMitha: I would check the documentation first if this is explicitly mentioned. Jul 17, 2011 at 19:07
• To answer your other question from the chat: \textit{...} works because soul detects \textit and then handles its argument correctly. However, if you use {\itshape ...}` then there is no argument which can be handled in a special way. Jul 18, 2011 at 17:05
• Is there a way of writing a macro that will handle \cite and \ref properly? I've found that even the extra '{}' sometimes seems to fail in large projects. And obviously it's a real annoyance to have to edit the main text in large documents to make a certain macro work.
– JPH
Jul 29, 2013 at 18:14
• Ahah! I have found the issue in my large project - {\cite{x0}} works, but {\cite{x0,x1}} doesn't - ie citation of more than one ref at a time breaks it.
– JPH
Jul 29, 2013 at 18:25