3

I would like my math equations to have a gray background. My initial attempt at this was to simply "sandwich" my align environment by a mdframed environment. It would seem far easier to me though if I could just define a new environment which handles that task for me so that I have to type less code every time I would like to use equations in my document.

My initial attempt at the environment was the following:

\newenvironment{matheq}
{
\begin{mdframed}[backgroundcolor=gray]
\align
}
{
\endalign
\end{mdframed}
}

So that I can use math equations like so:

\begin{matheq}
1+1=2
\end{matheq}

Regrettably, this does not work and I get the following error code:

! LaTeX Error: \begin{mdframed} on input line 7 ended by \end{matheq}.

Can someone help me out here?

  • Welcome to TeX.SX! This won't work this way, because of the special settings in align etc. environments. – user31729 Sep 18 '15 at 8:01
  • No \begin{...} should be in the “start” code (and no corresponding \end{...} in the “close” part). – egreg Sep 18 '15 at 8:06
3

This isn't a mdframed approach, but the tcolorbox package has special settings to create coloured box wrappers for ams environments like align, align*, equation and gather, see p. 293 of current manual (v. 3.72)

I've defined a separate tcolorbox environment for this with \newtcolorbox and set the ams align key, it's possible to specify some options individually with each call. For numbered boxes see the manual please (pages 14ff and 97ff)

\documentclass{article}



\usepackage[most]{tcolorbox}

\newtcolorbox{matheq}[1][]{ams align,colback=black!20!white,colframe=blue,#1}

\begin{document}
\begin{matheq}
\sum\limits_{n=1}^{\infty} \frac{1}{n} &= \infty.\\
\int x^2 ~\text{d}x &= \frac{1}{3} x^3 + c. \\
1 + 1 &= 2
\end{matheq}

\begin{matheq}[colback=yellow]
\sum\limits_{n=1}^{\infty} \frac{1}{n} &= \infty.\\
\int x^2 ~\text{d}x &= \frac{1}{3} x^3 + c.\\
1+1 &= 2
\end{matheq}

\end{document}

enter image description here

  • This works beautifully! Many thanks Christian! – Ivo Filot Sep 18 '15 at 8:13
  • @ifilot: Fine, happy TeXing then! – user31729 Sep 18 '15 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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