Como alterar o espaço entre colunas na matriz

Como alterar o espaço entre colunas na matriz

Estou tentando escrever 2 matrizes no Jupyter Notebook Markdown, mas não consigo descobrir como fazer com que as 2 matrizes tenham o mesmo tamanho. Não sei se posso alterar todo o tamanho ou reduzir o espaço entre as colunas para melhorar a aparência.

insira a descrição da imagem aqui

Este é o meu código:

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

Responder1

O pacote nicematrixpossui um recurso dedicado a esse problema.

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

Você precisa de várias compilações.

Saída do código acima

Responder2

Uma possibilidade com 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} 

insira a descrição da imagem aqui

Responder3

Aqui estão duas soluções adicionais:

  • use \phantom{-}diretivas para inserir mais espaço e use um bmatrix*ambiente em vez de um bmatrixambiente

    • pró: fácil de usar
    • con: é necessário inserir 1 \phantomdiretiva para cada coluna cuja largura precisa de ajuste fino
  • carregue o siunitxpacote e use seu Stipo de coluna em um arrayambiente personalizado

    • profissional: não é necessário mais ajuste fino
    • contra: para uma aplicação fácil e direta, os tipos de coluna devem ser bastante simples

insira a descrição da imagem aqui

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

Responder4

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

insira a descrição da imagem aqui

informação relacionada