# 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, 2014 at 11:21
• Aligned on the equal/not equal symbol Aug 31, 2014 at 11:31
• @Pippo I wouldn't try aligning on the relation symbol; seeing the result confirms my idea. ;-) Aug 31, 2014 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. Aug 31, 2014 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{}##$\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}


• Could you elaborate on this, please? I am trying to adapt this for multiple alignment points, but fail so far. Why is this low-level approach superior to something involving array or aligned? Jul 16, 2017 at 12:06
• @Bubaya Please, ask a new question; however I'd be wary of using multiple alignment points in such a situation. Jul 16, 2017 at 12:10
• @egreg I would love to use your solution together with \Big\rvert, but the problem is that the stacked lines end up being vertically misjustified with the bottom "tip" of the vertical bar (\rvert). So, I've been wondering what could I add to your code so that the "box" with the stacked lines is shifted up/down? Aug 8, 2019 at 15:22
• @SimonRighley This is worth a new question :) For a quick solution, try replacing the \vcenter with \vbox. Aug 15, 2019 at 2:14
• @egreg Thank you! Good tip! Not 100% satisfied, but with \vbox looks much better indeed. Aug 16, 2019 at 12:41

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, 2014 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}

• @Pippo - please be a bit more specific about the problems with spacing.
– Mico
Aug 31, 2014 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). Aug 31, 2014 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, 2014 at 14:11