3

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

\begin{equation}
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},
\end{equation}

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

enter image description here

The same matrix, when written as

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

results in a rather misaligned one.

enter image description here

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

\begin{equation}
    \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}
\end{equation}

enter image description here

3

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}
\begin{equation}
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{equation}
\end{document}

Result of above code

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

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

\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\begin{equation}
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{equation}
\end{document}

enter image description here

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

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.