[Tex/LaTex] Creating a transition probability matrix

arraysmatrices

I would like to create a transition probability matrix, like the
ones found in a textbook I'm using, An Introduction to Stochastic Modeling
by Mark A. Pinsky and Samuel Karlin.

The double bars are closer together than just using ||. Also, the spacing between the index column and the bars, and the spacing between the bars and the matrix entries are narrow. At the very least, I would like to achieve these two results.

In addition, I would like to be able to create a matrix exactly like in Ex1:

  1. The top indices are right-aligned with the matrix entries.
  2. The double vertical bars are slightly thicker than the regular bar (although that might just be the font).
  3. The double vertical bars are just long enough to reach the top entries and the bottom entries and are not longer.

If I can achieve these results too, that would be greatly appreciated!

Here is what I have so far. I used the blkarray package.
There is too much spacing (black) and the bars are too far apart (red).
Thanks for the help!
My result

\mathbf{P} = 
        \begin{blockarray}{cccc}
         & 0   & 1   & 2 \\
        \begin{block}{r||rrr||}
        0 & 0.7 & 0.2 & 0.1 \\
        1 & 0.3 & 0.5 & 0.2 \\
        2 & 0   & 0   & 0   \\
        \end{block}
    \end{blockarray}

Best Answer

You can use @{} specifications. Or see alternate approach that limits vertical line size.

\documentclass{article}
\usepackage{amsmath,blkarray}
\begin{document}
\[
\mathbf{P} = 
        \begin{blockarray}{c@{\hspace{1pt}}rrr@{\hspace{3pt}}}
         & 0   & 1   & 2 \\
        \begin{block}{r@{\hspace{1pt}}|@{\hspace{1pt}}
    |@{\hspace{1pt}}rrr@{\hspace{1pt}}|@{\hspace{1pt}}|}
        0 & 0.7 & 0.2 & 0.1 \\
        1 & 0.3 & 0.5 & 0.2 \\
        2 & 0   & 0   & 0   \\
        \end{block}
    \end{blockarray}
\]
\end{document}

enter image description here


ALTERNATE APPROACH:

This uses my tabstackengine and scalerel packages to achieve the OP's goal of limiting the vertical line height. I had to patch \Centerstack to omit the automatic \strut insertion.

\documentclass{article}
\usepackage{scalerel,tabstackengine,xpatch}
\setstacktabbedgap{1em}
\xpatchcmd\Centerstack{\strutlongstacks{T}}{}{}{}
\begin{document}
\[
\def\stackalignment{r}
\mathbf{P} = 
       \Centerstack{
        0  \\
        1  \\
        2    
        }\!
        \stackon{
        \stretchleftright{|\!|}{\tabbedCenterstack{
         0.7 & 0.2 & 0.1 \\
        0.3 & 0.5 & 0.2 \\
         0   & 0   & 0   
        }}{|\!|}}{\tabbedCenterstack{
        \protect\phantom{0.}0 & \protect\phantom{0.}1 & \protect\phantom{0.}2}
        \kern1pt}
\]
\end{document}

enter image description here