
行と列にラベルが付けられた括弧で囲まれた行列を作成しようとしています。私の試みは次のとおりです。
\begin{equation}
\begin{aligned}
&\begin{array}{*{7}{p{.8cm}}}
\mathcal{G}^{\{1\}} & \mathcal{G}^{\{2\}} & \mathcal{G}^{\{3\}} & \mathcal{G}^{\{4\}} & \mathcal{G}^{\{5\}} & e_{r1} & e_{r2}\end{array}\\
\begin{array}{r}
\text{plant (out)}\\
n_1\\
n_2\\
\text{plant (in)}\end{array} & \left[\begin{array}{*{7}{p{.8cm}}}
0 & 0 & -1 & -1 & -1 & -1 & 0\\
0 & 0 & -1 & -1 & -1 & -1 & 0\\
0 & 0 & -1 & -1 & -1 & -1 & 0\\
0 & 0 & -1 & -1 & -1 & -1 & 0\end{array}\right]
\end{aligned}
\end{equation}
しかし、これを行うと、「LaTeX エラー: \mathcal は数式モードでのみ使用できます。」や「$ が挿入されていません。」というエラーが多数発生します。私は、配列パッケージと amsmath パッケージの両方をプリアンブルに含めています。なぜこれが機能しないのでしょうか。また、これを実現する最善の方法は何でしょうか。$$ を追加すると、「\begin{array} の後に予期しない $ があります」というエラーが発生します。
答え1
列の内容は、p
デフォルトでテキスト モードで処理されます。自動改行は必要ないようですので、列p
タイプをw
、パッケージによって提供されarray
、長さパラメータ (列のデザイン幅) も引数として受け取る列タイプに置き換えることをお勧めします。
\documentclass{article}
\usepackage{array} % for 'w' column type
\usepackage{amsmath} % for 'aligned' env.
\newcommand\mG[1]{\mathcal{G}^{\{#1\}}} % handy shortcut macro
\begin{document}
\begin{equation}
\begin{aligned}
&\kern4pt
\begin{array}{ *{6}{wl{8mm}} r }
\mG{1} & \mG{2} & \mG{3} & \mG{4} & \mG{5} & e_{r1} & e_{r2}
\end{array} \\
\begin{array}{r}
\text{plant (out)}\\
n_1\\
n_2\\
\text{plant (in)}
\end{array}
&\left[
\begin{array}{ *{6}{wl{8mm}} r }
0 & 0 & -1 & -1 & -1 & -1 & 0\\
0 & 0 & -1 & -1 & -1 & -1 & 0\\
0 & 0 & -1 & -1 & -1 & -1 & 0\\
0 & 0 & -1 & -1 & -1 & -1 & 0
\end{array}
\right]
\end{aligned}
\end{equation}
\end{document}
答え2
を使用することをお勧めしますnicematrix
。
\documentclass{article}
\usepackage{amsmath} % for 'aligned' env.
\usepackage{nicematrix}
\newcommand\cG[1]{\mathcal{G}}
\begin{document}
\begin{equation}
\begin{bNiceArray}[first-row,first-col]{*{7}{w{c}{2em}}}
& \cG^{\{1\}} & \cG^{\{2\}} & \cG&{\{3\}} & \cG^{\{4\}} & \cG^{\{5\}} & e_{r1} & e_{r2} \\
\text{plant (out)} & 0 & 0 & -1 & -1 & -1 & -1 & 0 \\
n_1 & 0 & 0 & -1 & -1 & -1 & -1 & 0 \\
n_2 & 0 & 0 & -1 & -1 & -1 & -1 & 0 \\
\text{plant (in)} & 0 & 0 & -1 & -1 & -1 & -1 & 0
\end{bNiceArray}
\end{equation}
\end{document}
w{c}{2em}
固定幅の列が得られます。