9

Here's my MWE:

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\colorlet{LightLavender}{Lavender!40!}

\begin{document}

\colorbox{LightLavender}{hello} world

\end{document}

It produces something like this:

enter image description here

I'm wondering if it's possible to have the box have rounded corners. Ideally a solution would work in both text and math modes because I sometimes use it in equations to help designate which terms are alike/cancel out.

Sebastiano
  • 54,118

2 Answers2

16

tcolorbox, with use in math environment(s):

\documentclass[dvipsnames]{article}
\usepackage[most]{tcolorbox}
\colorlet{LightLavender}{Lavender!40!}
\tcbset{on line, 
        boxsep=4pt, left=0pt,right=0pt,top=0pt,bottom=0pt,
        colframe=white,colback=LightLavender,  
        highlight math style={enhanced}
        }

\begin{document} \tcbox{Hello} world! $\tcbhighmath{c^2} = a^2 + b^2$ \end{document}

enter image description here

Zarko
  • 296,517
  • Very nice...and the boxes are inline with the formula. +1. – Sebastiano Oct 30 '20 at 07:45
  • 1
    @Sebastiano, thank you very much! – Zarko Oct 30 '20 at 07:56
  • Awesome thanks! I didn't know you could add the dvipsnames option to the article class either, so that's an added bonus. – Nate Stemen Oct 30 '20 at 12:56
  • 1
    @Zarko how would you recommend doing this if I wanted to have multiple color highlights? – Nate Stemen Nov 01 '20 at 21:49
  • @NateStemen, this is new question which in comment is not visible well to others, who has similar problems. So it will be nice ask it as such :-). It can be solved on different way, the simplest seems to be add desired color as option to \tcbox or \tcbhighmath. – Zarko Nov 02 '20 at 02:49
  • I've noticed that \tcbox increases the line height when highlighting some words in a long text. Any suggestion to avoid this extra margin? Thanks. – unDavide Sep 16 '23 at 23:31
  • 1
    @unDavide, in such a cases you need redesign tcbox (define smaller boxsep). For more, I advice to ask new question, where you provide an MWE, which reproduce your problem. On it people can answer with solutions for your problem. – Zarko Sep 17 '23 at 00:13
  • Thanks for the suggestion @Zarko, I've posted a new question here. – unDavide Sep 17 '23 at 10:58
4

Use tcolorbox.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\colorlet{LightLavender}{Lavender!40!}
\usepackage{tcolorbox}
\begin{document}

\tcbox[on line,boxsep=0pt,left=2pt,right=2pt,top=2pt,bottom=2pt,colback=LightLavender]{hello} world

\end{document}

enter image description here