6

I am trying to create a bracket enclosed matrix where the rows and columns are labeled. My attempt is:

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

However when I do this I get many errors that "LaTeX Error: \mathcal allowed only in math mode." and "Missing $ inserted.". I have both the array and amsmath packages included in my preamble. Why doesn't this work and what would be the best way to accomplish this? Adding $$ causes the error "unexpected $ after \begin{array}"

1
  • Welcome to TeX.SE.
    – Mico
    Apr 16 at 20:30

2 Answers 2

5

The contents of a p column are processed in text mode by default. Since automatic line breaking doesn't appear to be required, I suggest you replace the p column type with the w column type, which is provided by the array package and also takes a length parameter (the column's design width) as its argument.

enter image description here

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

I suggest to use 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}

With w{c}{2em} you get fixed width columns.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .