# Centering text over matrix columns

How can I modify the code below to get the text centered over the matrix columns with 1s instead of aligned to the left edge of the columns.

Below is the MWE code and output.

\documentclass{article}

\usepackage{amsmath}

% command for overbrace in matrix
\newcommand\bovermat[2]{%
\makebox[0pt][l]{$\smash{\overbrace{\phantom{% \begin{matrix}#2\end{matrix}}}^{#1}}$}#2}

\begin{document}

$$\left[ {\begin{array}{ccccccccc} 0 & \cdots & 0 & \bovermat{\text{columns }q_1,\ldots,q_\ell}{1 & \cdots & 1} & 0 & \cdots & 0\\ 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0\\ \vdots & & \vdots & \vdots & & \vdots & & & \vdots \\ 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0\\ \end{array} } \right]$$

\end{document}


• Try to remove the & that precedes the \bovermat and put it inside the \bovermat. Like this: \bovermat{\text{columns }q_1,\ldots,q_\ell}{& 1 & \cdots & 1}. – Phelype Oleinik Jan 19 '18 at 18:37

You can also use blkarray, it has more flexibility.

\documentclass{article}
\usepackage{blkarray}
\usepackage{amsmath}

\begin{document}

$\begin{blockarray}{ccccccccc} & & & & \makebox[0pt]{\overbrace{\rule{4em}{0pt}}^{\text{columns }q_1,\ldots,q_\ell}} & & & & \\ \begin{block}{[ccccccccc]} 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0 \\ 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0 \\ \vdots & & \vdots & \vdots & & \vdots & & & \vdots \\ 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0 \\ \end{block} \end{blockarray}$

\end{document}


As suggested in the comments section by Phelype Oleinik, the solution is to remove the & that precedes the \bovermat and to put it inside the second argument to \bovermat.

\documentclass{article}

\usepackage{amsmath}

% command for overbrace in matrix
\newcommand\bovermat[2]{%
\makebox[0pt][l]{$\smash{\overbrace{\phantom{% \begin{matrix}#2\end{matrix}}}^{#1}}$}#2}

\begin{document}

$$\left[ {\begin{array}{ccccccccc} 0 & \cdots & 0 \bovermat{ \text{columns }q_1,\ldots,q_\ell}{& 1 & \cdots & 1} & 0 & \cdots & 0\\ 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0\\ \vdots & & \vdots & \vdots & & \vdots & & & \vdots \\ 0 & \cdots & 0 & 1 & \cdots & 1 & 0 & \cdots & 0\\ \end{array} } \right]$$

\end{document}