# How to box two lines inside align*

I asked a similar question today except it was targeted at one particular line inside the align* environment. Now I want to enclose two lines. The failed attempt below makes my intention clear:

\begin{align*}
\ce{K_a $\times$ K_b} &= \ce{K_w}\\
-\log(\ce{K_a $\times$ K_b}) &= -\log\ce{K_w}\\
-( \log(\ce{K_a}) + \log(\ce{K_b}) ) &= -\log\ce{K_w} & \text{(Using log law for LHS.)} \\
- \log(\ce{K_a}) - \log(\ce{K_b}) &= -\log\ce{K_w}\\
\fbox{
\begin{minipage}{\textwidth}
\ce{pK_a} + \ce{pK_b} &= \ce{pK_w}\\
&=14.00 \text{ (at \SI{25}{\celsius})}\\
\end{minpage}
}
\end{align*}


\Aboxed from mathtools pkg was a great solution, you didn't have to specify mandatory parameters and it was as simple as Aboxed{ } - alas it only works for single line boxes.

As for \Aboxed I don't want to be asked how wide the box should be - it should be automatically determined.

As usual, I prefer predefined solutions, and less obstrusive code to achieve the desired effect. The less clutter the better.

P.S. By the way, this manual seems to suggest \mbox, and anologously \fbox ought to draw frames around the enclosed content. Previously I could not get \mbox to display visible frames...

-
\mbox was never designed to draw frames. –  egreg Oct 2 '11 at 14:27
I've edited the OP to include the mandatory width argument for minipage - does anyone know why it's still not working? I'd like to try and get it to work the way above since the solutions below are far too advanced for me for the time being... –  ptrcao Oct 2 '11 at 17:17
@ptrcao: Honestly, I don't know. This must be connected in some way to the implementation of the align environment and the internal plumbings of (La)TeX. That's a bit of black magic for me too. :) –  Count Zero Oct 2 '11 at 20:26
@ptrcao: A very simple (and very ugly) approach would be to break down the align environment into two parts, one that is going to be framed and the other that is not. Then you can put the 'framable' content into a \parbox and frame the \parbox. But here again, you must specify manually the width of the box. NOTE: I deliberately didn't post this as an answer, because it's just too ugly... –  Count Zero Oct 2 '11 at 21:03
@ptrcao: I took a look at empheq. Reading the documentation, it is clear to me that it is way more difficult to achieve what you want with that one than with the answers already given. Trust me, I've been in a similar bind when I had to typeset some ugly colored frame around section titles (I had to follow a strict template). The titlesec package did the trick, but I spent a week until I figured it out. I guess I'm still pretty far from being a (La)TeX wizzard... –  Count Zero Oct 3 '11 at 19:58
show 1 more comment

Here an inspiration with tikz

(everyone is welcome to modify the example -- i am not familiar with tikz)

\documentclass{article}
\usepackage{mathtools}
\usepackage{mhchem}
\usepackage{siunitx}

\usepackage{tikz}
\newcounter{markeq}
\setcounter{markeq}{0}
\newcommand*\bmarkeq{%
\stepcounter{markeq}%
\tikz[remember picture]\node(startframe-\themarkeq){\strut};}
\newcommand*\emarkeq{%
\begin{tikzpicture}[remember picture,overlay]
\node (endframe-\themarkeq){\strut};
\draw[,red,opacity=0.8] (startframe-\themarkeq.north) rectangle (endframe-\themarkeq.south);
\end{tikzpicture}%
}

\begin{document}

\begin{align*}
\ce{K_a $\times$ K_b} &= \ce{K_w}\\
-\log(\ce{K_a $\times$ K_b}) &= -\log\ce{K_w}\\
-( \log(\ce{K_a}) + \log(\ce{K_b}) ) &= -\log\ce{K_w} & \text{(Using log law for LHS.)} \\
- \log(\ce{K_a}) - \log(\ce{K_b}) &= -\log\ce{K_w}\\
\bmarkeq\ce{pK_a} + \ce{pK_b} &= \ce{pK_w}\\
&=14.00 \text{ (at \SI{25}{\celsius})}\emarkeq
\end{align*}

\end{document}


EDIT

With the very great idea of egreg you get:

\documentclass{article}
\usepackage{mathtools}
\usepackage{mhchem}
\usepackage{siunitx}

\usepackage{tikz}
\newcounter{markeq}
\setcounter{markeq}{0}

\newcommand{\pstrut}[1]{\vrule height0pt depth0pt width0pt #1 \fboxsep}
\newcommand*\bmarkeq{\stepcounter{markeq}%
\tikz[remember picture]\node(startframe-\themarkeq){\pstrut{height}};%
\kern\fboxsep}
\newcommand*\emarkeq{\kern\fboxsep
\begin{tikzpicture}[remember picture,overlay]
\node (endframe-\themarkeq){\pstrut{depth}};
\draw[,red,opacity=0.8] (startframe-\themarkeq.north)
rectangle (endframe-\themarkeq.south);
\end{tikzpicture}%
}

\begin{document}

\begin{align*}
\ce{K_a $\times$ K_b} &= \ce{K_w}\\
-\log(\ce{K_a $\times$ K_b}) &= -\log\ce{K_w}\\
-( \log(\ce{K_a}) + \log(\ce{K_b}) ) &= -\log\ce{K_w} & \text{(Using log law for LHS.)} \\
- \log(\ce{K_a}) - \log(\ce{K_b}) &= -\log\ce{K_w}\\
\bmarkeq\ce{pK_a} + \ce{pK_b} &= \ce{pK_w}\\
&=14.00 \text{ (at \SI{25}{\celsius})}\emarkeq
\end{align*}

\end{document}


-

Here's an attempt that mimics the approach taken by \Aboxed from the mathtools package:

\documentclass{article}
\usepackage[version=3]{mhchem}% http://ctan.org/pkg/mhchem
\usepackage{calc}% http://ctan.org/pkg/calc
\usepackage{textcomp}% http://ctan.org/pkg/textcomp
\usepackage{siunitx}% http://ctan.org/pkg/siunitx

\begin{document}

\newlength{\mylen}% Length of widest element on left of &
\settowidth{\mylen}{$\displaystyle \ce{pK_a} + \ce{pK_b}$}

\begin{align*}
\ce{K_a $\times$ K_b} &= \ce{K_w} \\
-\log(\ce{K_a $\times$ K_b}) &= -\log\ce{K_w}\\
-( \log(\ce{K_a}) + \log(\ce{K_b}) ) &= -\log\ce{K_w} & \text{(Using log law for LHS.)} \\
- \log(\ce{K_a}) - \log(\ce{K_b}) &= -\log\ce{K_w}\\
\kern\mylen & \kern-\mylen\fbox{$% \begin{array}{@{}l@{}}% \ce{pK_a} + \ce{pK_b} = \ce{pK_w} \\[\jot]% \phantom{\ce{pK_a} + \ce{pK_b}} = 14.00 \text{ (at \SI{25}{\textcelsius})} \end{array}$}
\end{align*}

\end{document}


-