1

I would like to put a set of aligned equations into a colored box. After a quick search on tex.stackexchange, I have found the following solution to put (apparently) one equation in a color box :

>\newcommand{\boxedeq}[2]{\begin{empheq}[box={\fboxsep=6pt\fbox}]{align}\label{#1}#2\end{empheq}}
>\newcommand{\coloredeq}[2]{\begin{empheq}[box=\colorbox{lightgreen}]{align}\label{#1}#2\end{empheq}}

thanks to the empheq and framed packages. When I tried to use the align environment with coloredeq it did not work :

\documentclass[a4paper,10pt]{article}
\usepackage{empheq}
\usepackage{framed}
\usepackage{amsmath}
\newcommand{\boxedeq}[2]{\begin{empheq}[box={\fboxsep=6pt\fbox}]{align}\label{#1}#2\end{empheq}}
\newcommand{\coloredeq}[2]{\begin{empheq}[box=\colorbox{red}]{align}\label{#1}#2\end{empheq}}

\coloredeq{eq:myequation}{
\begin{align*}
f(x) & = {} g(x) \\
     & = h(x) 
\end{align*}
}

\end{document}

I shall also mention (in case this information is useful) that I would like to include this colored set of equations into a Beamer presentation. How can I use the coloredeq command with the align environment ?

  • 1
    You are missing a \begin{document}. Have you really found those definitions here? With the label included? You are using an align* which does not use labels at all. Please clarify a bit, what you want to get. Thank you. – LaRiFaRi Jun 9 '15 at 9:21
6

This doesn't solve the error in your code, but it answers the question you mentioned in the title. You can use Tcolorbox package that as its title suggests, creates color boxes. It is very well documented and it has many features. You can also use it in your Beamer presentation easily.

\documentclass[a4paper,10pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{amsmath}

\definecolor{yourcolor}{RGB}{128, 0, 0}

\begin{document}

\begin{tcolorbox}[ams align,colback=yellow!10!white,colframe=yourcolor]
f(x) & = {} g(x) \\
     & = h(x) 
\end{tcolorbox}

\end{document}

enter image description here

and if you want unnumbered align environment, simply use ams align* in the options of the environment. In fact, there is a library (\tcbuselibrary{theorems}) which allows you ams like environment within tcolorboxes.

  • Thank you for your helpful answer !! Quick question : how can I change the box color to a given color, defined by its RGB triplet ? I cannot find this information in the manual of the tcolorbox package. – jibounet Jun 9 '15 at 9:48
  • 1
    @jibounet colframe=yourcolor – LaRiFaRi Jun 9 '15 at 9:49
  • @jibounet, exactly as LaRiFaRi mentioned. Check the edited answer. – Pouya Jun 9 '15 at 9:59
  • Note unless one really wants an equation number each line this should be a \begin{split}...\end{split} inside an ams equation. Also the brackets {} in the first line of the equation are spurious. – Andrew Swann Jun 9 '15 at 10:01
4

You are mixing different environments here. In your MWE, you are putting an align* inside of an align which is defined in your preamble. Moreover, you are giving a label but what should that label refer to? To which line, to which number (especially if you do not want numbers)?

I changed the definitions to your needs. I hope I guessed right.

% arara: pdflatex

\documentclass{beamer}
\usepackage{empheq}
\newcommand{\boxedeq}[1]{\begin{empheq}[box={\fboxsep=6pt\fbox}]{align*}#1\end{empheq}}
\newcommand{\coloredeq}[1]{\begin{empheq}[box=\colorbox{red}]{align*}#1\end{empheq}}

\begin{document}
\begin{frame}
\boxedeq{%
    f(x) &= g(x) \\
    &= h(x) 
}
\coloredeq{%
    f(x) &= g(x) \\
    &= h(x) 
}
\end{frame}
\end{document} 

enter image description here

3

Here is a solution that defines a colouredeqs environment, which has the same syntax as the empheq environment (optional key-valued arguments, and AmS environment as a mandatory argument). The background colour can be changed in the preamble via \colourlet{bkgcolour}{…}. It relies on xcolor and empheq.

Comment aside, it is pointless to load amsmath if you load empheq, since the latter loads mathtools, which loadsamsmath`:

\documentclass[a4paper,10pt]{article}
\usepackage[x11names]{xcolor}
\usepackage{empheq}

\colorlet{bkgcolour}{Thistle2!40!}
\newcommand*\mycolouredbox[1]{%
\colorbox{bkgcolour}{\hspace{1em}#1\hspace{1em}}}
\newenvironment{colouredeqs}[2][]{%
\setkeys{EmphEqEnv}{#2}%
\setkeys{EmphEqOpt}{box=\mycolouredbox,#1}%
\EmphEqMainEnv}%
{\endEmphEqMainEnv}

\begin{document}

\begin{colouredeqs}[left = \color{VioletRed4}\empheqlbrace]{align*}
  f(x) & = g(x) \\
  & = h(x)
\end{colouredeqs}

\end{document} 

enter image description here

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.