# How to align equation numbers inside and outside of mdframed?

I am using mdframed to box equations to include the equation number, as I quite like how it looks. However I've noticed that this results in a misalignment between equation numbers that are boxed, and those that are not. Is there an easy fix for this, or is it recommended to use a different method to mdframed?

Below is an example of how I'm using it.

\documentclass{article}

\usepackage{amsmath}
\usepackage{mdframed}

\begin{document}

Here is an important equation I would like to highlight:
\begin{mdframed}
$$e^{i\pi}=-1$$
\end{mdframed}

Here is an equation not important enough to deserve a highlight:
$$y=mx+c$$

\end{document}


• Unless you make the box run into the right margin, aligning the equation numbers with those outside won't look good, I think. So I am wondering what you want to achieve. Do you want to shift ordinary equation numbers to the left, or run the frame through the numbers, or let the frame intrude the right margin?
– user121799
Oct 3, 2018 at 18:50
• I reckon the best would probably be to shift the unframed numbers left a little - I imagine there's a simple command for that I could probably look up
– Garf
Oct 3, 2018 at 20:02

It is easiest to let the frame extend in to the right margin:

\documentclass{article}

\usepackage{amsmath}
\usepackage{mdframed}

\newlength{\mywidth}
\setlength{\mywidth}{\dimexpr\linewidth+\mdflength{innerrightmargin}+\mdflength{linewidth}}

\begin{document}

Here is an important equation I would like to highlight:
\begin{mdframed}[userdefinedwidth=\mywidth]
$$e^{i\pi}=-1$$
\end{mdframed}

Here is an equation not important enough to deserve a highlight:
$$y=mx+c$$

\end{document}


To go the other way, you could perhaps include all displays in an mdframed, but just hide the lines:

\documentclass{article}

\usepackage{amsmath}
\usepackage{mdframed}

\newbool{equationframe}
\surroundwithmdframed[hidealllines=\ifbool{equationframe}{false}{true}]{equation}
\boolfalse{equationframe}

\begin{document}

Here is an important equation I would like to highlight but first
enough text to show the width of the line:
{\booltrue{equationframe}%
$$e^{i\pi}=-1$$}

Here is an equation not important enough to deserve a highlight but
first enough text to show the width of the line:
$$y=mx+c$$

\end{document}


Here I have added an extra flag dedicated to frames for equations, to avoid setting hidealllines globally and thus affecting other mdframed environments.