[Tex/LaTex] framed equations

equationsframed

I'm aware of possibilities to frame an "important" equation in Latex as the one suggested in mathmode for instance but I thought it would be nice to have a dedicated environment instead of putting frames or boxes around equations on the fly. So I came up with what follows by just adapting the existing equation environment:

\documentclass[fleqn,12pt]{book}
\usepackage{microtype}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{kpfonts}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{framed}

\renewcommand*\FrameCommand{\fcolorbox{red!80}{red!20}}%
\makeatletter
\newenvironment{fequation}{%
%\setlength\abovedisplayskip{3pt}
%\setlength\belowdisplayskip{3pt}
  \incr@eqnum%
  \mathdisplay@push%
  \st@rredfalse \global\@eqnswtrue%
  \MakeFramed{%
  \vspace*{-\baselineskip}%
  \FrameRestore}%
  \mathdisplay{equation}%
}{%
  \endmathdisplay{equation}%
  \mathdisplay@pop%
  \ignorespacesafterend%
  \vspace*{-\baselineskip}
  \endMakeFramed\noindent%
}%
\makeatother

\begin{document}
Suspendisse lectus tortor, dignissim sit amet, adipiscing nec
\begin{equation}
\cos t=\pi
\end{equation}
porttitor, orci nec nonummy molestie, enim est eleifend mi, non
\begin{fequation}
\cos t=\pi
\end{fequation}
vitae, consequat in, pretium a, enim. Pellentesque congue.
\begin{fequation}
\begin{aligned}
&\cos t=\pi\\
&\sin t=-\pi
\end{aligned}
\end{fequation}
vitae, consequat in, pretium a, enim. Pellentesque congue.
\begin{fequation}
\biggl(\int_\Omega \frac{t^2}{t+1}\D{t}\biggr)=1
\end{fequation}
\end{document}

I've been playing around the \vspace* as well as \abovedisplayskip and \belowdisplayskip commands for tuning but I am wondering how to control the vertical white space between main text and framed equations? Also, any improvement that you find interesting is welcome.

Best Answer

here is your code with a simple box:

\documentclass[fleqn,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{kpfonts}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{amsmath}
\newsavebox\MBox

\newenvironment{fequation}{%
  \setlength\abovedisplayskip{0pt}%
  \setlength\belowdisplayskip{0pt}%
  \setlength\abovedisplayshortskip{0pt}%
  \setlength\belowdisplayshortskip{0pt}%
  \par\noindent\begin{lrbox}{\MBox}
  
  \minipage{\dimexpr \linewidth-2\fboxsep-2\fboxrule\relax}

  \equation\strut%
}{%
  \endequation%
  \endminipage%
  \end{lrbox}\fcolorbox{red!80}{red!20}{\usebox\MBox}%
  \ignorespacesafterend\par\noindent}%
    
\begin{document}
Suspendisse lectus tortor, dignissim sit amet, adipiscing nec
%
\begin{equation}
\cos t=\pi
\end{equation}
%
por
%
\begin{fequation}
\cos t=\pi
\end{fequation}
%
vitae, 
\begin{fequation}
\begin{aligned}
&\cos t=\pi\\
&\sin t=-\pi
\end{aligned}
\end{fequation}
%
vitae, consequat in, pretium a, enim. Pellentesque congue.
\begin{fequation}
\biggl(\int_\Omega \frac{t^2}{t+1}\D{t}\biggr)=1
\end{fequation}
%
\end{document}

enter image description here

Related Question