[Tex/LaTex] How to add frame boxes around all equations

equations

How to add frame boxes to all equations in a LaTeX file, to make all the equations seems like

enter image description here

which can be obtained by

\[\boxed{a+b}\]

For example, transform this

enter image description here

into this

enter image description here

As far as I know, there are many types of equation environments in LaTeX. Such as

$a+b$, 

\[a+b\]

\begin{equation}
    a+b
\end{equation}

\begin{equation*}
    a+b
\end{equation*}

\begin{eqnarray}
    a+b \\
    c+d
\end{eqnarray}

\begin{align}
    a+b \\  
    c+d
\end{align}

\begin{equation}
    \left(
    \begin{matrix}
        a &b \\
        c &d
    \end{matrix}
    \right)
\end{equation}

And so on …

So, is there any magic method to add bounding boxes to all these equation environments without change the code of equations? I mean DO NOT modify the code of equations one by one, just like

$\boxed{a+b}$

\[\boxed{a+b}\]

\begin{equation}
    \boxed{a+b}
\end{equation}

\begin{equation*}
    \boxed{a+b}
\end{equation*}

\begin{eqnarray}
    \boxed{a+b} \\
    \boxed{c+d}
\end{eqnarray}

\begin{align}
    \boxed{a+b} \\
    \boxed{c+d}
\end{align}

\begin{equation}
\boxed{
    \left(
    \begin{matrix}
        a &b \\
        c &d
    \end{matrix}
    \right)
}
\end{equation}

Best Answer

(rewrote answer to let it handle multi-line math environments of amsmath package)

The following solution, which requires LuaLaTeX, handles inline math cases as well as single-line display-math environments such as equation, equation*, and displaymath. It also handles four of the multi-line equation environments of amsmath package -- align, alignat, gather, and multline, plus their "starred" variants -- with the caveat that any equation numbers are suppressed. (The only major standalone multi-line equation environment that's not automatically boxed is the flalign environment, and its starred cousin.)

The code provides two user-level LaTeX macros: \EqboxOn and \EqboxOff. The former turns on automatic boxing, and the latter turns it off. The Lua function that's enabled by \EqboxOn scans all input lines and inserts \boxed statements "on the fly", as needed. Note that the definitions of the math environments are not modified.

Observe that no code is provided to handle eqnarray and eqnarray* environments. This omission is deliberate: These environments are deprecated and should no longer be used.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{mathtools}
%% Lua-side code
\usepackage{luacode}
\begin{luacode*}
function eq_boxed ( s )   
  s = s:gsub ( "(\\%[)"       , "%1\\boxed{" )
  s = s:gsub ( "(\\%])"       , "}%1" )
  s = s:gsub ( "%$%$(.-)%$%$" , "\\[\\boxed{%1}\\]" ) -- $$ .. $$ 
  s = s:gsub ( "%$(.-)%$"     , "$\\boxed{%1}$" )     -- inline math
  s = s:gsub ( "(\\begin{displaymath})" , "%1\\boxed{" )
  s = s:gsub ( "(\\end{displaymath})"   , "}%1" )
  s = s:gsub ( "(\\begin{equation%*?})" , "%1\\boxed{" )
  s = s:gsub ( "(\\end{equation%*?})"   , "}%1" )
  -----
  s = s:gsub ( "(\\begin{align%*?})"    , "\\[\\boxed{\\begin{aligned}" )
  s = s:gsub ( "(\\end{align%*?})"      , "\\end{aligned}}\\]" )
  s = s:gsub ( "(\\begin{alignat%*?})"  , "\\[\\boxed{\\begin{alignedat}" )
  s = s:gsub ( "(\\end{alignat%*?})"    , "\\end{alignedat}}\\]" )
  s = s:gsub ( "(\\begin{gather%*?})"   , "\\[\\boxed{\\begin{gathered}" )
  s = s:gsub ( "(\\end{gather%*?})"     , "\\end{gathered}}\\]" )
  s = s:gsub ( "(\\begin{multline%*?})" , "\\[\\boxed{\\begin{multlined}" )
  s = s:gsub ( "(\\end{multline%*?})"   , "\\end{multlined}}\\]" )
return s
end
\end{luacode*}
%% LaTeX-side code
\newcommand\EqboxOn{\directlua{luatexbase.add_to_callback(
   "process_input_buffer", eq_boxed, "eq_boxed" )}}
\newcommand\EqboxOff{\directlua{luatexbase.remove_from_callback(
   "process_input_buffer", "eq_boxed" )}}

\begin{document}
\EqboxOn % turn on automatic boxing

$a+b$, $e^{i\pi}-1=0$

\[a+b\]

\[
c+d
\]

$$e+f$$

\begin{equation}
    g+h
\end{equation}

\begin{equation*}
    a^b+b^2=c^2
\end{equation*}

\begin{displaymath}
    1+1=2
\end{displaymath}

\begin{align}
    E   &= mc^2 \\
    b^2 &= a^2+2\int_a^b x\,dx
\end{align}

\begin{gather}
     d = e + f \\ 
     g = h + i + j + k
\end{gather}

\begin{multline*}
  d+e+f+g+h+i+j+k+l+m+n\\
     =o+p+q+r+s+t+u+v+w+x+y+z
\end{multline*}

\begin{alignat}{2}
x &= y_1-y_2+y_3-y_5+y_8-\dotsb&\quad& \text{by abc}\\
  &= y'\circ y^*                    && \text{by def}\\
  &= y(0) y'                        && \text {by Axiom 1.}
\end{alignat}

\end{document}