
答案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
使用其列類型S
array
- 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}