Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\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?

share|improve this question
    
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 at 11:21
    
Aligned on the equal/not equal symbol –  Pippo Aug 31 at 11:31
    
@Pippo I wouldn't try aligning on the relation symbol; seeing the result confirms my idea. ;-) –  egreg Aug 31 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 at 18:14

3 Answers 3

up vote 3 down vote accepted

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{}##$\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

share|improve this answer

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}
share|improve this answer
    
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 at 12:10
    
@Pippo - please be a bit more specific about the problems with spacing. –  Mico Aug 31 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 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 at 14:11

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

share|improve this answer
    
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 at 14:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.