# Add a fitted box to a multiline equation

I am trying to draw a fitted box around some equations. However, I was not able to control the width of the box. How can I achieve this?

\documentclass{beamer}
\usepackage{amssymb,amsmath,empheq}

\newcommand*\widefbox[1]{\fbox{\hspace{2em}#1\hspace{2em}}}

\begin{document}

\begin{frame}{Higher Order Equations}
\begin{itemize}
\item Must make into a system of first-order equations to use ODE solvers
\item Nonlinear is OK!
\item Pendulum example:
\end{itemize}
\scriptsize
\begin{flalign*}
&\ddot{\theta}+\frac{g}{L}sin(\theta)=0&\\
&\ddot{\theta}=-\frac{g}{L}sin(\theta)&\\
&\text{let } \dot{\theta} = \gamma&\\
&\dot{\gamma}=-\dfrac{g}{L}sin(\theta)&
\end{flalign*}
\begin{subequations}
\begin{empheq}[box=\widefbox]{flalign*}
&\overrightarrow{x} = \begin{bmatrix}\theta\\\gamma\end{bmatrix}&\\
&\dfrac{d\overrightarrow{x}}{dt} =  \begin{bmatrix}\dot{\theta}\\\dot{\gamma}\end{bmatrix}&
\end{empheq}
\end{subequations}
\normalsize
\end{frame}

\end{document}


In order to make the framed box fit its content one needs to measure the contents width first. In this case I simply assigned the content to measure to box register 0, i.e. \setbox0=\hbox{...} and retrieve its width from \wd0. So, your box macro will be defined as

\newcommand*\WideFittedFramebox[1]{%
\setbox0=\hbox{\hspace{2em}#1\hspace{2em}}%
\framebox[\the\wd0]{\hspace{2em}#1\hspace{2em}}%
}


Note that I changed its name and that \framebox now is employed to create the frame and not \fbox as the \framebox command takes an optional argument that sets the width. (Otherwise one would need to \fbox{\parbox{\the\wd0}{...}}.)

• using \scriptsize inside a group instead of \scriptsize...\normalsize,
• the gather environment together with the fleqn option instead of the flalign enviroment with empty columns,
• \sin... instead of plain sin... and
• by nesting the equation blocks properly inside the subequations environment.

The complete code then is

\documentclass[fleqn]{beamer}
\usepackage{amssymb,amsmath,empheq}

\newcommand*\WideFittedFramebox[1]{%
\setbox0=\hbox{\hspace{2em}#1\hspace{2em}}
\framebox[\the\wd0]{\hspace{2em}#1\hspace{2em}}
}

\begin{document}

\begin{frame}{Higher Order Equations}
\begin{itemize}
\item Must make into a system of first-order equations to use ODE solvers
\item Nonlinear is OK!
\item Pendulum example:
\end{itemize}

\begingroup \scriptsize
\begin{subequations}
\begin{gather*}
\ddot{\theta}+\frac{g}{L}\sin(\theta)=0\\
\ddot{\theta}=-\frac{g}{L}\sin(\theta)\\
\text{let }\dot{\theta}=\gamma\\
\dot{\gamma}=-\dfrac{g}{L}\sin(\theta)
\end{gather*}
\begin{empheq}[box=\WideFittedFramebox]{gather*}
\overrightarrow{x}=\begin{bmatrix}\theta\\\gamma\end{bmatrix}\\
\dfrac{d\overrightarrow{x}}{dt}=\begin{bmatrix}\dot{\theta}\\\dot{\gamma}\end{bmatrix}
\end{empheq}
\end{subequations}
\endgroup
\end{frame}

\end{document}


• Is there an alternative to fleqn? fleqn would flush all my equations to the left.
– MAS
Commented Jul 27, 2015 at 13:33
• @MAS -- Yes, there is the fleqn environment of the nccmath package (that allows flushleft display math locally), but there is some incopatibility problem (the equations in the framed block just disappear) that I can't localize/fix quickly. You could ask a follow-up question on this. Commented Jul 27, 2015 at 14:43
• scriptsize environment? There's no such thing. It works by mere fortune. \scriptsize is a font switch, so use it inside a group to restrict its scope. Commented Jul 28, 2015 at 0:13
• @GonzaloMedina -- I can't prove it, but it actually does exist ;) And the code compiles without any \endscriptsize undefined errors. Commented Jul 29, 2015 at 23:53
• @GonzaloMedina -- I don't know if the following source is trustworthy, but here is a first clue: de.wikibooks.org/wiki/…, texblog.org/2012/08/29/changing-the-font-size-in-latex Commented Jul 29, 2015 at 23:55