Math & Scienceamsmath: How to box an equation within an align environment?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
mabe83
Posts: 8
Joined: Sat Oct 23, 2010 6:51 pm

amsmath: How to box an equation within an align environment?

Post by mabe83 »

Hi. Big problem here!
How can I box an equation within an align environment?
Using \boxed seem to have no effect?!

Here is my example:

Code: Select all

\begin{align*}
\text{recall}    &= \frac{TP}{TP+FN} \lbr
\text{precision} &= \frac{TP}{TP+FP} \lbr
\text{TP rate}   &= \text{recall} \lbr
\text{FP rate}   &= \frac{FP}{FP+TN} 
\end{align*}
Now I want to put a box around.

Thanks in advance!
Last edited by mabe83 on Mon Oct 25, 2010 11:55 am, edited 1 time in total.

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

mabe83
Posts: 8
Joined: Sat Oct 23, 2010 6:51 pm

amsmath: How to box an equation within an align environment?

Post by mabe83 »

Ok, I found a solution. May it's not the best, but it works!

Code: Select all

\fbox{\begin{minipage}{0.5\textwidth}
	\begin{align*}
	\text{recall}    &= \frac{TP}{TP+FN}  \lbr
	\text{precision} &= \frac{TP}{TP+FP}  \lbr
	\text{TP rate}   &= \text{recall}     \lbr
	\text{FP rate}   &= \frac{FP}{FP+TN}  \lbr[1]
	\end{align*}
	\end{minipage}}
Do you have any different (may the "right") way?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

amsmath: How to box an equation within an align environment?

Post by gmedina »

Hi,

you could use the empheq package; a little example:

Code: Select all

\documentclass{book}
\usepackage{empheq}

\begin{document}

\begin{empheq}[box=\fbox]{align}
\begin{split}
  \text{recall}    &= \frac{TP}{TP+FN}  \\
  \text{precision} &= \frac{TP}{TP+FP}  \\
  \text{TP rate}   &= \text{recall}     \\
  \text{FP rate}   &= \frac{FP}{FP+TN}  \\
\end{split}
\end{empheq}

\end{document}
AS a side note, notice that your example (and mine also, since I don't know the meaning of your expressions) is inconsistent: on the left-hand-side you typeset TP and FP in text mode, but on the right-hand-side they are typeset in math mode; they should be treated consistently on both sides.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
mabe83
Posts: 8
Joined: Sat Oct 23, 2010 6:51 pm

amsmath: How to box an equation within an align environment?

Post by mabe83 »

Thank you very much! This package is really brilliant.

Since formulas become counted on the right side, is there any way to list all these formulas? I think on a "list of formulas". Currently I put a figure around, in order to capture it. May there is a better way for this purpose?
gmedina wrote: AS a side note, notice that your example (and mine also, since I don't know the meaning of your expressions) is inconsistent: on the left-hand-side you typeset TP and FP in text mode, but on the right-hand-side they are typeset in math mode; they should be treated consistently on both sides.
May I'm wrong. I thought (normal) text must be treated with the \text instruction. When I remove the \text then I lose white-spaces in between. What should I use instead of \text?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

amsmath: How to box an equation within an align environment?

Post by gmedina »

mabe83 wrote:...Since formulas become counted on the right side, is there any way to list all these formulas? I think on a "list of formulas". Currently I put a figure around, in order to capture it. May there is a better way for this purpose?
Should the formulae receive captions?
mabe83 wrote:...
gmedina wrote: AS a side note, notice that your example (and mine also, since I don't know the meaning of your expressions) is inconsistent: on the left-hand-side you typeset TP and FP in text mode, but on the right-hand-side they are typeset in math mode; they should be treated consistently on both sides.
May I'm wrong. I thought (normal) text must be treated with the \text instruction. When I remove the \text then I lose white-spaces in between. What should I use instead of \text?
What I meant is that you should use either always text mode for FP and TN or always use math mode for FP and TN:

Code: Select all

\documentclass{book}
\usepackage{empheq}

\begin{document}

% FP and TN always in text mode
\begin{empheq}[box=\fbox]{align}
\begin{split}
  \text{recall}    &= \frac{\text{TP}}{\text{TP}+\text{FN}}  \\
  \text{precision} &= \frac{\text{TP}}{\text{TP}+\text{FP}}  \\
  \text{TP rate}   &= \text{recall}     \\
  \text{FP rate}   &= \frac{\text{FP}}{\text{FP}+\text{TN}}  \\
\end{split}
\end{empheq}

% FP and TN always in math mode
\begin{empheq}[box=\fbox]{align}
\begin{split}
  \text{recall}    &= \frac{TP}{TP+FN}  \\
  \text{precision} &= \frac{TP}{TP+FP}  \\
  TP \text{ rate}   &= \text{recall}     \\
  FP \text{ rate}   &= \frac{FP}{FP+TN}  \\
\end{split}
\end{empheq}

\end{document}
In your example, they are typeset both in text and math mode and that is inconsistent.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
mabe83
Posts: 8
Joined: Sat Oct 23, 2010 6:51 pm

amsmath: How to box an equation within an align environment?

Post by mabe83 »

gmedina wrote: In your example, they are typeset both in text and math mode and that is inconsistent.
Ah, ok, I see... Sounds more consequent. Thanks for advising me. 8-)
gmedina wrote: Should the formulae receive captions?
Yes, would be great. Additionally, the caption should be defined for "the listing" purpose only. In my text I already explain the meaning of each formula, while referring its number. Therefore, the description should appear on the formula-listing-site only.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

amsmath: How to box an equation within an align environment?

Post by gmedina »

In the following example code I did the preparations needed to create a List of Formulas and defined the command \CFormula to create an entry in the List of Formulas:

Code: Select all

\documentclass{article}
\usepackage{empheq}

% this command will write its argument to the List of Formulas
\newcommand{\CFormula}[1]{%
  \stepcounter{equation}%
  \addcontentsline{lofo}{figure}{\protect\makebox[2em][l]{\theequation}#1}%
  \addtocounter{equation}{-1}%
}
\makeatletter
\newcommand\listformulaname{List of Formulas}
\newcommand\listofformulas{%
  \section*{\listformulaname}\@starttoc{lofo}}% change to \chapter* in book and report
\makeatother

\begin{document}
\listofformulas

\begin{empheq}[box=\fbox]{equation}
  a=b
\CFormula{A really interesting formula}%
\end{empheq}
text text text
\begin{empheq}[box=\fbox]{equation}
  a=b
\CFormula{Another really interesting formula}
\end{empheq}
text text text

\end{document}
Feel free to modify my example to suit your needs.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
mabe83
Posts: 8
Joined: Sat Oct 23, 2010 6:51 pm

Re: amsmath: How to box an equation within an align environm

Post by mabe83 »

Really really great!!! That's exactly what I was looking for :D :D :D
Thank you so much!!!

The only one more thing that could be added is a link (or reference) to its origin. Any idea? :?: :roll:
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

amsmath: How to box an equation within an align environment?

Post by gmedina »

mabe83 wrote:...Thank you so much!!!

The only one more thing that could be added is a link (or reference) to its origin. Any idea? :?: :roll:
You're welcome. If you load the hyperref package, links will be added to the entries in the List of Formulas.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
mabe83
Posts: 8
Joined: Sat Oct 23, 2010 6:51 pm

Re: amsmath: How to box an equation within an align environm

Post by mabe83 »

Works perfectly :D
Thank you very much! 8-) 8-) 8-)
Post Reply