latexmath-mode

How can I change default background color in math mode in tex?


I would like to change the default background color while writing in math mode in latex (but not for the inline math mode). I can achieve the result that I want using, for example, this piece of code:

\documentclass{article}
\usepackage{tikz,lipsum,lmodern}
\usepackage[most]{tcolorbox}
\begin{document}

    \begin{tcolorbox}[arc=0pt, colback=gray!10, boxrule=0pt]
    \[
       \begin{aligned}
          \text{this is an equation } &\text{ written inside a math mode block}\\
          &\sum_k x_k=\mathcal{G}
    \end{aligned}
    \]
    \end{tcolorbox}
\end{document}

enter image description here

I would like this to be the default behavior of math mode. How can I achieve this? Is there any way to "override" the behavior of math mode?


Solution

  • You can use the empheq package:

    \documentclass{article}
    
    \usepackage{xcolor}
    
    \usepackage[overload2]{empheq}
    
    \newcommand*\mybox[1]{%
      \colorbox{gray!10}{\hspace{1em}#1\hspace{1em}}%
    }
    \empheqset{box=\mybox}
        
    \begin{document}
    
    \[
           \begin{aligned}
              \text{this is an equation } &\text{ written inside a math mode block}\\
              &\sum_k x_k=\mathcal{G}
        \end{aligned}
    \]
        
    \end{document}
    

    enter image description here