Avatar
Avatar

Reputation: 15196

How to make a background box around a fraction ("bbox") with Katex?

The bbox command is not supported in Katex, see https://katex.org/docs/support_table.html

bbox with MathJax renders \bbox[#e1ffc1,5px]{ \frac{1}{2} } like:

bbox with mathjax

I have tried in Katex the alternative command:

\colorbox{#e1ffc1}{ \frac{1}{2} }

but it throws the error:

KaTeX parse error: Can't use function '\frac' in text mode at position 21: …rbox{#e1ffc1}{ \̲f̲r̲a̲c̲{1}{2} }

Likewise \fcolorbox does not work.

How to get the background box in Katex around a fraction to work?

Upvotes: 0

Views: 873

Answers (1)

Avatar
Avatar

Reputation: 15196

In LaTeX, \colorbox puts its contents into text mode. And KaTeX is a bit pickier about text mode than MathJax is.

You can still put math inside the box, but you have to explicitly shift it from text mode back into math mode with $…$ delimiters. So, to get your example working, try \colorbox{#e1ffc1}{$ \frac{1}{2} $}

Credits @ronkok via https://github.com/KaTeX/KaTeX/discussions/2531


Alternatively you could add colorboxes only around the elements within the formulas.

Or you dismiss the idea of having a background color, and instead you set the color of the formulas themselves, using e. g. \color{red}{\frac{1}{2}} or \textcolor{red}{\frac{1}{2}} (seems also to accept formulas!).

Upvotes: 1

Related Questions