6

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.

David Carlisle
  • 757,742
pluton
  • 16,421
  • 3
    use the package empheq, it has it already –  Sep 20 '11 at 16:07
  • ah, too bad, I missed that one! I'll have a look at the code to see what I can do. But still, what I do not understand in the provided code, is that \abovedisplayskip and \belowdisplayskip control the vertical space inside the box? Why not outside? – pluton Sep 20 '11 at 16:13

4 Answers4

6

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

Moriambar
  • 11,466
  • yes, it is nice and better vertically centered than what I had. Is it because the minipage? It looks like there is a small spurious horitontal space before vitae? probably because there is a missing % after \endminipage – pluton Sep 20 '11 at 21:07
  • yes, you are right. I edited my answer –  Sep 20 '11 at 21:26
4

With tcolorbox help it's easy to declare your own environments for boxed equations

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

\tcbset{colback=red!20, colframe=red!80}

\newtcolorbox{fequation}[1][]{ams equation,size=small,#1}

\begin{document}
\lipsum[4]
\begin{equation}
\cos t=\pi
\end{equation}
\lipsum[4]
\begin{fequation}
\cos t=\pi
\end{fequation}
vitae, consequat in, pretium a, enim. Pellentesque congue.
\begin{fequation}[ams align]
\cos t&=\pi\\
\sin t&=-\pi
\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

Ignasi
  • 136,588
3

You can use floatto create your own floating environment, with the boxed option:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{float}
\usepackage{lipsum}

\floatstyle{boxed}
\newfloat{frameequation}{hbtp}{feq}[section]
\floatname{frameequation}{Equation}

\newcommand{\freq}[2]{%
\begin{frameequation}%
\ensuremath{\hfill #1 \hfill}%
\caption{#2}
\end{frameequation}%
}


\begin{document}

\section{Some math topic}

\lipsum[1]

\freq{\int\limits^{\infty}_{0}\left(\frac{1}{x}\right)dx=???}{One}

\lipsum[1]

\freq{\int\limits^{\infty}_{0}\left(\frac{1}{x}\right)dx=???}{Two}

\lipsum[1]

\freq{\int\limits^{\infty}_{0}\left(\frac{1}{x}\right)dx=???}{Three}

\lipsum[1]

\end{document}

Which produces:

enter image description here

Tom Bombadil
  • 40,123
1

In addition to the empheq package mentioned by Herbert, there's the package mdframed that provides similar functionality. A nice feature of the mdframed package is there's no complication if the block to be framed contains a page break -- the frame around it will also cross the page boundary. :-)

Mico
  • 506,678
  • yes, I'm aware of this package but for an equation (which won't break along pages) it may not be necessary even though it would still work. – pluton Sep 20 '11 at 16:32
  • Just a quick note -- the mdframed package is not the same as the framed package you're loading in your MWE. The mdframed package lets you define your own environments (which could include, say, an equation), so that's why I thought it might meet your needs. – Mico Sep 20 '11 at 16:58
  • @pluton: You can define the environment with the option nobreak ;-) – Marco Daniel Sep 20 '11 at 19:03
  • I know that your package would be perfectly working for this case but a simple box would be enough I guess, unless you have something super nice in mind :) – pluton Sep 20 '11 at 21:08