# How to highlight math environments with colored background?

How to highlight maths environments (in-line mode excluded) with a coloured rectangular background e.g. grey?

A \textwidth rectangular behind the {equation} environment.

-
have you seen these questions‌​? –  cmhughes Oct 15 '12 at 16:43
There's actually a very similar question: \boxed{}, TikZ and colored equation background, but what do you mean with The rectangular is expected to be below the equation? Do the answers provided in the question I linked satisfy you (besides the point of highlighting also the equation counter, but for that I could provide you an answer)? –  Claudio Fiandrino Oct 15 '12 at 16:43

It is possible to use mdframed and surround the equation environments.

\documentclass{article}

\usepackage{mdframed}
\usepackage{xcolor}
\surroundwithmdframed[
hidealllines=true,
backgroundcolor=black!20,
skipbelow=\baselineskip,
skipabove=\baselineskip
]{equation}

\begin{document}
Text
$$1+1=2$$
Text
\end{document}


Extended version

To add the background also to $…$ some more code is necessary:

\documentclass{article}

\usepackage{mdframed}
\usepackage{xcolor}
\usepackage{etoolbox}

% define a style
\mdfdefinestyle{mathbackground}{
hidealllines=true,
backgroundcolor=black!20,
skipbelow=\baselineskip,
skipabove=\baselineskip,
innertopmargin=1pt,
}

\surroundwithmdframed[style=mathbackground]{equation}
% ... similar for other environments

% add the environment to  (needs etoolbox)
\preto{$}{% \begin{mdframed}[style=mathbackground]% \vspace{-\baselineskip}% } \appto{$}{%
\end{mdframed}%
}

\begin{document}
Text
$$1+1=2$$
Text

Text
$1+1=2$
Text
\end{document}


But use this with care I’m not sure if there are any drawbacks. Take it as a quick & dirty solution …

-
Thanks Tobi. This is exactly what I meant, but I'm writing my report in book class. I tried your code. It seems it doesn't work in this class. –  Milad O. Oct 19 '12 at 7:49
@MilO: Sorry but I can’t reproduce your errer. If I just replace article with book everything works fine. What’s the error message you get? –  Tobi Oct 19 '12 at 14:44
Is there a way I could use this as in a shortcut equation like $a = 1$? –  hoeni Aug 22 at 10:58
@hoeni: See my edit. But I’m not sure if this has any drawbacks or fuses other problems … –  Tobi Aug 22 at 14:06
@Tobi works fine! Had to do some space tuning due to my LaTex Beamer Environment, but I can see the principle, thank you! –  hoeni Aug 27 at 14:10

I think I really didn't follow

The rectangular is expected to be below the equation and also the equation counter. In better words \textwidth.

Hence this answer ;) I don't know to what extent this will be useful to anybody. But for the sake of it, I present the following:

This solution uses etoolbox with its \AfterEndEnvironment macro.

\documentclass{article}
\usepackage{tikz,lipsum}
\usepackage{etoolbox}
\AfterEndEnvironment{equation}{{\tikz\draw[fill=black!20,draw=none] (0,0) rectangle (\textwidth, .3);}\par}

\begin{document}
\lipsum[1]
$$1+1=2$$
\lipsum[2]
$$1+1=2$$
\lipsum[3]
\end{document}


-
I suspect that the "below" in the question should be "behind" –  egreg Oct 15 '12 at 17:36
@egreg: hehe. If that is the case Tobi has already answered it. Let this answer be for fun ;) –  Harish Kumar Oct 15 '12 at 17:38
Also "empheq" (tex.ac.uk/tex-archive/help/Catalogue/entries/empheq.html) from the "mh" bundle er useful for something like this. –  Svend Tveskæg Oct 15 '12 at 19:28
Do you know that you can use \fill[black!20] instead of \draw[fill=black!20,draw=none] ;-) –  Tobi Oct 15 '12 at 22:42
'er' --> 'is' ... I hate it when I mix Danish and English! –  Svend Tveskæg Oct 17 '12 at 8:14