[Tex/LaTex] Align in Substack

alignsubscripts

\sum_{\substack{n=1\\n\neq i}}^N

Using \substack to add multiple lines as subscripts of a sum does not make them aligned on the equal / not equal symbol.
I found some answers to this problem, like phantom characters with \hphantom or the aligned environment; however, the former is not useful in my case, while the latter ruins the spacing, and I would rather not have to manually fix it.

Isn't there a package which simply aligns two rows using the ampersand symbol in a math environment, similarly to tabular?

Best Answer

This essentially duplicates \substack, but adding an alignment point.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\subalign}[1]{%
  \vcenter{%
    \Let@ \restore@math@cr \default@tag
    \baselineskip\fontdimen10 \scriptfont\tw@
    \advance\baselineskip\fontdimen12 \scriptfont\tw@
    \lineskip\thr@@\fontdimen8 \scriptfont\thr@@
    \lineskiplimit\lineskip
    \ialign{\hfil$\m@th\scriptstyle##$&$\m@th\scriptstyle{}##$\hfil\crcr
      #1\crcr
    }%
  }%
}
\makeatother

\begin{document}
\[
\sum_{\substack{n=1\\n\neq i}}^N
\sum_{\subalign{n&=1\\n&\neq i}}^N
\sum_{\subalign{uvw&=1\\n&\neq ijk}}^N
\]
\end{document}

enter image description here