# Vertically align at operator, but keep default horizontal spacing

This is a follow up to «How can I align limits at “lim”?». What is the best approach to achieve the following alignment? Namely, I would like the two \prod to be aligned, but each line should remain identical to what (La)TeX produces by default, simply shifted horizontally to match the alignment requirement.

Here is the code which generates the above. As you can see, I've used an ad-hoc amount of \mspace.

\documentclass{standalone}
\usepackage{amsmath}
\begin{document}
\begin{minipage}{.75\textwidth}
\begin{align*}
& \prod_{-n\leq k\leq n-1} \left[\frac{k+1}{k}\right] = -1 \\
& \mspace{16mu} \prod_{1\leq j\leq n} j = \lim_{m\to\infty} \frac{m^{n+1} m!}{(n+1)(n+2)(n+3)\cdots(n+m+1)}
\end{align*}
\end{minipage}
\end{document}

-

The interesting thing is that \tempa gets set to zero at some point and has to be recomputed.

\documentclass{standalone}
\usepackage{mathtools}

\newlength{\tempa}

\newcommand{\aligncenter}[1]% #1=display math to be aligned
{\settowidth{\tempa}{$\displaystyle #1$}%
\hspace{0.5\tempa}&%
\settowidth{\tempa}{$\displaystyle #1$}%
\hspace{-0.5\tempa}#1}

\begin{document}
\begin{minipage}{0.75\textwidth}
\begin{align*}
\aligncenter{\prod_{-n\leq k\leq n-1}} \left[\frac{k+1}{k}\right] = -1 \\
\aligncenter{\prod_{1\leq j\leq n}} j = \lim_{m\to\infty} \frac{m^{n+1} m!}{(n+1)(n+2)(n+3)\cdots(n+m+1)}
\end{align*}
\end{minipage}
\end{document}


-
To avoid recomputing the width you can do \global\tempa=\tempa after computing it the first time. The problem arises because \settowidth assigns the value to \tempa locally, and & ends a group, hence the scope in which \tempa was set. – Bruno Le Floch Aug 30 '14 at 12:42
I have to say I like this approach, as it is not intrusive and as far as I can tell uses the default spacing. – Bruno Le Floch Aug 30 '14 at 12:43
@Bruno Le Floch - Yeah, I figured as much. I was thinking at first that \settowidth was global, like \addtolength. OTOH, a local \tempa is better since one can reuse it. – John Kormylo Aug 30 '14 at 14:09

In this specific case one sub-optimal option would be to use \smashoperator[l]:

\documentclass{article}
\begin{document}
\begin{align*}
& \smashoperator[l]{\prod_{-n \leq k \leq n-1}}\left[\frac{k+1}{k}\right] = -1 \\
& \smashoperator[l]{\prod_{1 \leq j \leq n}} j = \lim_{m\to\infty} \frac{m^{n+1} m!}{(n+1)(n+2)(n+3)\cdots(n+m+1)}
\end{align*}
\end{document}


I mention sub-optimal as the alignment is achieved, yet the total horizontal width of the aligned equation is short about the width of -n\leq that has zero width in the top \prod. One might correct this with some work. Here's one such attempt:

\documentclass{article}
\DeclareMathOperator*{\dummyop}{}
\begin{document}
\begin{align*}
& \smashoperator[l]{\prod_{-n \leq k \leq n-1}}\left[\frac{k+1}{k}\right] = -1 \\
& \smashoperator[l]{\prod_{1 \leq j \leq n}} j = \lim_{m\to\infty} \frac{m^{n+1} m!}{(n+1)(n+2)(n+3)\cdots(n+m+1)}
\end{align*}

\begin{align*}
& \smashoperator[l]{\prod_{-n \leq k \leq n-1}}\left[\frac{k+1}{k}\right] = -1 \\
& \smashoperator[l]{\prod_{1 \leq j \leq n}} j = \lim_{m\to\infty} \frac{m^{n+1} m!}{(n+1)(n+2)(n+3)\cdots(n+m+1)}
\hphantom{\smashoperator[r]{\dummyop_{-n \leq k \leq n-1}}}
\end{align*}

\end{document}


We create an empty operator \dummyop with the same, widest limits and smash it on the [r]ight at the end of the second (longest) equation.

-
Nice approach. I think the \hphantom construction should go before any of the &. Replacing all -n \leq k \leq n-1 by \rule{100pt}{5pt} shows that more clearly. My understanding is that the width of the alignment will be off by half of the width of \prod. Is that correct? – Bruno Le Floch Aug 30 '14 at 13:10

You can achieve that with the \mathclap command. As I don't see what the minipage environment is here for, I deleted it.

\documentclass{article}
\usepackage{mathtools}
\usepackage{nccmath}

\begin{document}

\begin{align*}
& \prod_{\mathclap{k = -n}}^{\mathclap{n-1}}\; \biggl[\frac{k+1}{k}\biggr] = -1 \\
& \prod_{\mathclap{j = 1}}^{n} j = \lim_{m\to\infty} \frac{m^{n+1} m!}{(n+1)(n+2)(n+3)\cdots(n+m+1)}
\end{align*}

\end{document}


-
With this approach you've changed the horizontal spacing between the operator and its operands. Therefore, each line does not "remain identical to what (La)TeX produces by default". – Werner Aug 29 '14 at 23:48
Well it's shifted to the left, as you asked if I understood well. One may argue the value of the small space I add (or not) but on the whole I find it better-looking than in your post, especially in the 2nd line, where one does not know whether the limit is equal to j or to the product of the j-s. – Bernard Aug 30 '14 at 0:00
I'm sorry, I should have mentioned why \mathclap doesn't cut it for me: the limits are too long. In this simplified example, you are right that it is nicer looking to place the limits above and below \prod, but that cannot be done in complicated cases. I am looking for more automatic methods, which do not require fiddling with spacing, such as with \; in your first line. I picked a bad example. – Bruno Le Floch Aug 30 '14 at 13:12
I'm not sure there's a fully automatic way, since it involves aesthetic choices that may vary according to the context. I'd like to see one of your ‘complicated’ cases, just to tackle it. – Bernard Aug 30 '14 at 13:21