How to make fbox fit automatically around wide equations using empheq package

Is there a trick to make fbox used with empheq fit around the equations, even if they are too wide, or does not have to manually enter numerical values for sizes in this case?

One set of my equations I'd like to put a box around are too wide, and I get this: MWE

\documentclass[12pt,notitlepage]{article}%
\usepackage{amsmath}
\usepackage{empheq}
\usepackage{siunitx}
\begin{document}

\begin{empheq}[box=\fbox]{align*}
\frac{v}{r}  & =2.651e^{-0.055268t}+0.4867e^{-56.5025t}+7.849e^{-0.4218t}
\left(  \cos\left(  2.2873t+\SI{80.15}{\degree}\right)  +\sin\left(  2.2873t+
\SI{80.15}{\degree}\right)  \right)  \\
%
\frac{p}{r}  & =-0.4348e^{-0.055268t}+\overset{\text{compare to g=0
below}}{\overbrace{168.36e^{-56.5025t}}}+1.543e^{-0.4218t}\left(  \cos\left(
2.2873t-\SI{98.95}{\degree}\right)  +
\sin\left(  2.2873t-\SI{98.95}{\degree}\right)  \right)  \\
%
\frac{r}{r}  & =e^{-0.055268t}+e^{-56.5025t}+e^{-0.4218t}\left(
\cos2.2873t+\sin2.2873t\right)  \\
%
\frac{\phi}{r}  & =133.09e^{-0.055268t}-50.4e^{-56.5025t}+11.22e^{-0.4218t}
\left(  \cos\left(  2.2873t-\SI{199.39}{\degree}\right)
+\sin\left(  2.2873t-\SI{199.39}{\degree}\right)  \right)
\end{empheq}

\end{document}

I've seen couple of related questions, but too complicated for me to follow. I hope there is a simple way to just tell Latex to make the box as wide as needed? May be I should be using something other than fbox in this case?

• I guess that something like \tikzmark is better; but I wouldn't typeset such a thing for any amount of money. – egreg Apr 2 '14 at 22:19

Not really automatic, but the result seems near to what you want. But I'd do this in my document for no amount of money.

\documentclass[12pt,notitlepage]{article}
\usepackage[pass,showframe]{geometry}
\usepackage{amsmath,empheq}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

\begin{empheq}[box=\mbox]{align*}
\tikzmark{A}
\frac{v}{r}  & =2.651e^{-0.055268t}+0.4867e^{-56.5025t}+7.849e^{-0.4218t}
\left(  \cos\left(  2.2873t+\SI{80.15}{\degree}\right)  +\sin\left(  2.2873t+
\SI{80.15}{\degree}\right)  \right)  \\
%
\frac{p}{r}  & =-0.4348e^{-0.055268t}+\overset{\text{compare to g=0
below}}{\overbrace{168.36e^{-56.5025t}}}+1.543e^{-0.4218t}\left(  \cos\left(
2.2873t-\SI{98.95}{\degree}\right)  +
\sin\left(  2.2873t-\SI{98.95}{\degree}\right)  \right)  \\
%
\frac{r}{r}  & =e^{-0.055268t}+e^{-56.5025t}+e^{-0.4218t}\left(
\cos2.2873t+\sin2.2873t\right)  \\
%
\frac{\phi}{r}  & =133.09e^{-0.055268t}-50.4e^{-56.5025t}+11.22e^{-0.4218t}
\left(  \cos\left(  2.2873t-\SI{199.39}{\degree}\right)
+\sin\left(  2.2873t-\SI{199.39}{\degree}\right)  \right)\tikzmark{B}
\end{empheq}
\begin{tikzpicture}[remember picture,overlay]
\draw ([shift={(-.2em,3.5ex)}]pic cs:A) rectangle ([shift={(1.6em,-2.5ex)}]pic cs:B);
\end{tikzpicture}
\end{document} • Thanks, it works. I had to compile the file more than one time to see the frame around for some reason. Nice idea of using \end{tikzpicture}. How to remove all the other frames and lines that are showing up and just leave the frame around the equations? – Nasser Apr 2 '14 at 22:54
• @Nasser: Yeah, I prefer to use the tikzmark solution for drawing as well, and all tiksmark will require at least two runs to stabilize. – Peter Grill Apr 2 '14 at 23:03
• @Nasser The other frames are made by geometry, I used them to show the page frame. Just remove the call to the geometry package (or, if you use it, the showframe option). – egreg Apr 2 '14 at 23:11
• @egreg why would you not want to do this for your own documents? – Jörg Apr 2 '14 at 23:20
• @Jörg A display that spills into both margins? No way. ;-) – egreg Apr 2 '14 at 23:31

Not Recommended:

One solution would be to change the margins before and _after the empheq. Below I have applied the solution from How can I change the margins for only part of the text? to adjust the margins: However, I would recommend the alignat* solution below.

Notes:

• The showframe package was used just to show the page margins. It is not needed in your actual use.
• It is definitely an not automatic aolution, and I do now know why the margins had to be tweaked to be asymmetric in the use of the ChangeMargin environment.

Recommended Solution:

While this does not really answer your question, I would recommend you re-format the equations so that are more readily parasable by humans by aligning the appropriate points: Notes:

• I also added some vertical space between the equations via the optional parameter to \\.
• \llap was used so that the \overset does not effect the alignment.

