# Properly aligned entries of a matrix using nicematrix

I have written two matrices, using pNiceMatrix package and using pmatrix. However, I am facing difficulties in getting them aligned properly, especially when negative entries are present in the matrices.

The first one, when I write using pmatrix is something like this

$$A =\begin{pmatrix} \p 1 & \p 0 & \p\cdots & \p\cdots & \p 0 \\ -1 & \p 1 & \p 0 & \p\cdots & \p 0\\ \p 0 & -1 &\p \ddots & \p \ddots & \p\vdots\\ \p \vdots & \p 0 & \p\ddots & \p\ddots & \p 0\\ \p \vdots & \p \vdots & \p\ddots & \p\ddots & \p 1\\ \p 0 & \p 0 & \p\cdots & \p 0 & -1 \end{pmatrix},$$


where \p is representative of \newcommand{\p}{\phantom{-}}. The output is something like this.

The same matrix, when written as

$$A =\begin{pNiceMatrix} 1 & 0 & \Cdots & & 0 \\ -1 & \Ddots & \Ddots & & \Vdots \\ 0 & \Ddots & & & \\ \Vdots & \Ddots & & & 0 \\ & & & & 1 \\ 0 & \Cdots & & 0 & -1 \end{pNiceMatrix},$$


results in a rather misaligned one.

How do I correct such misalignment? This really seems to create issues when I write long matrices, such as

$$\begin{pmatrix} B = w_{\ell,\ell+1}+w_{\ell,\ell-1} & -w_{\ell,\ell+1} & 0 & \cdots & 0 & -w_{\ell,\ell-1} \\ -w_{\ell,\ell-1} & \ddots & \ddots & \ddots & \ddots & 0 \\ 0 & \ddots & \ddots & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & \ddots & \ddots & 0\\ 0 & \ddots & \ddots & \ddots & \ddots & -w_{\ell,\ell-1} \\ -w_{\ell,\ell+1} & 0 & \cdots & 0 & -w_{\ell,\ell-1} & w_{\ell,\ell+1}+w_{\ell,\ell-1} \end{pmatrix}$$


You may use the option R (alias r) of {NiceMatrix}. I have added nullify-dots to have all the lines with the same height.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
$$A =\begin{pNiceMatrix}[R,nullify-dots] 1 & 0 & \Cdots & & 0 \\ -1 & \Ddots & \Ddots & & \Vdots \\ 0 & \Ddots & & & \\ \Vdots & \Ddots & & & 0 \\ & & & & 1 \\ 0 & \Cdots & & 0 & -1 \end{pNiceMatrix}$$
\end{document}


• Thanks for the info. Let me try for large matrices too. – Abhinav Sinha May 9 at 7:12

If you add nullify dots, it looks much better IMHO.

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
$$A =\begin{pNiceMatrix}[nullify-dots] 1 & 0 & \Cdots & & 0 \\ -1 & \Ddots & \Ddots & & \Vdots \\ 0 & \Ddots & & & \\ \Vdots & \Ddots & & & 0 \\ & & & & 1 \\ 0 & \Cdots & & 0 & -1 \end{pNiceMatrix},$$
\end{document}


• Thanks for the info. Let me try for large matrices too. – Abhinav Sinha May 9 at 7:12