等式中不同项的背景颜色

等式中不同项的背景颜色

如何在一个方程中为不同的项设置不同的背景颜色。下面是我尝试的示例。我需要方程中方框项的背景为绿色,第二项为洋红色,最后一项为黄色背景。如何实现这一点。预期结果看起来像最后一个方程。

\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

执行部分如下——请看看它是否满足要求

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

在此处输入图片描述

    \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}

答案2

另一个选项是使用\colorbox包提供的xcolor。结果如下(以及一些代码的精简以及用明确的大小指令替换\left和指令):\right

在此处输入图片描述

\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}

相关内容