Background color for math in display mode: Rendering a zero-padding box? #3751
Replies: 2 comments 1 reply
-
Giaco In case there is no native solution and assuming you're open to some post-processing, you could do this. Add the following styles:
Then add this script:
The script gets the required height for the new box and its position relative to the wrapping span.mfrac, then inserts the new span behind the fraction. I'm assuming you'll have more than one fraction on the page, and this will put a box around the first fraction in each math expression. Result for 3 different-size fractions: It adds no extra spacing. Hope it helps. Regards, |
Beta Was this translation helpful? Give feedback.
-
Here's how you work around that: 2 \colorbox{red}{$\displaystyle \frac{a}{b}$}
This is trickier. Reading the LaTeX source code (specifically One way to change this globally is to override the CSS rule: .katex .boxpad { padding: 0 } Example rendering: The alternative is to manually undo the spacing in KaTeX, which hard-codes a -3pt (0.3em) space. Example: 2 \frac{a}{b}
\\[1ex]
2 \colorbox{red}{\hspace{-3pt}$\displaystyle \frac{a}{b}$\hspace{-3pt}} You can make this into a macro for easier use: \gdef\red#1{\colorbox{red}{\hspace{-3pt}$\displaystyle #1$\hspace{-3pt}}}
2 \red{\frac{a}{b}} |
Beta Was this translation helpful? Give feedback.
-
I'd like to introduce a red background only behind the$\frac{a}{b}$ in the below equation:
The$2$ should not have a background color.
I naively tried to accomplish this via:
but that produces a pretty weird result:
I'm wondering if there's a way got get a zero-padding box around an element. Then I could do
and presumably my CSS would now work.
One thing that's important is I would like the spacing to be exactly the same whether the box is there or not.
I know about
\colorbox
, but it switches its contents to text mode and I wasn't sure how to get them back into display math mode. It also adds extra spacing around the element.Any suggestions? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions