1

I try to highlight large paragraph of text, where there are various other macros such as \cite, \SI, \ref, etc. in between.

If I do something like the following:

\hl{the duration is \SI{4}{\second} as shown in Figure~\ref{fig:fig1} \citep{Doe2015}}

The text would truncate at "is", or even cannot compile sometimes.

Is there a way to write a macro like this:

\DeclareRobustCommand{\robusthl}[1]{
    \ifnotmacro\hl{#1}\fi}

Thank you so much!

LaRiFaRi
  • 43,807
Jerry Ma
  • 161
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – LaRiFaRi Jun 16 '15 at 07:03
  • Seems to be dificult. Maybe you want to search for some other highlighting package. Related: http://tex.stackexchange.com/q/42368, http://tex.stackexchange.com/q/90014, http://tex.stackexchange.com/q/243719, and https://github.com/josephwright/siunitx/issues/185 – LaRiFaRi Jun 16 '15 at 07:18

1 Answers1

1

Protect the offending commands in \mbox. On the other hand, underlining should be avoided as much as possible.

\documentclass{article}

\usepackage{soul,siunitx,natbib}
\soulregister{\ref}{1}
\soulregister{\citep}{1}

\begin{document}

\hl{the duration is \mbox{\SI{4}{\second}} as shown in
Figure~\mbox{\ref{fig:fig1}} \mbox{\citep{Doe2015}}}

\begin{figure}\caption{X}\label{fig:fig1}\end{figure}

\begin{thebibliography}{1}

\bibitem[Doe(2015)]{Doe2015} A paper

\end{thebibliography}

\end{document}

enter image description here

egreg
  • 1,121,712
  • But is there a way to define a macro that would automatically do this, or does one have to do it manually every time? Thanks! – Matifou Mar 15 '22 at 13:08
  • Update: Actually discussed here: https://tex.stackexchange.com/questions/139463/how-to-make-hl-highlighting-to-automatically-place-incompatible-commands-in – Matifou Mar 15 '22 at 14:12