# Creating a transition probability matrix

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!

\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}


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}


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}


• This is great! If you could also adjust the length of the bars so that it does not extend past the top and bottom rows, that would be perfect! – David Sep 13 '15 at 2:08
• @maxtexing Using the current approach, I do not see how to shorten the bar length. But if I think of a completely alternate approach that achieves that, I will post it. – Steven B. Segletes Sep 13 '15 at 2:10
• Thank you so much. Where can I read more about the method you used, preferably a website? For example @{\hspace{1pt}? Also, it appears that if I want to include more columns in the matrix, I would just need to include more r columns and if I want more rows, just use more \\. Is this correct? – David Sep 13 '15 at 2:18
• @maxtexing The use of @{} specifications is covered in Lamport's LaTeX user guide (see description of array environment). However, I have added an alternate approach, using my stackengine and scalerel packages to make the lines flush with the numbers. I had to, in the preamble, redefine \Centerstack, to not automatically add a \strut to \Centerstacks. – Steven B. Segletes Sep 13 '15 at 2:31