1

I have an issue with the soul package (and soulutf8). In order to easily turn on/off highlighting text (\hl{<text>}), I have defined a counter highlighter, that is either set to 0 (highlighting off) or 1 (highlighting on). When compiling the MWE below, the text is still highlighted, allthough I set the counter to 0. This happens only when I use Fuchsia as optional argument. I really don't have a clue what's wrong.

\documentclass[12pt]{book}
\usepackage[usenames, svgnames]{xcolor}
\usepackage{soul}
\newcounter{highlighter}
\setcounter{highlighter}{1}
\DeclareRobustCommand{\hilite}[2][Aqua]{%
    \ifnum\arabic{highlighter}=0%
        #2%
    \else%
        {\sethlcolor{#1}\hl{#2}}%
    \fi}

\begin{document}
Sample text with some \hl{highlighted} words.

Sample text with some \hilite{highlighted} words.

Sample text with some \hilite[LawnGreen]{highlighted} words.

\setcounter{highlighter}{0}
Sample \hilite{text} \hilite[LawnGreen]{with} \hilite[Fuchsia]{4} words.
\end{document}
1

Note that this problem has nothing to do with the colour you use (the first argument), the problem is entirely caused by the highlighted text (the second argument). If it is a number, things go pear-shaped: try \hilite{4} as in your MWE and compare it to \hilite{0}, \hilite{00foo}, \hilite{12foo} and \hilite{007foo} to see a different (but still wrong) behaviour.

This is one of the few situations where you don't want a % at the end of the line.

\DeclareRobustCommand{\hilite}[2][Aqua]{%
  \ifnum\value{highlighter}=0
    #2%
  \else
    {\sethlcolor{#1}\hl{#2}}%
  \fi}

without a % at the end of \ifnum\value{highlighter}=0 works just fine.

But why do things go wrong if you have a % at the end of the line? Remember that spaces at the beginning of a line are ignored anyway, if you add a % to the end of the line the space that would normally be inserted by the line break is also ignore. And so TeX reads the first two lines of your macro definition as

\ifnum\arabic{highlighter}=0#2%

That means that TeX won't stop to look for a number after it has read the 0, it will continue to parse a number. In particular TeX will read as much of #2 as possible as a number and will use the result for comparison, only the non-numeric part of the second argument will be left in the true branch.

If #2 does not start with something that could be parsed as a number everything is fine. The highlighter is compared against 0 and the entire #2 ends up in the true branch.

If #2 starts with a number, there are two possibilities: (1) The number that TeX ends up parsing is zero, in that case the remaining bit of #2 that did not constitute a number is printed without highlighting in the true branch. (2) The number TeX parses is non-zero, in that case the entire second argument is printed in highlighted form.

\documentclass[12pt]{book}
\usepackage[usenames, svgnames]{xcolor}
\usepackage{soul}
\newcounter{highlighter}
\setcounter{highlighter}{1}
\DeclareRobustCommand{\hilite}[2][Aqua]{%
  \ifnum\value{highlighter}=0
    #2%
  \else
    {\sethlcolor{#1}\hl{#2}}%
  \fi}

\begin{document}
Sample text with some \hl{highlighted} words.

Sample text with some \hilite{highlighted} words.

Sample text with some \hilite[LawnGreen]{highlighted} words.

\setcounter{highlighter}{0}
Sample \hilite{text} \hilite[LawnGreen]{with} \hilite[Fuchsia]{4} words.
\end{document}
  • Awesome! Concerning “TeX” programming, I’m a newbie. Other than that I get things done. ;) – rsa-krypto Sep 13 '18 at 17:52

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.