
答え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
追加の解決策が 2 つあります。
ディレクティブを使用して
\phantom{-}
スペースを挿入し、環境bmatrix*
ではなく環境を使用します。bmatrix
- 長所: 使いやすい
\phantom
欠点:幅を微調整する必要がある列ごとに1つのディレクティブを挿入する必要がある
パッケージをロードし、カスタム環境で
siunitx
その列タイプを使用するS
array
- 利点: 微調整が不要
- 欠点: 簡単でわかりやすいアプリケーションの場合、列タイプはかなり単純でなければなりません
\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}