## LaTeX forum ⇒ Math & Science ⇒ How to align Items in a Sum?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm

### How to align Items in a Sum?

I'm having a small alignment problem with the idex of a sum :
\sum_{\substack{i,\, j \,=\, 1 \\ j \, \ne \, i}}  A_{ij}

The = and \ne signs aren't properly aligned. How should we modifity that code to align the items properly ?
Last edited by Cham on Wed Jun 22, 2011 10:47 pm, edited 1 time in total.

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
You can insert a vertical phantom, that is to say an appropriate box with hidden content.
\documentclass[11pt,a4paper]{article}\usepackage{amsmath} \begin{document}   $\sum_{\substack{i,\, j\,=\, 1 \\ \hphantom{i,} j\,\ne\, i}} A_{ij}$\end{document}

Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
localghost wrote:You can insert a vertical phantom

I tried it. The alignment isn't perfect.

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
Works fine for me. Please clarify in which way this is not satisfactory.
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
localghost wrote:Works fine for me. Please clarify in which way this is not satisfactory.

The "=" and "\ne" aren't exactly aligned. If you compile and watch the index closely, you'll notice a small (and noticeable) offset. It's ugly.

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
I can't comprehend this.
Attachments
The rendered output of the provided code.
sum-index.png (4.96 KiB) Viewed 6543 times
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

Stefan Kottwitz
Posts: 9595
Joined: Mon Mar 10, 2008 9:44 pm
Hi Cham,

since equations/inequalities have to be aligned at a relation symbol, you could use an aligned environment. Also here you can customize size and fine-tune spacing. For example:

\documentclass{article}\usepackage{amsmath}\begin{document}\sum_{\scriptsize\begin{aligned}i, j &= 1 \\[-4pt] j &\ne i\end{aligned}} A_{ij}\end{document}

Btw. Thorstens way with a phantom command is handy in many situations.

Stefan

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
Stefan_K wrote:Hi Cham,

since equations/inequalities have to be aligned at a relation symbol, you could use an aligned environment. Also here you can customize size and fine-tune spacing. For example:

\documentclass{article}\usepackage{amsmath}\begin{document}\sum_{\scriptsize\begin{aligned}i, j &= 1 \\[-4pt] j &\ne i\end{aligned}} A_{ij}\end{document}

This code works fine. Thanks a lot for it !

Cham
Posts: 936
Joined: Sat Apr 02, 2011 4:06 pm
I'm getting some warning messages in the log file about the previous formula :
LaTeX Font Warning: Command \scriptsize invalid in math mode on input line 222.

The formula associated to this warning message is this :
\sum_{\scriptsize\begin{aligned} i, j &= 1 \\[-4pt] i &\ne j \end{aligned}}

Apparently, the \scriptsize command is not allowed, while the compiled formula is nicely looking on the PDF version. What's going on here ?

Is there a better way to code that double-sum, without getting that warning message ?