7

Im trying to write 2 matrices in Jupyter Notebook Markdown but I can't figure out how to make the 2 matrices to have the same size. I don't know if I can change the whole size or reduce the space between columns to make it look better.

enter image description here

This is my code:

$$ G_x = \begin{bmatrix} -1 & -2 & -1 \\  0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix}$$

$$ G_y\begin{bmatrix} -1 & 0 & 1 \ -2 & 0 & 2 \ -1 & 0 & 1 \end{bmatrix}$$

Bernard
  • 271,350

4 Answers4

6

The package nicematrix has a feature dedicated to that problem.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceMatrixBlock}[auto-columns-width] First matrix [G_x = \begin{bNiceMatrix} -1 & -2 & -1 \ 0 & 0 & 0 \ 1 & 2 & 1 \end{bNiceMatrix}] and second matrix [G_y = \begin{bNiceMatrix} -1 & 0 & 1 \ -2 & 0 & 2 \ -1 & 0 & 1 \end{bNiceMatrix}] \end{NiceMatrixBlock}

\end{document}

You need several compilations.

Output of the above code

F. Pantigny
  • 40,250
4

A possibility with blkarray:

\documentclass{article}
\usepackage{amsmath}
\usepackage{blkarray, bigstrut}

\begin{document}

[ \begin{blockarray}{@{}r{3}{r}} \begin{block}{@{}c@{\enspace}[{3}{r}]} & -1 & -2 & -1 \bigstrut[t] \ G_x = {}& 0 & 0 & 0 \ & 1 & 2 & 1 \ \end{block}\[-1ex] \begin{block}{@{}c@{\enspace}[*{3}{r}]} & -1 & 0 & 1 \bigstrut[t] \ G_y = {}& -2 & 0 & 2\ & -1 & 0 & 1 \ \end{block} \end{blockarray} ]

\end{document}

enter image description here

Bernard
  • 271,350
4

Here are two additional solutions:

  • use \phantom{-} directives to insert a more space, and use a bmatrix* environment rather than a bmatrix environment

    • pro: easy to use
    • con: need to insert 1 \phantom directive for each columns whose width needs fine-tuning
  • load the siunitx package and use its S column type in a a bespoke array environment

    • pro: no more fine-tuning required
    • con: for easy-and-straightforward application, column types must be fairly simple

enter image description here

\documentclass{article}
\usepackage{mathtools} % for 'bmatrix*' environment

\usepackage{siunitx} % for 'S' column type \usepackage{array} % for '\newcolumntype' macro \newcolumntype{T}{S[table-format=-1.0]} % <-- bespoke version of 'S' column type

\begin{document} \begin{align} G_0 &= \begin{bmatrix}[r] -1 & -2 & -1 \ 0 & 0 & 0 \ 1 & 2 & 1 \end{bmatrix} \ G_1 &= \begin{bmatrix}[r] % <-- note use of 'bmatrix' env. -1 & \phantom{-}0 & \phantom{-}1 \ -2 & 0 & 2 \ -1 & 0 & 1 \end{bmatrix} \ G_2 &= \left[ \begin{array}{@{} TTT @{}} % <-- use the 'T' column type for all columns -1 & 0 & 1 \ -2 & 0 & 2 \ -1 & 0 & 1 % <-- note: no fine-tuning needed \end{array}\right] \end{align*} \end{document}

Mico
  • 506,678
2

With tabularray:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\NewDocumentEnvironment{mymatrix}{+b}{
    \begin{+bmatrix}[columns={1.3em, r, colsep=2pt}]
    #1
    \end{+bmatrix}
    }{}

\begin{document} [ G_x = \begin{mymatrix} -1 & -2 & -1 \ 0 & 0 & 0 \ 1 & 2 & 1 \end{mymatrix} ] [ G_y = \begin{mymatrix} -1 & 0 & 1 \ -2 & 0 & 2 \ -1 & 0 & 1 \end{mymatrix} ] \end{document}

enter image description here

CarLaTeX
  • 62,716