Code:

\documentclass[12pt,notitlepage]{article}%
\usepackage{amsmath}
\usepackage{empheq}
\usepackage{siunitx}
\usepackage{showframe}

% https://tex.stackexchange.com/questions/588/how-can-i-change-the-margins-for-only-part-of-the-text
\def\ChangeMargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}%
\let\endChangeMargin=\endlist%

\begin{document}

\begin{ChangeMargin}{-10.0em}{-20.0ex}%
\begin{empheq}[box=\fbox]{align*}
\frac{v}{r}  & =2.651e^{-0.055268t}+0.4867e^{-56.5025t}+7.849e^{-0.4218t}
\left(  \cos\left(  2.2873t+\SI{80.15}{\degree}\right)  +\sin\left(  2.2873t+
\SI{80.15}{\degree}\right)  \right)  \\
%
\frac{p}{r}  & =-0.4348e^{-0.055268t}+\overset{\text{compare to g=0
below}}{\overbrace{168.36e^{-56.5025t}}}+1.543e^{-0.4218t}\left(  \cos\left(
2.2873t-\SI{98.95}{\degree}\right)  +
\sin\left(  2.2873t-\SI{98.95}{\degree}\right)  \right)  \\
%
\frac{r}{r}  & =e^{-0.055268t}+e^{-56.5025t}+e^{-0.4218t}\left(
\cos2.2873t+\sin2.2873t\right)  \\
%
\frac{\phi}{r}  & =133.09e^{-0.055268t}-50.4e^{-56.5025t}+11.22e^{-0.4218t}
\left(  \cos\left(  2.2873t-\SI{199.39}{\degree}\right)
+\sin\left(  2.2873t-\SI{199.39}{\degree}\right)  \right)
\end{empheq}
\end{ChangeMargin}
\end{document}

Code: Recommended

\documentclass[12pt,notitlepage]{article}%
\usepackage{amsmath}
\usepackage{empheq}
\usepackage{siunitx}
\usepackage{showframe}

\begin{document}

\begin{empheq}[box=\fbox]{alignat*=5}
\frac{v}{r}   &={} &2.651 e^{-0.055268t}   &+{} &0.4867e^{-56.5025t}                                                    &+{} &7.849e^{-0.4218t} &f(\SI{80.15}{\degree}) \\
\frac{p}{r}   &={} &-0.4348 e^{-0.055268t} &+{} &\overset{\llap{$\scriptstyle\text{compare to g=0 below}$}}{\overbrace{168.36e^{-56.5025t}}} &+{} &1.543e^{-0.4218t} &f(\SI{98.95}{\degree}) \$2ex] \frac{r}{r} &={} &e^{-0.055268t} &+{} & e^{-56.5025t} &+{} &e^{-0.4218t} &f(\SI{0}{\degree}) \\[2ex] \frac{\phi}{r}&={} &133.09 e^{-0.055268t} &-{} & 50.4e^{-56.5025t} &+{} &11.22e^{-0.4218t} &f(\SI{199.39}{\degree}) \end{empheq} where \[ f(x) = \cos\left( 2.2873t + x\right) + \sin\left( 2.2873t+ x\right)$

\end{document}

You have to cut 3 of the 4 equations that are too long to fit within the margins. That can be done with the multlined environment from the \ mathtools package (it's loaded by empheq, and it loadsamsmath, so it is unnecessary to load both of them). I loaded empheq with theoverloadoption – which simplifies the typing, and defined awidebox` command, that has a better looking result, in my opinion.

\documentclass[12pt,notitlepage]{article}%
%\usepackage{amsmath}
\usepackage[showframe, nomarginpar]{geometry}
\usepackage{siunitx}
\newcommand*{\widebox}[1em]{\setlength{\fboxsep}{7pt}\fbox{\hspace{#1}$\displaystyle #2$\hspace{#1}}}
\begin{document}

\begin{align*}[box=\widebox]%\small
\frac{v}{r}  & = \begin{multlined}[t]
2.651e^{-0.055268t}+0.4867e^{-56.5025t}\\+7.849e^{-0.4218t}
\left(  \cos\left(  2.2873t+\SI{80.15}{\degree}\right)  +\sin\left(  2.2873t+
\SI{80.15}{\degree}\right)  \right)
\end{multlined} \\[6pt]
%
\frac{p}{r}  & =\begin{multlined}[t]-0.4348e^{-0.055268t}+\overset{\text{compare to g=0
below}}{\overbrace{168.36e^{-56.5025t}}} \\ +1.543e^{-0.4218t}\left(  \cos\left(
2.2873t-\SI{98.95}{\degree}\right)  +
\sin\left(  2.2873t-\SI{98.95}{\degree}\right)  \right)
\end{multlined}  \\[6pt]
%
\frac{r}{r}  & =e^{-0.055268t}+e^{-56.5025t} +e^{-0.4218t}\left(
\cos2.2873t+\sin2.2873t\right)\\[6pt]
%
\frac{\phi}{r}  & = \begin{multlined}[t]133.09e^{-0.055268t}-50.4e^{-56.5025t}\\+11.22e^{-0.4218t}
\left(  \cos\left(  2.2873t-\SI{199.39}{\degree}\right)
+\sin\left(  2.2873t-\SI{199.39}{\degree}\right)  \right)
\end{multlined}
\end{align*}
\end{document} 