7

I am trying to write a matrix with a bit of text sidewards and above it in order to explain its meaning. At the moment, this is my code:

\begin{equation}
 \begin{turn}{90}
 \mbox{\# rows}
 \end{turn}
\stackrel{\mbox{\# columns}}{
\begin{pmatrix}
  1 & 1 & 1 & 0 & 0\\
  0 & 0 & 1 & 0 & 1\\
  0 & 0 & 0 & 0 & 0\\
  1 & 1 & 1 & 0 & 1\\
  1 & 1 & 0 & 0 & 1\\
 \end{pmatrix}
 }
\end{equation}

And this is the result:

enter image description here

However, the text on the left side of the matrix is not correctly centered (it is too high). How to center the text around the matrix correctly?

1

3 Answers 3

10

It's easier with an array and \rotatebox:

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}

\begin{document}
\begin{equation}
\begin{array}{@{}c@{\hspace{1ex}}c@{}}
 & \text{\# columns} \\[1ex]
\rotatebox[origin=c]{90}{\text{\# rows}} &
\begin{pmatrix}
  1 & 1 & 1 & 0 & 0\\
  0 & 0 & 1 & 0 & 1\\
  0 & 0 & 0 & 0 & 0\\
  1 & 1 & 1 & 0 & 1\\
  1 & 1 & 0 & 0 & 1\\
 \end{pmatrix}
\end{array}
\end{equation}
\end{document}

enter image description here

2
  • why to use @{}? The c alignment is not enough to centre the array contents?
    – Sigur
    Feb 23, 2015 at 21:16
  • 1
    @Sigur The additional space on the sides may be unwanted depending on the context.
    – egreg
    Feb 23, 2015 at 21:18
2

Just because I like doing things with stacks...

The inter-column gap can be adjusted by changing the 2ex argument to \setstacktabbedgap. Vertical row separation is adjusted with the argument to \setstackgap{L}{length}.

\documentclass{article}
\usepackage{amsmath, graphicx, tabstackengine}
\stackMath
\begin{document}
\begin{equation}
\setstacktabbedgap{2ex}
\setstackgap{L}{1.2\baselineskip}
\rotatebox[origin=c]{90}{\text{\# rows}}
\stackon{%
\parenMatrixstack{
  1 & 1 & 1 & 0 & 0\\
  0 & 0 & 1 & 0 & 1\\
  0 & 0 & 0 & 0 & 0\\
  1 & 1 & 1 & 0 & 1\\
  1 & 1 & 0 & 0 & 1
}
}{\text{\# columns}}
\end{equation}
\end{document}

enter image description here

2

Here is what you can do with nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{equation*}
\begin{pNiceMatrix}[first-row,first-col]
& \Block{1-5}{\mbox{\# columns}}\\
\Block{5-1}<\rotate>{\mbox{\# rows}}
&  1 & 1 & 1 & 0 & 0\\
&  0 & 0 & 1 & 0 & 1\\
&  0 & 0 & 0 & 0 & 0\\
&  1 & 1 & 1 & 0 & 1\\
&  1 & 1 & 0 & 0 & 1\\
 \end{pNiceMatrix}
\end{equation*}

\end{document}

Output of the above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.