I want to add a box (black color) to encapsulate the last two lines of this triple-line-aligned expression.

As you can see, the current solution inspired by this post is not perfect because it misses some topmost components.

enter image description here

Note that the target is a part of the aligned-expression so the general commends like boxed or empheq may not work.

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

    = &\sum_{i=1}^{m} \ln p\left(x_{i}, z_{i} ; \theta\right) \\
    = \bmarkeq &\sum_{i=1}^{m}\left(z_{i}\left[\ln (\pi)+x_{i} \ln \left(p_{r}\right)+\left(1-x_{i}\right) \ln \left(1-p_{r}\right)\right] \\
    &+ \left(1-z_{i}\right)\left[\ln (1-\pi)+x_{i} \ln \left(p_{b}\right)+\left(1-x_{i}\right) \ln \left(1-p_{b}\right)\right]\right) \emarkeq

1 Answer 1


You don't need TiKZ to do that: amsmath has the \boxed command and mathtools adds the \Aboxed command, which may include the alignment points. Here are two suggestions to do that:



    &=\sum_{i=1}^{m} \ln p(x_{i}, z_{i} ; \theta) \\
   & =\boxed{\begin{aligned}[t] &\sum_{i=1}^{m}\bigl(z_{i}\bigl[\ln (\pi)+x_{i} \ln (p_{r})+(1-x_{i}) \ln(1-p_{r})\bigr]\\
   &+(1-z_{i})\bigl[\ln (1-\pi)+x_{i} \ln(p_{b})+(1-x_{i}) \ln(1-p_{b})\bigr]\bigr)\end{aligned}}

    &=\sum_{i=1}^{m} \ln p(x_{i}, z_{i} ; \theta) \\
\Aboxed{& ={\begin{aligned}[t]&\sum_{i=1}^{m}\bigl(z_{i}\bigl[\ln (\pi)+x_{i} \ln (p_{r})+(1-x_{i}) \ln(1-p_{r})\bigr]\\
   &+(1-z_{i})\bigl[\ln (1-\pi)+x_{i} \ln(p_{b})+(1-x_{i}) \ln(1-p_{b})\bigr]\bigr)\end{aligned}}}


enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .