[Tex/LaTex] Vertical alignment of dots in equation

equationshorizontal alignment

Consider the following example:

\documentclass{article}

\usepackage{mathtools}

\begin{document}

\begin{alignat*}{6}
                       1 &&                           2 &&                    \ldots &&                           r &&                    \ldots &&                     m & \\
                   m + 1 &&                       m + 2 &&                    \ldots &&                       m + r &&                    \ldots &&                    2m & \\
                  2m + 1 &&                      2m + 2 &&                    \ldots &&                      2m + r &&                    \ldots &&                    3m & \\
  \vdotswithin{(n-1)m+1} &&      \vdotswithin{(n-1)m+2} &&      \vdotswithin{\ldots} &&      \vdotswithin{(n-1)m+r} &&      \vdotswithin{\ldots} &&      \vdotswithin{nm} & \\
            (n - 1)m + 1 &&\quad           (n - 1)m + 2 &&\quad               \ldots &&\quad           (n - 1)m + r &&\quad               \ldots &&\quad               nm &
\end{alignat*}

\end{document}

output

As can be seen, the vertical dots are not perfectly centered relative to the longest entry in each column; they are a little to the left of the center. How do I fix this?

Best Answer

The \ldots is defined as \mathinner which adds surrounding space. When used in this context the extra space after is removed. Inside \vdotswithin it is treated as {}\ldots{} which ensure that the surrounding spaces are preserved.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{alignat*}{6}
                       1 &&                           2 &&                {}\ldots{} &&                           r &&                {}\ldots{}&&                     m & \\
                   m + 1 &&                       m + 2 &&                {}\ldots{} &&                       m + r &&                {}\ldots{}&&                    2m & \\
                  2m + 1 &&                      2m + 2 &&                {}\ldots{} &&                      2m + r &&                {}\ldots{}&&                    3m & \\
  \vdotswithin{(n-1)m+1} &&      \vdotswithin{(n-1)m+2} &&     \vdotswithin{\ldots}  &&      \vdotswithin{(n-1)m+r} &&     \vdotswithin{\ldots} &&      \vdotswithin{nm} & \\
            (n - 1)m + 1 &&\quad           (n - 1)m + 2 &&\quad           {}\ldots{} &&\quad           (n - 1)m + r &&\quad           {}\ldots{} &&\quad               nm &
\end{alignat*}
\end{document}

enter image description here