TransWikia.com

Formula alignment in a transition matrix

TeX - LaTeX Asked by Didier on June 15, 2021

I used the issue raised in the link below to build 2 alternative ways of generating a transition matrix for a Markov Chain.
However, as you can see, in the first version, my formula are losing their expected alignment.
And in the second, m column and row labels lose their alignments.

The link:
Creating a transition probability matrix

Could you please help me identify what is going wrong? I have tried editing a lot of this with no success.

Option 1:

usepackage{amsmath, blkarray}
[
mathbf{P} = 
begin{blockarray}{c@{hspace{1pt}}rrrrr@{hspace{3pt}}}
    & 0   & 1   & 2   & 3   & 4 
    begin{block}{r@{hspace{1pt}}|@{hspace{1pt}}|@{hspace{1pt}}
            rrrrr@{hspace{1pt}}|@{hspace{1pt}}|}
        0 & 0 & $frac{1}{3}(frac{2}{3}$)^{1-1}$ & $frac{1}{3}(frac{2}{3}$)^{2-1}$  & $frac{1}{3}(frac{2}{3}$)^{3-1}$ & {$frac{1}{3}(frac{2}{3}$)^{4-1}$}
        1 & 1.0 & 0 & 0  & 0 & 0
        2 & 0   & 1.0   & 0 & 00 & 0   
        3 & 0   & 0   & 1.0 & 0 & 0   
        4 & 0   & 0   & 0 & 1.0 & 0   
    end{block}
end{blockarray}
]

enter image description here

Option 2:

usepackage{scalerel,tabstackengine,xpatch}
setstacktabbedgap{1em}
xpatchcmdCenterstack{strutlongstacks{T}}{}{}{}
[
defstackalignment{c}
mathbf{P} = 
Centerstack{
    0  
    1  
    2  
    3  
    4  
}!
stackon{
    stretchleftright{|!|}{tabbedCenterstack{
            0.0 & {$frac{1}{3}(frac{2}{3}$)^{1-1}$} & {$frac{1}{3}(frac{2}{3}$)^{2-1}$} & {$frac{1}{3}(frac{2}{3}$)^{3-1}$} & {$frac{1}{3}(frac{2}{3}$)^{4-1}$}
            1.0 & 0.0 & 0.0 & 0.0 & 0.0 
            0.0   & 1.0   & 0   & 0.0 & 0.0 
            0.0 & 0.0 & 1.0 & 0.0 & 0.0 
            0.0 & 0.0 & 0.0 & 1.0 & 0.0
    }}{|!|}}{tabbedCenterstack{
        protectphantom{0.5}0 & protectphantom{0.5}1 & protectphantom{0.5}2 & protectphantom{0.5}3 & protectphantom{0.5}4}
    kern1pt}
]

enter image description here

Thank you very much for the help!

It is much appreciated.

One Answer

It's much easier with nicematrix:

documentclass{article}
usepackage{amsmath, nicematrix}

begin{document}

[
mathbf{P} =
begin{VNiceMatrix}[first-col,first-row]
  & 0   & 1   & 2   & 3   & 4 
0 & 0   & frac{1}{3}(frac{2}{3})^{1-1}
        & frac{1}{3}(frac{2}{3})^{2-1}
        & frac{1}{3}(frac{2}{3})^{3-1}
        & frac{1}{3}(frac{2}{3})^{4-1} 
1 & 1.0 & 0 & 0  & 0 & 0
2 & 0   & 1.0   & 0 & 00 & 0   
3 & 0   & 0   & 1.0 & 0 & 0   
4 & 0   & 0   & 0 & 1.0 & 0   
end{VNiceMatrix}
]

end{document}

enter image description here

The entries of a matrix, be it XNiceMatrix, Xmatrix, array or blockarray are already in math mode, so no $ should be used. (The X stands for any of the allowed character for getting the various matrix delimiters.)

Answered by egreg on June 15, 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