2

I'd like to provide a ‘background-colour’ for spans of code or math inline with text, in the same way that CSS allows. I've tried to do this with \colorbox, but unfortunately, that seems to add vertical alignment issues:

\documentclass{article}

\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.89}

\begin{document}

\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\colorbox{light-gray}{$\mathsf{A \rightarrow P(x)}$}
This is a sentence, after-which another formula with colour will be on the next line.
\colorbox{light-gray}{$\mathsf{A \rightarrow P(x)}$}
\blindtext

\end{document}

Product: enter image description here

I'd much prefer the colour to ‘bleed’ into the space dedicated to above/below lines, if that makes sense (i.e. still be ‘tall’, as it is by default, taller than the line-height; but for it to not affect the effective line-height of the lines it's in.)

  • 1
    if you know the content will fit in the line without the box then \smash{\colorbox{... will safely hide the height – David Carlisle Feb 2 '16 at 21:15
3

Here's a version without \colorbox (apart from the example), but using the very nice \tcbhighmath command from tcolorbox and the configurable settings of highlight math style={....}

The most important setting is size=tight, but there is size=minimal too!

\documentclass{article}
\usepackage{xcolor}    
\usepackage[most]{tcolorbox}

\usepackage{blindtext}

\definecolor{light-gray}{gray}{0.89}

\tcbset{highlight math style={enhanced jigsaw,size=tight,colback=light-gray,boxrule=0pt,sharp corners}}

\begin{document}

\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\tcbhighmath{\mathsf{A \rightarrow P(x)}}
This is a sentence, after-which another formula with colour will be on the next line.
\colorbox{light-gray}{$\mathsf{A \rightarrow P(x)}$}
\blindtext

\end{document}

enter image description here

  • Nice solution! Is there a way to render \tcbhighmath output using textstyle? In particular, have it use \tfrac instead of \dfrac? – Casimir Jul 26 '16 at 0:10
2

\colorbox adds padding on the four sides; you want to remove it.

\documentclass{article}

\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.89}

\newcommand{\graybg}[1]{%
  \begingroup\setlength{\fboxsep}{0pt}% no padding
  \colorbox{light-gray}{#1}%
  \endgroup
}

\begin{document}

\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\graybg{$\mathsf{A \rightarrow P(x)}$}
This is a sentence, after-which another formula with colour will be on the next line.
\graybg{$\mathsf{A \rightarrow P(x)}$}
\blindtext

\end{document}

enter image description here

If you instead want the padding, but that it doesn't interfere with the vertical spacing, smash the color box, but add a vertical phantom with the actual size.

\documentclass{article}

\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.89}

\newcommand{\graybg}[1]{%
  \mbox{\vphantom{#1}\smash{\colorbox{light-gray}{#1}}}%
}

\begin{document}

\blindtext
$\mathsf{A \rightarrow P(x)}$
\blindtext
\graybg{$\mathsf{A \rightarrow P(x)}$}
This is a sentence, after-which another formula with colour will be on the next line.
\graybg{$\mathsf{A \rightarrow P(x)}$}
\blindtext

\end{document}

enter image description here

Maybe you want a mix between the two methods. The default value of \fboxsep is 3pt.

  • Can I do this without reducing the effective size of the coloured region? Something like a margin with negative padding in CSS? – ELLIOTTCABLE Feb 2 '16 at 21:16
  • @ELLIOTTCABLE I just added it – egreg Feb 2 '16 at 21:18

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.