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 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}

-
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

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
show 1 more comment