Block matrix with lines inside border matrix

bordermatrixmatricesnicematrix

I'm wondering if anyone can help me typeset this border matrix with lines inside it. Dashed or solid lines are okay but I would prefer the lines to not extend past the brackets. I would also prefer to not need to load 3+ packages to get this to happen.

I have tried several other solutions I've found on StackExchange but they don't seem to work (I don't have kbordermatrix and nicematrix doesn't draw lines for some reason).

Any advice is appreciated.

illustration

EDIT: Here's my current code:

\bbordermatrix{
              & {\bf 1}        & {\bf 5}        & {\bf 2}     & {\bf 4}        & {\bf 3} \cr
    {\bf 1}\, & \nicefrac16    & \nicefrac56    & 0           & 0              & 0 \cr
    {\bf 5}\, & \nicefrac16    & \nicefrac56    & 0           & 0              & 0 \cr
    {\bf 2}   & 0              & 0              & \nicefrac23 & \nicefrac13    & 0 \cr
    {\bf 4}   & 0              & 0              & \nicefrac23 & \nicefrac13    & 0 \cr
    {\bf 3}   & \nicefrac1{36} & \nicefrac5{36} & \nicefrac59 & \nicefrac5{18} & 0
    }

I've started to troubleshoot what's going wrong with NiceMatrix. I have a header file that I include as \include{…}. Something in that header file seems to make this matrix (source: bordermatrix with blocks):

nicegood

come out like this:

nicebad

The problem is, I went through and commented out every line of code in that header and nothing seems to specifically be the problem. In fact, if I don't include the header but just copy in every line, when I typeset the document the matrix first comes out wrong, but the second time I typeset it comes out right. I've never seen this sort of behavior before… how can I get different results?

Then if I remove the lines of the header file and replace them with \include, I get the reverse behavior: the first time it will typeset correctly, and all subsequent times it will typeset incorrectly.

I have no idea what's going on.

Best Answer

Here is a solution using nicematrix and two custom rules.

b

\documentclass[12pt,a4paper]{article}

\usepackage{xfrac} % nice fractions

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
    
\NiceMatrixOptions
{
    custom-line ={command= H, tikz= dashed, width= 1mm}, % horizontal, dashed
    custom-line = {letter= I, tikz= dashed, width= 1mm}, % vertical, dashed
}
        
$\begin{bNiceArray}{cc I cc I c}[first-row, first-col, cell-space-top-limit=3pt, extra-margin=4pt]
        &1              &5              &2              &4              &3\\
    1   &\sfrac{1}{6}   &\sfrac{5}{6}   &0              &0              &0\\
    5   &\sfrac{1}{6}   &\sfrac{5}{6}   &0              &0              &0\\ \H % custom rule <<<<
    2   &0              &0              &\sfrac{2}{3}   &\sfrac{1}{3}   &0\\
    4   &0              &0              &\sfrac{2}{3}   &\sfrac{1}{3}   &0\\ \H % custom rule <<<<
    3   &\sfrac{1}{36}  &\sfrac{5}{36}  &\sfrac{5}{9}   &\sfrac{5}{18}  &0\\    
\end{bNiceArray}$
    
\end{document}

Used nicematrix.sty 2022/05/08 v6.8a

Update After follow-up question: bold face for the first column and the first row of the array. Use the keys

code-for-first-row and code-for-first-col

c

\documentclass[12pt,a4paper]{article}

\usepackage{xfrac} % nice fractions

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
    
\NiceMatrixOptions
{
    custom-line ={command= H, tikz= dashed, width= 1mm}, % horizontal, dashed
    custom-line = {letter= I, tikz= dashed, width= 1mm}, % vertical dashed  
}
        
$\begin{bNiceArray}{cc I cc I c}[first-row, first-col, cell-space-top-limit=3pt, extra-margin=4pt,  code-for-first-row = $\boldmath$, code-for-first-col = $\boldmath$]% changed <<<<<<<
        &1              &5              &2              &4              &3\\
    1   &\sfrac{1}{6}   &\sfrac{5}{6}   &0              &0              &0\\
    5   &\sfrac{1}{6}   &\sfrac{5}{6}   &0              &0              &0\\ \H % custom rule <<<<
    2   &0              &0              &\sfrac{2}{3}   &\sfrac{1}{3}   &0\\
    4   &0              &0              &\sfrac{2}{3}   &\sfrac{1}{3}   &0\\ \H % custom rule <<<<
    3   &\sfrac{1}{36}  &\sfrac{5}{36}  &\sfrac{5}{9}   &\sfrac{5}{18}  &0\\    
\end{bNiceArray}$
    
\end{document}