2

I use this code to change the appearance of the equation, but it is full of errors even if at the end it gives a result, please help me to correct it. Thank you .

\documentclass{article}

\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{environ}
\usepackage{lipsum}

\let\oldequation\equation
\let\endoldequation\endequation
\RenewEnviron{equation}{\everymath{\displaystyle}
\everydisplay{\displaystyle}
  \begin{minipage}{\textwidth}\centering
    \fcolorbox{white}{lightgray!30}{
      \begin{oldequation}\nonumber 
        \BODY 
      \end{oldequation}
    }
  \end{minipage}
}


\begin{document}

\begin{equation}
n!\underset{n\rightarrow +\infty }{\sim }\left( \frac{n}{e}\right) ^{n}.%
\sqrt{2\pi n}
\end{equation}%
\bigskip

\lipsum[1]

\end{document}
1
  • 1
    You cannot just wrap \fcolorbox around equation, that does not work even without the redefinition. Perhaps you should start by explaining what the endgoal is? THen perhaps others can come up with better solutions.
    – daleif
    Commented May 21 at 10:44

1 Answer 1

6

I guess you want to show the equation with a gray background.

\documentclass{article}

\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{environ}
\usepackage{lipsum}

\NewEnvironmentCopy{oldequation}{equation}
\NewEnvironmentCopy{oldequation*}{equation*}
\RenewDocumentEnvironment{equation}{b}{%
  \everymath{\displaystyle}%
  \everydisplay{\displaystyle}%
  \begin{oldequation}
  \fcolorbox{white}{lightgray!30}{$#1$}%
  \end{oldequation}
}{\ignorespacesafterend}
\RenewDocumentEnvironment{equation*}{b}{%
  \everymath{\displaystyle}%
  \everydisplay{\displaystyle}%
  \begin{oldequation*}
  \fcolorbox{white}{lightgray!30}{$#1$}%
  \end{oldequation*}
}{\ignorespacesafterend}


\begin{document}

\lipsum[1][1-4]
\begin{equation}
n!\underset{n\rightarrow +\infty }{\sim }\left( \frac{n}{e}\right) ^{n}\cdot\sqrt{2\pi n}
\end{equation}
\lipsum[2][1-4]
\begin{equation*}
n!\underset{n\rightarrow +\infty }{\sim }\left( \frac{n}{e}\right) ^{n}\cdot
\sqrt{2\pi n}
\end{equation*}
\lipsum[3][1-4]

\end{document}

Note that the period is never used as a multiplication symbol.

enter image description here

For rounded corners you can use tcolorbox.

\documentclass{article}

\usepackage{amsmath}
\usepackage{tcolorbox}

\usepackage{lipsum}

\newtcbox{\eqbox}{colback=lightgray!30,boxrule=0pt,box align=base}

\NewEnvironmentCopy{oldequation}{equation}
\NewEnvironmentCopy{oldequation*}{equation*}
\RenewDocumentEnvironment{equation}{b}{%
  \begin{oldequation}
  \mbox{\eqbox{$\displaystyle#1$}}
  \end{oldequation}
}{\ignorespacesafterend}
\RenewDocumentEnvironment{equation*}{b}{%
  \begin{oldequation*}
  \mbox{\eqbox{$\displaystyle#1$}}
  \end{oldequation*}
}{\ignorespacesafterend}


\begin{document}

\lipsum[1][1-4]
\begin{equation}
n!\underset{n\rightarrow +\infty }{\sim }\left( \frac{n}{e}\right) ^{n}\cdot\sqrt{2\pi n}
\end{equation}
\lipsum[2][1-4]
\begin{equation*}
n!\underset{n\rightarrow +\infty }{\sim }\left( \frac{n}{e}\right) ^{n}\cdot
\sqrt{2\pi n}
\end{equation*}
\lipsum[3][1-4]

\end{document}

enter image description here

5
  • Thank you very much, it's a good job, I wonder if it's possible to make the corners round
    – M.La
    Commented May 21 at 11:29
  • @M.La For that you need tcolorbox.
    – egreg
    Commented May 21 at 12:14
  • Sorry I couldn't use it. Can you help me please
    – M.La
    Commented May 21 at 18:39
  • @M.La I added an example. Look in the manual to modify it.
    – egreg
    Commented May 21 at 21:21
  • Thank you very much,
    – M.La
    Commented May 22 at 23:58

You must log in to answer this question.

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