2

I'm trying to make a new command for highlighting a part of text using black background and white text. I use the x11names option for the xcolor package and don't want to change it but it doesn't has the color black and white so I tried to define these two color and use it but Late gave me this error "Package xcolor: Undefined color `{myW}'." Here is my code:

\usepackage[x11names]{xcolor}
\usepackage{soul}

\definecolor{myB}{RGB}{0, 0, 0}
\definecolor{myW}{RGB}{225, 225, 225}
\newcommand\hlblack[1]{{\sethlcolor{myB}\hl{\textcolor{myW}{#1}}}}

\begin{document}
\hlblack{some text here}.
\end{document}

I also tried using Robust command but it just gave the same error. How can i resolve this ?

2
  • Type \hbox{\textcolor{myW}{#1}} instead of \textcolor{myW}{#1} Commented May 8 at 6:49
  • it resolved the above error but there is still this one "Package soul: Reconstruction failed." Commented May 8 at 6:51

2 Answers 2

5

The problem is the usage of \textcolor inside the \hl argument. Interchanging the commands helps:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{soul}

\definecolor{myB}{RGB}{0, 0, 0}
\definecolor{myW}{RGB}{225, 225, 225}
\newcommand\hlblack[1]{{\sethlcolor{myB}\textcolor{myW}{\hl{#1}}}}

\begin{document}
\hlblack{some text here}.
\end{document}

enter image description here

Notes:

  • Package soul needs to split the argument of \hl into single tokens. So using another command with arguments inside \hl can be problematic. Some commands (with at most one argument) are explicitly handled or can be registered (see the soul-ori manual for more information). Unfortunately \textcolor{…}{…} has two arguments and is neither handled automatically nor can be registered using \soulregister. But you can define your own one argument command for text in color myW:

    \documentclass{article}
    \usepackage[x11names]{xcolor}
    \usepackage{soul}
    
    \definecolor{myB}{RGB}{0, 0, 0}
    \definecolor{myW}{RGB}{225, 225, 225}
    \newcommand*{\textmyW}[1]{\textcolor{myW}{#1}}
    \soulregister{\textmyW}{1}
    \newcommand\hlblack[1]{{\sethlcolor{myB}\hl{\textmyW{#1}}}}
    
    \begin{document}
    \hlblack{some text here}.
    \end{document}
    

    Result is the same as shown above. However, IMHO, the solution with interchanged commands is easier. However, a trick like this can help in other cases.

  • Sometimes you can use \mbox to avoid the problem. But in your case, I assume, that you are using soul, because you want to allow line breaks inside \hlblack. So using a \mbox like:

    \newcommand\hlblack[1]{{\sethlcolor{myB}\hl{\mbox{\textcolor{myW}{#1}}}}}
    

    wouldn't be a solution, because the \mbox would prevent the line break.

  • Another suggestion, that often helps, is to use extra braces. But in your case:

    \newcommand\hlblack[1]{{\sethlcolor{myB}\hl{{\textcolor{myW}{#1}}}}}
    

    would result in the error:

    Package soul Error: Reconstruction failed.
    

    and therefore also isn't a solution.

4

white and black are always defined. Beside this, put the text color outside the \hl:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{soul}

\newcommand\hlblack[1]{{\color{white}\sethlcolor{black}\hl{#1}}}

\begin{document}
\hlblack{some text here}.
\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged .