13
\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
20

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

  • 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? – Bubaya Jul 16 '17 at 12:06
  • @Bubaya Please, ask a new question; however I'd be wary of using multiple alignment points in such a situation. – egreg Jul 16 '17 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? – Simon Righley Aug 8 '19 at 15:22
  • @SimonRighley This is worth a new question :) For a quick solution, try replacing the \vcenter with \vbox. – Ruixi Zhang Aug 15 '19 at 2:14
  • @egreg Thank you! Good tip! Not 100% satisfied, but with \vbox looks much better indeed. – Simon Righley Aug 16 '19 at 12:41
5

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}

enter image description here

  • 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
4

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.)

enter image description here

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

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.