12

How can I highlight the dollar currency symbol? For some reason, the code below is underlining rather than highlighting when I try it.

\documentclass{article}
\usepackage{soul}

\begin{document}
\hl{\$ 10} 
\end{document}

3 Answers 3

18

If you include the package color or xcolor it will highlight the text background.

\documentclass{article}
\usepackage{soul}
\usepackage{color}

\begin{document}
\hl{\$ 10}
\end{document}

Example

Using \sethlcolor you can set the highlighting color.

\documentclass{article}
\usepackage{soul}
\usepackage{color}

\DeclareRobustCommand{\hlgreen}[1]{{\sethlcolor{green}\hl{#1}}}

\begin{document}
\hl{\$ 10} \hlgreen{\$ 10}
\end{document}

Second Example

See this answer for why \DeclareRobustCommand should be used.

littleO's answer using xcolor's \colorbox results in a margin around the highlighted word, which might be useful in some scenarios.

\documentclass{article}
\usepackage{soul}
\usepackage{xcolor}

\DeclareRobustCommand{\hlgreen}[1]{{\sethlcolor{green}\hl{#1}}}
\DeclareRobustCommand{\boxgreen}[1]{\colorbox{green}{#1}}

\begin{document}
This item costs \hlgreen{\$10} in a shop

This item costs \boxgreen{\$10} in a shop
\end{document}

Example 3

1
  • Using the soul package, there is a way to add more top and bottom margin, redefining the correct leaders, like with \makeatletter\def\SOUL@hlpreamble{\setul{}{3.5ex}\let\SOUL@stcolor\SOUL@hlcolor\SOUL@stpreamble}\makeatother, based on this answer. Nevertheless, I can't find how to add left and right margin to it.
    – Olivier
    Feb 5 at 15:38
2

You can do that with just the xcolor package and

\fcolorbox{<bordor color>}{<fill color>}{<text}}

which yields:

enter image description here

If you don't want a border color, use the same <bordor color> and <fill color>.

Code:

\documentclass{article}
\usepackage{xcolor}

\newcommand*{\ColorBox}[1]{%
    \fboxsep=1pt%
    \fcolorbox{gray}{yellow}{#1}%
}

\begin{document}
\ColorBox{\$ 10} 
\end{document}
1

Here's a solution that worked for me using the xcolor package and a colorbox:

\documentclass{article}
\usepackage{xcolor}

\begin{document}
\colorbox{yellow!100}{\$10}
\end{document}

You must log in to answer this question.

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