如何更改矩陣中列之間的間距

如何更改矩陣中列之間的間距

我試圖在 Jupyter Notebook Markdown 中編寫 2 個矩陣,但我不知道如何讓這 2 個矩陣具有相同的大小。我不知道是否可以改變整體大小或減少列之間的空間以使其看起來更好。

在此輸入影像描述

這是我的程式碼:

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

答案1

該軟體包nicematrix有一個專門解決該問題的功能。

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

你需要幾個編譯。

上述程式碼的輸出

答案2

一種可能性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} 

在此輸入影像描述

答案3

這裡有兩個額外的解決方案:

  • 使用\phantom{-}指令插入更多空間,並使用bmatrix*環境而不是bmatrix環境

    • 優點:易於使用
    • \phantom缺點:需要為寬度需要微調的每列插入 1 個指令
  • 加載包並在自訂環境中siunitx使用其列類型Sarray

    • pro:不再需要微調
    • 缺點:對於簡單直接的應用程序,列類型必須相當簡單

在此輸入影像描述

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

答案4

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}

在此輸入影像描述

相關內容