# How to group and label rows and columns in a matrix

I am trying to reproduce

but I am not sure where to start. The basic matrix with the extra spaces between columns is

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\setcounter{MaxMatrixCols}{20}
\begin{document}

\section{}
$\begin{pmatrix} 1 & & 0 & & 0 & & 1 & 1 & & 1 & 1 & & 0 & 0 & & 1 & 1 & 1\\ 0 & & 1 & & 0 & & 1 & 0 & & 0 & 0 & & 1 & 1 & & 1 & 1 & 0\\ 0 & & 0 & & 1 & & 0 & 0 & & 1 & 0 & & 1 & 0 & & 1 & 0 & 0\\ 1 & & 1 & & 0 & & 0 & 0 & & 1 & 1 & & 1 & 1 & & 0 & 0 & 0\\ 1 & & 0 & & 1 & & 1 & 1 & & 0 & 0 & & 1 & 0 & & 0 & 0 & 0\\ 0 & & 1 & & 1 & & 1 & 0 & & 1 & 0 & & 0 & 0 & & 0 & 0 & 0\\ 1 & & 1 & & 1 & & 0 & 0 & & 0 & 0 & & 0 & 0 & & 1 & 0 & 0 \end{pmatrix}$
\end{document}


How can I add the row and column labels? Should I be using a table instead maybe?

• Using empty columns between the groups, perhaps?. Don't use $$...$$. Use $....$ instead
– user31729
Commented Oct 12, 2015 at 12:03
• @ChristianHupfer Thank you. I applied those changes to the question.
– Simd
Commented Oct 12, 2015 at 12:08

Use fixed width columns:

\documentclass[a4paper,10pt]{article}

\usepackage{amsmath,array}

\begin{document}

\begin{equation*}
\begin{array}{
@{}% no space at left
r% right aligned
@{}
*{7}{>{\centering\arraybackslash$}p{3.5em}<{$}@{}}% width 3em centered
}
&\{1\}&\{2\}&\{3\}&\{1,2\}&\{1,3\}&\{2,3\}&\{1,2,3\} \\
\{1\}     & 1 & 0 & 0 & 1 \ 1 & 1 \ 1 & 0 \ 0 & 1 \ 1 \ 1\\
\{2\}     & 0 & 1 & 0 & 1 \ 0 & 0 \ 0 & 1 \ 1 & 1 \ 1 \ 0\\
\{3\}     & 0 & 0 & 1 & 0 \ 0 & 1 \ 0 & 1 \ 0 & 1 \ 0 \ 0\\
\{1,2\}   & 1 & 1 & 0 & 0 \ 0 & 1 \ 1 & 1 \ 1 & 0 \ 0 \ 0\\
\{1,3\}   & 1 & 0 & 1 & 1 \ 1 & 0 \ 0 & 1 \ 0 & 0 \ 0 \ 0\\
\{2,3\}   & 0 & 1 & 1 & 1 \ 0 & 1 \ 0 & 0 \ 0 & 0 \ 0 \ 0\\
\{1,2,3\} & 1 & 1 & 1 & 0 \ 0 & 0 \ 0 & 0 \ 0 & 1 \ 0 \ 0
\end{array}
\end{equation*}

\end{document}


Don't use . Never, in LaTeX.

Alternative version:

\documentclass[a4paper,10pt]{article}

\usepackage{amsmath}

\begin{document}

\begin{equation*}
\begin{array}{r*{7}{c}}
&\{1\}&\{2\}&\{3\}&\{1,2\}&\{1,3\}&\{2,3\}&\{1,2,3\} \\
\{1\}     & 1 & 0 & 0 & 1 \ 1 & 1 \ 1 & 0 \ 0 & 1 \ 1 \ 1\\
\{2\}     & 0 & 1 & 0 & 1 \ 0 & 0 \ 0 & 1 \ 1 & 1 \ 1 \ 0\\
\{3\}     & 0 & 0 & 1 & 0 \ 0 & 1 \ 0 & 1 \ 0 & 1 \ 0 \ 0\\
\{1,2\}   & 1 & 1 & 0 & 0 \ 0 & 1 \ 1 & 1 \ 1 & 0 \ 0 \ 0\\
\{1,3\}   & 1 & 0 & 1 & 1 \ 1 & 0 \ 0 & 1 \ 0 & 0 \ 0 \ 0\\
\{2,3\}   & 0 & 1 & 1 & 1 \ 0 & 1 \ 0 & 0 \ 0 & 0 \ 0 \ 0\\
\{1,2,3\} & 1 & 1 & 1 & 0 \ 0 & 0 \ 0 & 0 \ 0 & 1 \ 0 \ 0
\end{array}
\end{equation*}

\end{document}


• @Lembik I added another version that's closer to the original. Commented Oct 12, 2015 at 12:34

just wanted to let you know of tabu (https://www.ctan.org/pkg/tabu?), one of the most versatile table packages available for tex. Especially the X column types are handy. The syntax is very intuitive X[relative_scale,v_align,h_align].

The to \linewidth automatically adjust the table to fit the page (any size can be used here..)

A MWE using tabu is given below:

\begin{table}%[![enter image description here][1]][1]
\centering
% table caption is above the table
\caption{Test for http://tex.stackexchange.com}
\label{tab:test}% Give a unique label
\begin{tabu} to \linewidth {X[r] *7{X[c]}}

& \{1\} & \{2\} & \{3\} & \{1,2\}&\{1,3\}&\{2,3\}&\{1,2,3\}\\
%\firsthline
\{1\}     &  1    &  0    &  0    & 1~1 & 1~1 & 0~0 & 1~1~1\\
\{2\}     &  0    &  1    &  0    & 1~0 & 0~0 & 1~1 & 1~1~0\\
\{3\}     &  0    &  0    &  1    & 0~0 & 1~0 & 1~0 & 1~0~0\\
\{1,2\}   &  1    &  1    &  0    & 0~0 & 1~1 & 1~1 & 0~0~0\\
\{1,3\}   &  1    &  0    &  1    & 1~1 & 0~0 & 1~0 & 0~0~0\\
\{2,3\}   &  0    &  1    &  1    & 1~0 & 1~0 & 0~0 & 0~0~0\\
\{1,2,3\} &  1    &  1    &  1    & 0~0 & 0~0 & 0~0 & 1~0~0\\
%\lasthline
\end{tabu}

\end{table}