# Align diagonals of a labelled matrix with diagonal dots

I have a labelled matrix generated through the blockarray package.

\documentclass{article}
\usepackage{blkarray}
\usepackage{amsmath}
\begin{document}
$$\bm{\tilde{T}}_{(2,1)} = \begin{blockarray}{cccccccc} 1 & 2 & & N-1 & N & N+1 & & N(N+1)\\ \begin{block}{(cccccccc)} -1 & 0 & \cdots & 0 & 1 & 0 & \cdots & 0\\ 0 & -1 & 0 & \cdots & 0 & 1 & \ddots & \vdots\\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & 0\\ 0 & \dots & 0 & -1 & 0 & \cdots & 0 & 1\\ \end{block} \end{blockarray}$$
\end{document}


As you can see, there is no alignment between the diagonals of the matrix and the diagonal dots, because of the changing width of the columns, in turn caused by the labels.

How can I get a nice diagonal alignment, so that it is clear for the reader what elements the diagonal dots are connecting?

## 3 Answers

I propose this solution, with smaller labels and a width equal to the width of –1:

\documentclass{article}
\usepackage{blkarray}
\usepackage{amsmath, bm, amssymb}
\usepackage{makebox, bigstrut, relsize}

\begin{document}

$$\setlength{\arraycolsep}{3pt} \bm{\widetilde{T}}_{(2,1)} = \begin{blockarray}{*{8}{c}} \mathsmaller{1} & \mathsmaller{2} & & \makebox*{-1}{\mathsmaller{N-1}} & \makebox*{-1}{\mathsmaller{N}} & \makebox*{-1}{\mathsmaller{N+1}} & & \makebox*{-1}{\mathsmaller{N(N+1)}}\\ \begin{block}{(cccccccc)} -1 & 0 & \cdots & 0 & 1 & 0 & \cdots & 0 \bigstrut \\ 0 & -1 & 0 & \cdots & 0 & 1 & \ddots & \vdots\\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & 0 \bigstrut[b]\\ 0 & \dots & 0 & -1 & 0 & \cdots & 0 & 1\bigstrut \\ \end{block} \end{blockarray}$$

\end{document}


• Thanks, it looks good! May I ask you to check whether I understand what you did? So, you increase the column separation to 3pt, then you employ \mathsmaller to reduce the size of the label and \makebox to set a width equal to the width of -1 for long labels. However I do not understand the use of \bigstrut. Why do you have \bigstrut for first and last row, \bigstrut[b] for the third row and nothing for the second row? – Francesco Jan 23 at 16:25
• I added \bigstrut for two reasons: first, the delimiters in blockarray are often a bit too short. \bigstrut adds \bigstrutjot (defaults to 2pt) at the top and bottom of the cellin which it is found, so I obtain sensible delimiters. The second reason is to adjust the vertical spacing between rows, so as to have a better alignment of the diagonals. The [b] option is to add \bigstrutjot only at the bottom of the cell in that particular row. All this was decided by trial and error, I must say. – Bernard Jan 23 at 17:58

My another proposal is this MWE with the option nicematrix.

\documentclass{article}
\usepackage{mathtools,nicematrix,bm}
\begin{document}
$\bm{\widetilde{T}}_{(2,1)} =\begin{pNiceArrayRC}{CCCCCCCC}[columns-width = .5cm] {\scriptstyle1}& {\scriptstyle2} & & {\scriptstyle N-1} & {\scriptstyle N} & {\scriptstyle N+1} & & {\scriptstyle N(N+1)}\\ -1 & 0 & \cdots & 0 & 1 & 0 & \cdots & 0 \\ 0 & -1 & 0 & \cdots & 0 & 1 & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & 0 \\ 0 & \dots & 0 & -1 & 0 & \cdots & 0 & 1& \\ \end{pNiceArrayRC}$
\end{document}


as supplement to interesting @Sebastiano answer (+1):

\documentclass{article}
\usepackage{bm}
\usepackage{amsmath}    % for \dotsm
\usepackage{nicematrix}

\begin{document}
$\NiceMatrixOptions{code-for-first-row = \scriptstyle\color{blue}} \bm{\widetilde{T}}_{(2,1)} = \begin{pNiceArrayRC}{>{\rule[-0.2ex]{0pt}{2.4ex}}CCCCCCCC} 1 & 2 & & N-1 & N & N+1 & & N(N+1) \\ -1 & 0 & \dotsm & 0 & 1 & 0 & \cdots & 0 \\ 0 & -1 & 0 & \dotsm & 0 & 1 & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & \ddots & \ddots & 0 \\ 0 & \dotsm & 0 & -1 & 0 & \dotsm & 0 & 1 \\ \end{pNiceArrayRC}$
\end{document}


• Thank you very much to declarate my name :-) – Sebastiano May 3 at 11:23