TransWikia.com

Add Box to multiline-expression which is part of aligned-equation?

TeX - LaTeX Asked on February 9, 2021

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}
newcommand*bmarkeq{stepcounter{markeq}%
  tikz[remember picture]node(startframe-themarkeq){pstrut{height}};%
  kernfboxsep}
newcommand*emarkeq{kernfboxsep
  begin{tikzpicture}[remember picture,overlay]
    node (endframe-themarkeq){pstrut{depth}};
    draw[,black,opacity=0.8] (startframe-themarkeq.north) 
      rectangle (endframe-themarkeq.south);
  end{tikzpicture}%
}


begin{align*}
    mathrm{L}_{c}(theta) 
    = &sum_{i=1}^{m} ln pleft(x_{i}, z_{i} ; thetaright) 
    = 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
end{align*}

One Answer

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:

documentclass{article}
usepackage{mathtools}

begin{document}

begin{align*}
    mathrm{L}_{c}(theta)
    &=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}}
end{align*}

begin{align*}
    mathrm{L}_{c}(theta)
    &=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}}}
end{align*}

end{document} 

enter image description here

Answered by Bernard on February 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP