マトリックスの列間のスペースを変更する方法

マトリックスの列間のスペースを変更する方法

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

追加の解決策が 2 つあります。

  • ディレクティブを使用して\phantom{-}スペースを挿入し、環境bmatrix*ではなく環境を使用します。bmatrix

    • 長所: 使いやすい
    • \phantom欠点:幅を微調整する必要がある列ごとに1つのディレクティブを挿入する必要がある
  • パッケージをロードし、カスタム環境でsiunitxその列タイプを使用するSarray

    • 利点: 微調整が不要
    • 欠点: 簡単でわかりやすいアプリケーションの場合、列タイプはかなり単純でなければなりません

ここに画像の説明を入力してください

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

ここに画像の説明を入力してください

関連情報