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 \begin{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
add comment

2 Answers

up vote 8 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}
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
add comment

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

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.