# Align in Substack

\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?

-
Please be more specific about what you mean by "align" in this context: Should the rows be left-aligned, right-aligned, aligned on the equal/not equal symbol, or maybe something else? – Mico Aug 31 '14 at 11:21
Aligned on the equal/not equal symbol – Pippo Aug 31 '14 at 11:31
@Pippo I wouldn't try aligning on the relation symbol; seeing the result confirms my idea. ;-) – egreg Aug 31 '14 at 16:20
Thank you very much for all your precious replies. I chose egreg's answer because the final spacing between lines was the best. – Pippo Aug 31 '14 at 18:14

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@
\lineskip\thr@@\fontdimen8 \scriptfont\thr@@
\lineskiplimit\lineskip
\ialign{\hfil$\m@th\scriptstyle##$&$\m@th\scriptstyle{}##$\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}

-

You can also use \mathmakebox from mathtools

\mathmakebox[width][pos]{content}

MWE:

\documentclass{article}
\usepackage{mathtools}
\usepackage{calc}   %% provides \widthof

\begin{document}
$\sum_{\substack{xyz=1\\n\neq 45}}^N \quad\text{vs.}\quad \sum_{\substack{xyz=1\\\mathmakebox[\widthof{xyz}+2\fboxrule][r]{n} \neq 45}}^N$
\end{document}

-
The use of \makemathbox won't assure vertical alignment on the = and \ne symbols, resp. Replace, e.g., 45 with 4545 to see what I mean. – Mico Aug 31 '14 at 14:20

You could define your own specialized array environment, along the lines of the following example; the first expression uses a "regular" \substack expression, and the second uses a custom array environment called mysubarray. (I offer my abject apologies in advance for the lack of mathematical meaning -- the example is merely meant to illustrate the typographic aspects.)

\documentclass{article}
\usepackage{array,amsmath}

\newcolumntype{L}{>{\scriptstyle}l}
\newcolumntype{C}{>{\scriptstyle}c}
\newcolumntype{R}{>{\scriptstyle}r}
\newenvironment{mysubarray}{%
\scriptstyle
\setlength\arraycolsep{0pt}%
\setlength\extrarowheight{-1ex}
\renewcommand\arraystretch{0}
\begin{array}{RCL}}{\end{array}}

\begin{document}
$\sum_{\substack{xyz=1\\n\neq 45}}^N \quad\text{vs.}\quad \sum_{\begin{mysubarray} xyz&=&1 \\ n&\neq&45 \end{mysubarray}}^N$
\end{document}
-
Thank you very much for your answer, @Mico; btw, we may even be neighbours ;) However, I still have several problems with the spacing. – Pippo Aug 31 '14 at 12:10
@Pippo - please be a bit more specific about the problems with spacing. – Mico Aug 31 '14 at 13:02
Basically, I get a very large vertical space between the lines, similarly to using the aligned environment. I can reduce this space, but only to a certain limit (some still remains). – Pippo Aug 31 '14 at 13:12
I've added an extra instruction to the code of 'mysubarray' (\renewcommand\arraystretch{0}) to further tighten the row spacing. Let me know if this gives you a better result. – Mico Aug 31 '14 at 14:11