我在方程中有一個數組並對齊,但無法識別數學模式

我在方程中有一個數組並對齊,但無法識別數學模式

我正在嘗試創建一個括號括起來的矩陣,其中標記了行和列。我的嘗試是:

\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 僅允許在數學模式下。”和“缺少$插入。”。我的序言中包含了 array 和 amsmath 軟體包。為什麼這不起作用?新增 $$ 會導致錯誤“unexpected $ after \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}你就得到了固定寬度的列。

在此輸入影像描述

相關內容