Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

A \textwidth rectangular behind the {equation} environment.

share|improve this question
    
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

2 Answers 2

up vote 10 down vote accepted

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

equation

\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 …

share|improve this answer
    
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
1  
@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 2 days ago

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}

enter image description here

share|improve this answer
3  
I suspect that the "below" in the question should be "behind" –  egreg Oct 15 '12 at 17:36
1  
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.