LaTeX forum ⇒ Math & ScienceSymetrical box around equation

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

Symetrical box around equation

Postby Cham » Tue Nov 28, 2017 1:50 pm

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 ?

  1. \documentclass[12pt,letterpaper,twoside]{book}
  2. \usepackage{xcolor}
  3.  
  4. \usepackage{empheq}
  5. \definecolor{beige}{RGB}{240,235,225}
  6.  
  7. \newenvironment{important}[2][]{
  8. \setkeys{EmphEqEnv}{#2}
  9. \setkeys{EmphEqOpt}{box={\setlength{\fboxsep}{10pt}\fcolorbox{black}{beige}},#1}
  10. \EmphEqMainEnv}
  11. {\endEmphEqMainEnv}
  12.  
  13. \begin{document}
  14.  
  15. \begin{important}{equation}
  16. E = m \, c^2.
  17. \end{important}
  18.  
  19. \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 ?

User avatar
Stefan Kottwitz
Site Admin
Posts: 8544
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Tue Nov 28, 2017 3:09 pm

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

  1. \documentclass[12pt,letterpaper,twoside]{book}
  2. \usepackage{xcolor}
  3.  
  4. \usepackage{empheq}
  5. \definecolor{beige}{RGB}{240,235,225}
  6.  
  7. \newenvironment{important}[2][]{
  8. \setkeys{EmphEqEnv}{#2}
  9. \setkeys{EmphEqOpt}{box={\setlength{\fboxsep}{10pt}\fcolorbox{black}{beige}},#1}
  10. \EmphEqMainEnv}
  11. {\endEmphEqMainEnv}
  12.  
  13. \newcommand*{\symleft}{\color{white}\left|\color{black}}
  14. \newcommand*{\symright}{\color{white}\right|\color{black}}
  15.  
  16. \begin{document}
  17.  
  18. \begin{important}{equation}
  19. \symleft E = m \, {{c^2}^2}^2. \symright
  20. \end{important}
  21.  
  22. \end{document}


Stefan
Site admin

User avatar
Cham
Posts: 820
Joined: Sat Apr 02, 2011 4:06 pm

Postby Cham » Tue Nov 28, 2017 8:48 pm

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 ?

  1. \begin{important}{align}
  2. E &= m \, c^2. \\[12pt]
  3. E &= \hbar \, \omega.
  4. \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: 8544
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Tue Nov 28, 2017 9:01 pm

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
Site admin


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 3 guests