# 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

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}


Here is a variant of this answer that:

• doesn't rely on luck1 to look right (try setting inner sep=0 in the \node options, printing the reference point of each TikZ picture or simply changing the \vrule width in the second code sample to something non-zero like 1pt and you'll see :-);

• respects the height of the first boxed line and the depth of the last boxed line (this is the tricky part!);

• allows one to tune each of the six interesting lengths (four inside and two outside the frame), like this:

\setlength{\boxPartOfAlignOuterBeforeSep}{...}%
\setlength{\boxPartOfAlignOuterAfterSep}{...}%
\setlength{\boxPartOfAlignBeforeSep}{...}%
\setlength{\boxPartOfAlignAfterSep}{...}%
\setlength{\boxPartOfAlignLeftSep}{...}%
\setlength{\boxPartOfAlignRightSep}{...}%


Here is the code:

\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{mhchem}
\usepackage{siunitx}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\newcounter{markeq}

% Spacing above the top rule and below the bottom rule of our frames
\newlength{\boxPartOfAlignOuterBeforeSep}
\newlength{\boxPartOfAlignOuterAfterSep}

% Spacing inside the frames
\newlength{\boxPartOfAlignBeforeSep}
\newlength{\boxPartOfAlignAfterSep}
\newlength{\boxPartOfAlignLeftSep}
\newlength{\boxPartOfAlignRightSep}

\newcommand*{\bmarkeq}{%
\noalign{%
\nointerlineskip
\kern
\dimexpr \boxPartOfAlignOuterBeforeSep + \boxPartOfAlignBeforeSep
+ \jot \relax
\vbox to 0pt
{\hbox to \hsize{\tikzmark{aboveLine-\themarkeq}\hss}\vss}%
\nointerlineskip}%
\tikz[remember picture] \coordinate (before-\themarkeq) {};%
}

\newcommand*{\emarkeq}{%
\tikz[remember picture] \coordinate (after-\themarkeq) {};%
%
\begin{tikzpicture}[remember picture, overlay]
\coordinate (upperLeftCorner) at
([shift={(-\boxPartOfAlignLeftSep,\boxPartOfAlignBeforeSep)}]
before-\themarkeq |- {pic cs:aboveLine-\themarkeq});

\coordinate (lowerRightCorner) at
([shift={(\boxPartOfAlignRightSep,-\boxPartOfAlignAfterSep)}]
after-\themarkeq |- {pic cs:belowLine-\themarkeq});

\draw[red,opacity=0.8] (upperLeftCorner) rectangle (lowerRightCorner);
\end{tikzpicture}%
%
% Insert an empty line in the alignment in order to be able to put vertical
% material before it (in particular a zero height, zero depth \vbox
% containing a \tikzmark that tells us where the bottom of the just-finished
% line is located).
\\
\noalign{%
\nointerlineskip
\vbox to 0pt{\hbox to \hsize{\tikzmark{belowLine-\themarkeq}\hss}\vss}%
\stepcounter{markeq}%
\nointerlineskip
% Insert “after skip” and compensate for the empty line (maybe imperfect?)
\kern \dimexpr \boxPartOfAlignAfterSep + \boxPartOfAlignOuterAfterSep
-\baselineskip + \jot \relax}%
\null\hfill                                   % this is the empty line
}

% Test material that gets repeated in every align* env (used to make the
% repeated examples shorter).
\newcommand*{\startMaterial}{%
\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} +}

\begin{document}
\setlength{\boxPartOfAlignOuterBeforeSep}{0pt}%
\setlength{\boxPartOfAlignOuterAfterSep}{0pt}%
\setlength{\boxPartOfAlignBeforeSep}{0pt}%
\setlength{\boxPartOfAlignAfterSep}{0pt}%
\setlength{\boxPartOfAlignLeftSep}{0pt}%
\setlength{\boxPartOfAlignRightSep}{0pt}%
Let's start with tight settings (all six tunable lengths set to 0pt):
\begin{align*}
\startMaterial
\ce{pK_b} &= \ce{pK_w} \\
&= 14.00 \text{ (at \SI{25}{\celsius})}\emarkeq
\end{align*}
Text after the alignment. \lipsum[1][1-3]

