What is the best way to achieve this? Could I define a solution environment that will start with a \med skip and then color the box until I end the solution?
[Tex/LaTex] Background color box with xcolor
color
Related Solutions
Just use mdframed
package. Here is an example from the document with a little modification:
\documentclass{article}
\usepackage{xcolor}
\usepackage{mdframed}
\newtheorem{mdtheorem}{Theorem}
\newenvironment{theorem}%
{\begin{mdframed}[backgroundcolor=lightgray]\begin{mdtheorem}}%
{\end{mdtheorem}\end{mdframed}}
\begin{document}
\begin{theorem}
test
\end{theorem}
\end{document}
Update below: one way to deal with \intertext
in this method.
Normally \everymath
only regards inline, and there is \everydisplay
for display math. But amsmath
typesets things in a, hmm, complicated way. In particular using \everydisplay
to add a \color
command seems difficult (it works when amsmath
is not loaded). Anyway, here is a work-around:
\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{etoolbox}
\makeatletter
\AtBeginEnvironment{align*}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{align}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{equation*}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{equation}{\let\SetColor\@gobble \color{display}}
\makeatother
% maybe some other nice package has set-up things in \everymath!
\everymath\expandafter{\the\everymath\SetColor{inline}}
\definecolor{display}{rgb}{0,1,0}
\definecolor{inline}{rgb}{0,0,1}
\let\SetColor\color
\begin{document}
$xyz$
\begin{align*}
E &= mc^2\\
x^n+y^n&= z^n
\end{align*}
$$hello $$ %% for this we would need \everydisplay
%% but some interference with amsmath makes its use
%% a bit delicate.
\begin{equation}
E = mc^2
\end{equation}
\end{document}
Output:
Update: adding to the code above in the preamble
\everydisplay\expandafter{\the\everydisplay\SetColor{display}}
will make the displayed hello
also green. Note though that with amsmath
this works only because its environments have been patched here to annihilate \SetColor
. With amsmath
, \everydisplay {\color{green}}
causes an error. So here in align*
the \SetColor
does not do anything and it is the \color
inserted at the begin of the environment which sets the color.
To deal with \intertext
. One possibility.
\documentclass{article}
\usepackage{color}
\usepackage{amsmath}
\usepackage{etoolbox}
\makeatletter
\AtBeginEnvironment{align*}{\let\SetColor\@gobble \color{display}%
\Patchintertext}
\AtBeginEnvironment{align}{\let\SetColor\@gobble \color{display}\Patchintertext}
\AtBeginEnvironment{equation*}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{equation}{\let\SetColor\@gobble \color{display}}
\def\Patchintertext{\let\oldintertext@\intertext@
\def\intertext@{\oldintertext@\Patchintertext@}}
\def\Patchintertext@ {\let\oldintertext\intertext
\def\intertext ##1{\oldintertext
{\color{black}\let\SetColor\color ##1}}}
\makeatother
\everymath\expandafter{\the\everymath\SetColor{inline}}
\everydisplay\expandafter{\the\everydisplay\SetColor{display}}
\definecolor{display}{rgb}{.6,.6,.2}
\definecolor{inline}{rgb}{0,0,1}
\let\SetColor\color
\begin{document}
here is some text before with $xyz$ math inline,
\begin{align*}
E &= mc^2\\
\intertext {this is some intertext with math $a^n+b^n=c^n$ inside,}
x^n+y^n&= z^n\quad \text{(display)}
\end{align*}
and here is some more text after with again $A^B$ math inline.
$$this is some standard math display$$
\begin{equation}
E = mc^2
\end{equation}
\end{document}
Best Answer