2

I got pretty long math formulas in my LaTeX document. Now I want to color the background of those formulas. However, if I use \colorbox{}{} the linebreak doesn't work anymore.

Note: the gray bar at the right side of the images marks the end of the page and the "Das" you can see at the bottom of the images marks the end of the line. The downarrow marks the middle of the line.


1. This is centered and the linebreak works:

\begin{center}
    \(32x^{15}+91x^{14}+11x^{13}+120x^{12}+209x^{11}+114x^{10}+220x^{9}+77x^{8}+67x^{7}+64x^{6}+236x^{5}+17x^{4}+236x^{3}+17x^{2}+236x+17x\)
\end{center}

enter image description here


2. Here the linebreak doesn't work and the \hbox is overfull:

\[32x^{15}+91x^{14}+11x^{13}+120x^{12}+209x^{11}+114x^{10}+220x^{9}+77x^{8}+67x^{7}+64x^{6}+236x^{5}+17x^{4}+236x^{3}+17x^{2}+236x+17x\]

enter image description here


3. Here the background is colored, but the linebreak doesn't work anymore:

\begin{center}
    \colorbox{grn}{
        \(32x^{15}+91x^{14}+11x^{13}+120x^{12}+209x^{11}+114x^{10}+220x^{9}+77x^{8}+67x^{7}+64x^{6}+236x^{5}+17x^{4}+236x^{3}+17x^{2}+236x+17x\)
    }
\end{center}

enter image description here


I already tried multiple different things, but couldn't find a solution, that worked for me. So how can I have the math formulas centered and the background colored at the same time without having an overfull hbox?

Any help is appreciated.

imnothere
  • 14,215

3 Answers3

3

I'd not rely on automatic line breaking in this case.

\documentclass{article}
\usepackage{amsmath,mathtools,xcolor}

\definecolor{grn}{RGB}{210,253,210}

\begin{document}

\begin{equation} \begin{gathered} 32,91,11,120,209,114,220,77,67,64,236,17,236,17,236,17 \ \downarrow \ \colorbox{grn}{$\begin{multlined} 32x^{15}+91x^{14}+11x^{13}+120x^{12}+209x^{11}+114x^{10}+220x^{9}+77x^{8} \ +67x^{7}+64x^{6}+236x^{5}+17x^{4}+236x^{3}+17x^{2}+236x+17x \end{multlined}$} \end{gathered} \end{equation}

\end{document}

enter image description here

egreg
  • 1,121,712
3

The empheq package has tools for that:

\documentclass{article}
\usepackage{geometry} 
\usepackage{empheq}
\usepackage[svgnames]{xcolor}
\newcommand{\eqcolorbox}[1]{\colorbox{YellowGreen!25}{#1}}

\begin{document}

\begin{empheq}[box=\eqcolorbox]{gather*} 32x^{15}+91x^{14}+11x^{13}+120x^{12}+209x^{11}+114x^{10}+220x^{9}+77x^{8}+67x^{7}+64x^{6}+ {}\ 236x^{5}+17x^{4}+236x^{3}+17x^{2}+236x+17x \end{empheq}

\end{document}

enter image description here

Bernard
  • 271,350
2

With tcolorbox and amsmath's gather:

enter image description here

\documentclass{article}
\usepackage[svgnames]{xcolor}

\usepackage[most]{tcolorbox}

\newtcolorbox{coloredequation}{ams gather*, colback=LightGreen, sharp corners, boxrule=0pt, frame hidden,} \begin{document}

\begin{coloredequation} 32x^{15}+91x^{14}+11x^{13}+120x^{12}+209x^{11}+114x^{10}+220x^{9}+77x^{8}\ +67x^{7}+64x^{6}+236x^{5}+17x^{4}+236x^{3}+17x^{2}+236x+17x \end{coloredequation}

\end{document}

leandriis
  • 62,593