3

So I am writing a transition matrix for markov chains using the gauss package. Here's what I have:

Code:

\documentclass[12pt]{article}
\usepackage{float}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{gauss}

\begin{document} 
    \begin{align*}
        T\left(s, a_1, s^\prime\right) &= 
            \begin{bmatrix}
            0 & 0.5 & 0.5 & 0 & 0\\
            0 & 0 & 0 & 0.5 & 0.5\\
            0 & 0 & 0 & 0.9 & 0.1 \\
            0 & 0 & 0 & 1 & 0\\
            0 & 0 & 0 & 0 & 1
        \end{bmatrix}\\
        T\left(s, a_1, s^\prime\right) &= 
        \begin{gmatrix}[b]
            \mathllap{S_1\quad} 0 & 0.5 & 0.5 & 0 & 0\\
            \mathllap{S_2\quad} 0 & 0 & 0 & 0.5 & 0.5\\
            \mathllap{S_3\quad} 0 & 0 & 0 & 0.9 & 0.1 \\
            \mathllap{S_4\quad} 0 & 0 & 0 & 1 & 0\\
            \mathllap{S_5\quad} 0 & 0 & 0 & 0 & 1\colops
            \def\colmultlabel#1{\makebox[1.2em]{$#1$}}
            \mult0{S_1}
            \mult1{S_2}
            \mult2{S_3}
            \mult3{S_4}
            \mult4{S_5}
        \end{gmatrix}\\
        T\left(s, a_1, s^\prime\right) &= 
        \begin{bmatrix}
            0 & 0.5 & 0.5 & 0 & 0\\
            0 & 0 & 0 & 0.5 & 0.5\\
            0 & 0 & 0 & 0.9 & 0.1 \\
            0 & 0 & 0 & 1 & 0\\
            0 & 0 & 0 & 0 & 1
        \end{bmatrix}
    \end{align*}
\end{document}

Output:

enter image description here

I realized that the gmatrix outputs a normal matrix placed like a normal matrix from amsmath, but the problem is that the labels do not get factored in. That is, the row and column labels do not create space for it to be separated from the equals sign on its line or the equation above.

How to fix it?

3
  • Is there a reason for writing ^\prime when ' creates the identical output with much less input effort?
    – Mico
    Commented May 30, 2020 at 14:30
  • From my memory, writing \prime gave me a bigger sign, and it wasn’t positioned correctly.
    – Superman
    Commented May 30, 2020 at 16:29
  • 1
    I did not ask you why you don't write \prime in the document. I asked you why you would input ^\prime -- which is definitely not the same as \prime -- given that ' creates the identical output but does so with much less input effort.
    – Mico
    Commented May 30, 2020 at 16:36

1 Answer 1

4

You could just insert some horizontal space.

\documentclass[12pt]{article}
\usepackage{float}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{gauss}

\begin{document} 
    \begin{align*}
        T\left(s, a_1, s^\prime\right) &= 
            \begin{bmatrix}
            0 & 0.5 & 0.5 & 0 & 0\\
            0 & 0 & 0 & 0.5 & 0.5\\
            0 & 0 & 0 & 0.9 & 0.1 \\
            0 & 0 & 0 & 1 & 0\\
            0 & 0 & 0 & 0 & 1
        \end{bmatrix}\\
        T\left(s, a_1, s^\prime\right) &=\;\quad
        \begin{gmatrix}[b]
            \mathllap{S_1\quad} 0 & 0.5 & 0.5 & 0 & 0\\
            \mathllap{S_2\quad} 0 & 0 & 0 & 0.5 & 0.5\\
            \mathllap{S_3\quad} 0 & 0 & 0 & 0.9 & 0.1 \\
            \mathllap{S_4\quad} 0 & 0 & 0 & 1 & 0\\
            \mathllap{S_5\quad} 0 & 0 & 0 & 0 & 1\colops
            \def\colmultlabel#1{\makebox[1.2em]{$#1$}}
            \mult0{S_1}
            \mult1{S_2}
            \mult2{S_3}
            \mult3{S_4}
            \mult4{S_5}
        \end{gmatrix}\\
        T\left(s, a_1, s^\prime\right) &= 
        \begin{bmatrix}
            0 & 0.5 & 0.5 & 0 & 0\\
            0 & 0 & 0 & 0.5 & 0.5\\
            0 & 0 & 0 & 0.9 & 0.1 \\
            0 & 0 & 0 & 1 & 0\\
            0 & 0 & 0 & 0 & 1
        \end{bmatrix}
    \end{align*}
\end{document}

enter image description here

However, if you want this output I recommend nicematrix, with which you can add the S_i automatically.

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}
\usepackage{nicematrix}

\begin{document} 
    \begin{align*}
        T\left(s, a_1, s^\prime\right) &= 
            \begin{bmatrix}
            0 & 0.5 & 0.5 & 0 & 0\\
            0 & 0 & 0 & 0.5 & 0.5\\
            0 & 0 & 0 & 0.9 & 0.1 \\
            0 & 0 & 0 & 1 & 0\\
            0 & 0 & 0 & 0 & 1
        \end{bmatrix}\\
        T\left(s, a_1, s^\prime\right) &=
        \begin{bNiceMatrix}[first-row,first-col,
    code-for-first-row = S_{\number\value{jCol}},
    code-for-first-col = S_{\number\value{iRow}}
    ]
            & & & & &  \\ 
            & 0 & 0.5 & 0.5 & 0 & 0\\
            & 0 & 0 & 0 & 0.5 & 0.5\\
            & 0 & 0 & 0 & 0.9 & 0.1 \\
            & 0 & 0 & 0 & 1 & 0\\
            & 0 & 0 & 0 & 0 & 1
        \end{bNiceMatrix}\\
        T\left(s, a_1, s^\prime\right) &= 
        \begin{bmatrix}
            0 & 0.5 & 0.5 & 0 & 0\\
            0 & 0 & 0 & 0.5 & 0.5\\
            0 & 0 & 0 & 0.9 & 0.1 \\
            0 & 0 & 0 & 1 & 0\\
            0 & 0 & 0 & 0 & 1
        \end{bmatrix}
    \end{align*}
\end{document}

enter image description here

8
  • Your spacing command really does a good job of properly spacing it out! Thank you so much!
    – Superman
    Commented May 30, 2020 at 3:58
  • I was wondering if there is a “general” command that can space out as properly as if I wrote a bmatrix instead of a makeshift command like what you wrote.
    – Superman
    Commented May 30, 2020 at 3:59
  • 1
    @Superman Naively it should be \hphantom{S_1\quad} but this does not yield an appealing result. I added a second possibility with which you do not have to worry about any of this.
    – user194703
    Commented May 30, 2020 at 4:03
  • 2
    @Superman These are the cells that are outside of the matrix. That is, first-row,first-col, indicates that the first row and columns are outside of the matrix. They get automatically filled with S_{\number\value{jCol}} and S_{\number\value{iRowl}}, respectively, due to code-for-first-row and code-for-first-col
    – user194703
    Commented May 30, 2020 at 4:19
  • 2
    @Superman Yes, just drop code-for-first-row = S_{\number\value{jCol}}, code-for-first-col = S_{\number\value{iRow}} and put the letters in the empty cells.
    – user194703
    Commented May 30, 2020 at 4:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .