Math & ScienceSymetrical box around equation

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Symetrical box around equation

Post by Cham »

I'm using the code below to draw a nice fancy box around some important equations in my documents. I would like to draw a vertically symetrical box around the equal sign, whatever the symbols entering the equation. Currently, the box is a bit too high above the "=", and I have to hack the equation by adding some invisible symbols (adding \phantom{\frac{1}{2}}, for example, but then the box may become too big). Is there a way to modify the macro so all boxes are perfectly symetrical (vertical symetry !), while not too big at the same time ?

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{xcolor}
 
\usepackage{empheq}
\definecolor{beige}{RGB}{240,235,225}

\newenvironment{important}[2][]{
	\setkeys{EmphEqEnv}{#2}
	\setkeys{EmphEqOpt}{box={\setlength{\fboxsep}{10pt}\fcolorbox{black}{beige}},#1}
	\EmphEqMainEnv}
{\endEmphEqMainEnv}

\begin{document}
 
	\begin{important}{equation}
		E = m \, c^2.
	\end{important}
 
\end{document}
Take note that some equations may not have an "=" in them. Also, using other environments like align (instead of equation) may cause some problems with the vertical symetry idea. How to define a vertical symetry if there are two aligned equations one on top of another ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Symetrical box around equation

Post by Stefan Kottwitz »

At least a quick hack :-) auto-scaling delimiters around the equation, vertically centered, made white:

Code: Select all

\documentclass[12pt,letterpaper,twoside]{book}
\usepackage{xcolor}
 
\usepackage{empheq}
\definecolor{beige}{RGB}{240,235,225}
 
\newenvironment{important}[2][]{
	\setkeys{EmphEqEnv}{#2}
	\setkeys{EmphEqOpt}{box={\setlength{\fboxsep}{10pt}\fcolorbox{black}{beige}},#1}
	\EmphEqMainEnv}
{\endEmphEqMainEnv}

\newcommand*{\symleft}{\color{white}\left|\color{black}}
\newcommand*{\symright}{\color{white}\right|\color{black}}

\begin{document}
 
	\begin{important}{equation}
		\symleft E = m \, {{c^2}^2}^2. \symright
	\end{important}
 
\end{document}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Symetrical box around equation

Post by Cham »

I guess the white color used is just to show the effect ?

And how is this working for any equation ? Should this hack be part of the box macro itself, instead of inserting the vertical bars separately ?

And how should this aply to boxed aligned equations like these below ?

Code: Select all

\begin{important}{align}
		E &= m \, c^2. \\[12pt]
		E &= \hbar \, \omega.
	\end{important}
Actually, my problem is to get an aesthetical pleasing box around any equation, with equilibrated space above and below the boxed equation. Without any hack, the box is often not well "equilibrated" (too much space above or under the equation).
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Symetrical box around equation

Post by Stefan Kottwitz »

The white color was for "hiding" the bar, so it takes space without being visible. (\vphantom doesn't work with the auto-sizing delimiter)

Yes, it could be part of the box macro, but that's a bit more work and at first one should decide if to choose that way.

Because of the vertical centering of both the equal sign and the delimiter, the distance will be symmetrical. That the vertically centered delimiter adjusts its size automatically to height and depth of the equation, is a nice thing that we used, so it's a solution for single equations.

Multiple equations in a single box are a different beast. Again, one could use delimiters with an aligned environment inside, or array, \venter, ... On the other hand, boxed and colored equations are maybe for school kids books and not so for serious publications, so I'm not sure if it's really useful to work on that... maybe at the end you might choose a serious formal classical display anyway.

Stefan
LaTeX.org admin
Post Reply