7
$\begingroup$

For this post I'd like to align some euqations so that matching terms are arranged in columns. But both alternatives that I could think of didn't work as intended:

Using \begin{array}{rlcrrrrrrr} I get too much space between the columns, i,.e. in front of the operators:

$$\begin{array}{rlcrrrrrrr} \exp(x) &= \sum_{n=0}^\infty\frac{x^n}{n!} &=& 1 &+ x &+ \frac{x^2}{2!} &+ \frac{x^3}{3!} &+ \frac{x^4}{4!} &+ \frac{x^5}{5!} &+ \cdots \\ \sin(x) &= \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} &=&& x &&- \frac{x^3}{3!} &&+ \frac{x^5}{5!} &- \cdots \\ \cos(x) &= \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &=& 1 &&- \frac{x^2}{2!} &&+ \frac{x^4}{4!} &&- \cdots \end{array}$$

Using \begin{alignat*}{9} I get too little space. Apparently my operators don't get the spacing they usually do.

\begin{alignat*}{9} \exp(x) &\,= \sum_{n=0}^\infty\frac{x^n}{n!} &=& 1 &+& x &+& \frac{x^2}{2!} &+& \frac{x^3}{3!} &+& \frac{x^4}{4!} &+& \frac{x^5}{5!} &+& \;\cdots \\ \sin(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} &=&&& x &&&-& \frac{x^3}{3!} &&&+& \frac{x^5}{5!} &-& \;\cdots \\ \cos(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &=& 1 &&&-& \frac{x^2}{2!} &&&+& \frac{x^4}{4!} &&&-& \;\cdots \end{alignat*}

If this were $\LaTeX$, I'd use @{} in or @{\,} in the array approach, but MathJax does not seem to understand this.

Is there a way to obtain this level of control over the horizontal spacing of custom-aligned equations, without placing all the \, manually on both sides of each operator?

$\endgroup$
5
$\begingroup$

The problem is not with alignat, which by design doesn't add any extra spaces, and is precisely what you want. The problem is that your way of using alignat forces the binary operators + and - to be interpreted as ordinary characters instead of binary operators which includes appropriate spacing.

You wrote

\begin{alignat*}{9}
\exp(x) &\,= \sum_{n=0}^\infty\frac{x^n}{n!} &=& 1 &+& x &+&
\frac{x^2}{2!} &+& \frac{x^3}{3!} &+& \frac{x^4}{4!} &+&
\frac{x^5}{5!} &+& \;\cdots \\
\sin(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} &=&&& x
&&&-& \frac{x^3}{3!} &&&+& \frac{x^5}{5!} &-& \;\cdots \\
\cos(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &=& 1 &&&-&
\frac{x^2}{2!} &&&+& \frac{x^4}{4!} &&&-& \;\cdots
\end{alignat*}

The simplest way to get what you want is to write

\begin{alignat*}{7}
\exp(x) &\,= \sum_{n=0}^\infty\frac{x^n}{n!} && = 1 & {}+ x && {} +
\frac{x^2}{2!} && {}+ \frac{x^3}{3!} && {}+ \frac{x^4}{4!} && {}+
\frac{x^5}{5!} && {} + \cdots \\
\sin(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} && =&
x
&&&&{} - \frac{x^3}{3!} &&&&{} + \frac{x^5}{5!} &&{} - \cdots \\
\cos(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &&= 1 &&&{} -
\frac{x^2}{2!} &&&&{} + \frac{x^4}{4!} &&&&{} - \cdots
\end{alignat*}

The empty object {} asks the parser to interpret the + sign following it (which is itself followed by a variable) as a mathematical binary operator, and this way the proper spacing for a binary operator is generated.

Here are the outputs as compiled by TeXLive 2012 enter image description here

And here's the corresponding renderings in MathJax

\begin{alignat*}{9} \exp(x) &\,= \sum_{n=0}^\infty\frac{x^n}{n!} &=& 1 &+& x &+& \frac{x^2}{2!} &+& \frac{x^3}{3!} &+& \frac{x^4}{4!} &+& \frac{x^5}{5!} &+& \;\cdots \\ \sin(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} &=&&& x &&&-& \frac{x^3}{3!} &&&+& \frac{x^5}{5!} &-& \;\cdots \\ \cos(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &=& 1 &&&-& \frac{x^2}{2!} &&&+& \frac{x^4}{4!} &&&-& \;\cdots \end{alignat*}

\begin{alignat*}{7} \exp(x) &\,= \sum_{n=0}^\infty\frac{x^n}{n!} && = 1 & {}+ x && {} + \frac{x^2}{2!} && {}+ \frac{x^3}{3!} && {}+ \frac{x^4}{4!} && {}+ \frac{x^5}{5!} && {} + \cdots \\ \sin(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} && =& x &&&&{} - \frac{x^3}{3!} &&&&{} + \frac{x^5}{5!} &&{} - \cdots \\ \cos(x) &\,= \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &&= 1 &&&{} - \frac{x^2}{2!} &&&&{} + \frac{x^4}{4!} &&&&{} - \cdots \end{alignat*}

$\endgroup$
  • $\begingroup$ I'm not sure I consider adding {} that much easier than the \, I mentioned in my question, but I agree that it is conceptually cleaner. I've used this approach for now, but somehow I still hope someone might come up with an even more elegant solution… $\endgroup$ – MvG Jul 30 '13 at 14:48
  • $\begingroup$ @MvG: the advantage is that with {} you are being consistent: if someone changes the spacing used for binary operators your entire expression will change in the same way. If you code in the \, ad hoc it "may" break. Of course, in practice this kind of situation is almost never a problem. $\endgroup$ – Willie Wong Jul 31 '13 at 8:33
3
$\begingroup$

An alternative that I'm pretty sure you won't like is to use \phantom to leave space for the missing terms (rather than use an array). E.g.

\begin{alignat*}{1}
    \exp(x) &{} = \sum_{n=0}^\infty\frac{x^n}{n!} &{} = 
    1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} +
       \frac{x^5}{5!} + \cdots \\
    \sin(x) &{} = \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} &{} =
    \phantom{1 +{}} x \phantom{{}+ \frac{x^2}{2!}} - \frac{x^3}{3!}
       \phantom{{}+ \frac{x^4}{4!}} + \frac{x^5}{5!} - \cdots \\
    \cos(x) &{} = \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &{} = 
    1 \phantom{{}+ x} - \frac{x^2}{2!} \phantom{{}+ \frac{x^3}{3!}} +
       \frac{x^4}{4!} \phantom{{}+ \frac{x^5}{5!}} - \cdots
    \end{alignat*}

You do need to use the {} trick to make the phantomed + signs be binary rather than unary ones.

\begin{alignat*}{1} \exp(x) &{} = \sum_{n=0}^\infty\frac{x^n}{n!} &{} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \frac{x^5}{5!} + \cdots \\ \sin(x) &{} = \sum_{n=0}^\infty\frac{(-1)^nx^{2n+1}}{(2n+1)!} &{} = \phantom{1 +{}} x \phantom{{}+ \frac{x^2}{2!}} - \frac{x^3}{3!} \phantom{{}+ \frac{x^4}{4!}} + \frac{x^5}{5!} - \cdots \\ \cos(x) &{} = \sum_{n=0}^\infty\frac{(-1)^nx^{2n}}{(2n)!} &{} = 1 \phantom{{}+ x} - \frac{x^2}{2!} \phantom{{}+ \frac{x^3}{3!}} + \frac{x^4}{4!} \phantom{{}+ \frac{x^5}{5!}} - \cdots \end{alignat*}

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .