3

How to put a different background color for different terms in one single equation. Below is the example I tried. I need boxed term in the equation to have a background of green, the second term with magenta, and the last term to be a yellow background. how to achieve this. The expected result looks like the last equation.

\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}

\newcommand{\diff}{\mathop{}\!\mathrm{d}}

\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}

\usepackage{empheq}
\usepackage{xcolor}
\definecolor{lightgreen}{HTML}{90EE90}
\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}}

\newcommand{\diff}{\mathop{}\!\mathrm{d}}

\begin{document}
\begin{subequations}
 \begin{align}
 \mathcal{\bar{L}}&=\boxed{\textcolor{red}{\frac{\beta^4}{2}\left(\int_{0}^{1}\,\hspace{-0.05in} \left( \psi(\eta)\right)^2 \diff\eta + \sum_{j=1}^{n} \gamma_{j}^3\, \int_{0}^{\gamma_{j}}  \hspace{-0.05in} \left( \chi_{j}(\xi)\right)^2 \diff\xi \right)}}-\nonumber \\ &\quad  \textcolor{blue}{\frac{1}{2}\,\left(\int_{0}^{1} \hspace{-0.05in} \left(\frac{\partial^2\psi(\eta)}{\partial \eta^2}\right)^2 \diff\eta  +\sum_{j=1}^{n}\alpha_{j}\, \int_{0}^{\gamma_{j}}\left(\frac{\partial \chi_{j}(\xi)}{\partial \xi}\right)^2 \diff\xi \right)}+\nonumber \\ &\quad \sum_{j=1}^{n}\lambda_{j}^{*}\left(\psi(\zeta_{j})-\chi_{j}(\gamma)\right) \nonumber  
 \end{align}
 \end{subequations}
\coloredeq{eq:second}{c_i = \langle\psi|\phi\rangle} 
\end{document}

1 Answer 1

7

The operative part is given below -- please see if it meets the rquirement

          \tikz[baseline]{\node[fill=green!25, rectangle, anchor=base]
                {.............}%<-------------------------------item to be colored 
                }

enter image description here

    \documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand{\diff}{\mathop{}\!\mathrm{d}}

\begin{document}
\begin{subequations}
 \begin{align}
 \mathcal{\bar{L}}&=\boxed{\textcolor{red}{\frac{\beta^4}{2}\left(\int_{0}^{1}\,\hspace{-0.05in} \left( \psi(\eta)\right)^2 \diff\eta + \sum_{j=1}^{n} \gamma_{j}^3\, \int_{0}^{\gamma_{j}}  \hspace{-0.05in} \left( \chi_{j}(\xi)\right)^2 \diff\xi \right)}}-\nonumber \\ &\quad  \textcolor{blue}{\frac{1}{2}\,\left(\int_{0}^{1} \hspace{-0.05in} \left(\frac{\partial^2\psi(\eta)}{\partial \eta^2}\right)^2 \diff\eta  +\sum_{j=1}^{n}\alpha_{j}\, \int_{0}^{\gamma_{j}}\left(\frac{\partial \chi_{j}(\xi)}{\partial \xi}\right)^2 \diff\xi \right)}+\nonumber \\ &\quad \sum_{j=1}^{n}\lambda_{j}^{*}\left(\psi(\zeta_{j})-\chi_{j}(\gamma)\right) \nonumber  
 \end{align}
 \end{subequations}
{eq:second} \tikz[baseline]{
                \node[fill=green!25, rectangle, anchor=base]
                {$c_i = \langle\psi|\phi\rangle$} 
                            }
\end{document}

Answer2

The other option is to use \colorbox, provided by the package xcolor. Here's the result (along with some streamlining of your code and replacing \left and \right directives with explicit sizing instructions):

enter image description here

\documentclass{article}
\usepackage{xcolor,amsmath,tikz}
\usetikzlibrary{shapes}
\newcommand{\diff}{\mathop{}\!\mathrm{d}}

\begin{document}
%\begin{subequations}
\begin{align*}
\colorbox{green}{$\mathcal{\bar{L}}$}
   &=\boxed{%
   \textcolor{red}{ \frac{\beta^4}{2} \biggl(\,
   \colorbox{green}{$\displaystyle \int_{0}^{1} 
   ( \psi(\eta))^2 \diff\eta$} 
   + \sum_{j=1}^{n} \gamma_{j}^3 \int_{0}^{\gamma_{j}}  
   ( \chi_{j}(\xi) )^2 \diff\xi 
   \biggr) } % end \textcolor
   }  % end \boxed
   \\ 
   &\quad - 
   \textcolor{blue}{ \frac{1}{2} \biggl( 
   \int_{0}^{1} \biggl(
   \frac{\partial^2\psi(\eta)}{\partial \eta^2}\biggr)^{\!2} \diff\eta  
   +\sum_{j=1}^{n}\alpha_{j} \! \int_{0}^{\gamma_{j}}
   \biggl(\frac{\partial \chi_{j}(\xi)}{\partial \xi}\biggr)^{\!2} 
   \diff\xi \biggr)}\\ 
   &\quad +
   \sum_{j=1}^{n}\lambda_{j}^{*} \bigl(\psi(\zeta_{j})-\chi_{j}(\gamma)\bigr)  
\end{align*}
%\end{subequations}
{eq:second} \tikz[baseline]{
                \node[fill=green!25, rectangle, anchor=base]
                {$c_i = \langle\psi|\phi\rangle$} 
                            }
\end{document}
3
  • not working showing some package error, Package tikz Error: Giving up on this path. Did you forget a semicolon? ... {$c_i = \langle\psi|\phi\rangle$} }
    – vijay
    Commented Jun 21, 2020 at 6:28
  • edited the answer --please have a look
    – js bibra
    Commented Jun 21, 2020 at 6:36
  • 2
    +1. I've taken the liberty of streamlining the code in "Answer 2"; feel free to revert.
    – Mico
    Commented Jun 21, 2020 at 7:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .