How to highlight maths environments (in-line mode excluded) with a coloured rectangular background e.g. grey?
A \textwidth rectangular behind the {equation} environment.
How to highlight maths environments (in-line mode excluded) with a coloured rectangular background e.g. grey?
A \textwidth rectangular behind the {equation} environment.
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
\begin{equation}
1+1=2
\end{equation}
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,
}
% add it to {equation}
\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
\begin{equation}
1+1=2
\end{equation}
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 …
article with book everything works fine. What’s the error message you get?
– Tobi
Oct 19 '12 at 14:44
\[ a = 1 \]?
– hoeni
Aug 22 '14 at 10:58
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]
\begin{equation}
1+1=2
\end{equation}
\lipsum[2]
\begin{equation}
1+1=2
\end{equation}
\lipsum[3]
\end{document}

\fill[black!20] instead of \draw[fill=black!20,draw=none] ;-)
– Tobi
Oct 15 '12 at 22:42