Same thing with lines following the boxed line(s):
\begin{align*}
\startMaterial
\ce{pK_b} &= \ce{pK_w}\\
&= 14.00 \text{ (at \SI{25}{\celsius})}\emarkeq\\
&= \text{whatever}
\end{align*}

{%
\setlength{\jot}{10pt}%
Same thing with more spacing between two consecutive lines of the alignment
(\verb|\jot| set to \verb|10pt|):
\begin{align*}
\startMaterial
\ce{pK_b} &= \ce{pK_w}\\
&= 14.00 \text{ (at \SI{25}{\celsius})}\emarkeq\\
&= \text{whatever}
\end{align*}%
}
\lipsum[2][1-8]                 % fill the first page (for the screenshots...)

The height of the first boxed line and the depth of the last boxed line are
perfectly respected by our frame:
\begin{align*}
\startMaterial
\vrule width 0.4pt height 20pt depth 0pt\relax
\ce{pK_b} &= \ce{pK_w} \\
&= \vrule width 0.4pt height 0pt depth 10pt\relax
14.00 \text{ (at \SI{25}{\celsius})}\emarkeq\\
&= \text{whatever}
\end{align*}

\setlength{\boxPartOfAlignOuterBeforeSep}{3pt}%
\setlength{\boxPartOfAlignOuterAfterSep}{3pt}%
\setlength{\boxPartOfAlignBeforeSep}{3pt}%
\setlength{\boxPartOfAlignAfterSep}{3pt}%
\setlength{\boxPartOfAlignLeftSep}{3pt}%
\setlength{\boxPartOfAlignRightSep}{3pt}%
Now, with all six tunable lengths set to 3pt:
\begin{align*}
\startMaterial
\vrule width 0.4pt height 20pt depth 0pt\relax
\ce{pK_b} &= \ce{pK_w} \\
&= \vrule width 0.4pt height 0pt depth 10pt\relax
14.00 \text{ (at \SI{25}{\celsius})}\emarkeq\\
&= \text{whatever}
\end{align*}

\setlength{\boxPartOfAlignOuterBeforeSep}{3pt}%
\setlength{\boxPartOfAlignOuterAfterSep}{6pt}%
\setlength{\boxPartOfAlignBeforeSep}{2pt}%
\setlength{\boxPartOfAlignAfterSep}{4pt}%
\setlength{\boxPartOfAlignLeftSep}{4pt}%
\setlength{\boxPartOfAlignRightSep}{8pt}%
Finally, with funky values for the six tunable lengths:\\*[1ex]\hspace*{3em}%
\begin{tabular}{@{}>{\ttfamily\textbackslash}l@{\hspace{2em}}l}
boxPartOfAlignOuterBeforeSep & 3pt\\
boxPartOfAlignOuterAfterSep & 6pt\\
boxPartOfAlignBeforeSep & 2pt\\
boxPartOfAlignAfterSep & 4pt\\
boxPartOfAlignLeftSep & 4pt\\
boxPartOfAlignRightSep & 8pt\\
\end{tabular}
%
\begin{align*}
\startMaterial
\ce{pK_b} &= \ce{pK_w} \\
&= 14.00 \text{ (at \SI{25}{\celsius})}\emarkeq\\
&= \text{whatever}
\end{align*}

\end{document}


First page:

Second page:

With a minor change:

\newcommand*{\emarkeq}{%

(...)

\draw[blue, opacity=0.8, rounded corners]
(upperLeftCorner) rectangle (lowerRightCorner);
\end{tikzpicture}%

(...)

}


one can obtain:

Footnote:

1. Or rather, on the default value of inner sep for TikZ nodes.

One can also use the empheq package. For example

\begin{empheq}[box=\fbox]{align}
a&=b\\
E&=mc^2 + \int_a^b x dx
\end{empheq}


will produce the following output :

More info is available on CTAN or in the pdf documentation of the empheq package.

EDIT: It seems like this package only allows boxing all the align environment and not the two last lines for example. I'm not sure however. Please note that I don't delete this answer because of its simplicity.

• This suggestion may not suits with the OP's requirement, hence empheq will draw a box for the whole equation not the part of an equation... – MadyYuvi Jul 23 '19 at 12:38
• @MadyYuvi That's what I precised in the "Edit" part of my answer – Rphad Dec 26 '19 at 22:09