Math & ScienceLarge box around a part of an 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

Large box around a part of an equation ?

Post by Cham »

I just came across a new problem that I didn't had before, for my usual documents. I now need to show a calculation on several lines (using the align environment), then emphasize the end result on the last line with a large box around it. How to achieve this ?

Here's a MWE code to work with :

Code: Select all

\documentclass[12pt,letterpaper]{article}
\usepackage{amsmath}
\usepackage{empheq}

\begin{document}

Test :

	\begin{align}
		3d + 7 y - 3 x &= aba \nonumber \\[12pt]
		&= 32g hy - \frac{1}{2} \, x y 
	\end{align}

\end{document}
In my usual documents, I simply put the whole equation inside a box using the following code in the preamble :

Code: Select all

\newenvironment{important}[2][]{
	\setkeys{EmphEqEnv}{#2}
	\setkeys{EmphEqOpt}{box={\setlength{\fboxsep}{10pt}\fcolorbox{black}{gray}},#1}
	\EmphEqMainEnv}
{\endEmphEqMainEnv}
However, this would not work for a part of an align display. How could I enclose the last part (after the equal sign) inside a large box ?

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

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Large box around a part of an equation ?

Post by Cham »

Doh ! Apparently, the simple command \boxed{} do the trick ! :lol:

Is there another way in doing this ? Maybe with a color background inside the box ?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Large box around a part of an equation ?

Post by cgnieder »

You can have fancy boxes with tcolorbox and its theorems library:

Code: Select all

\documentclass[12pt,letterpaper]{article}
\usepackage{amsmath}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\begin{document}

Test :

\begin{align}
  3d + 7 y - 3 x &= aba \nonumber \\[12pt]
                 &= \tcboxmath{32g hy - \frac{1}{2} \, x y}
\end{align}

\end{document}
Regards
site moderator & package author
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Large box around a part of an equation ?

Post by Cham »

Wow ! This box is nice, thanks ! Maybe a bit too big, though. I'll check the documentation.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Large box around a part of an equation ?

Post by cgnieder »

Cham wrote:Maybe a bit too big, though. I'll check the documentation.
tcolorbox has so many possibilities customization that I have no doubt the box can be adapted to whatever you wish.
site moderator & package author
Post Reply