# $\rm\LaTeX$ align code is not working properly.

Recently I had answered a question.

I had used the $$\rm\LaTeX$$ command \begin{align}...\end{align} but the equations are not perfectly aligned. This is the code I used

\begin{align} f(x)&=\lim_{n\to\infty} \frac{ax} {n}\left(\sum_{k=1}^n \frac{[k^2-e^{-x}+k-1]}{k(k+1)}\right)+\lambda\\ &=\lim_{n\to\infty} \frac{ax}{n}\left(\sum_{k=1}^n \frac{k^2+k-2}{k(k+1)}\right)+\lambda\\ &=\lim_{n\to\infty} \frac{ax}{n}\left(\sum_{k=1}^n 1-2\left(\frac{1}{k}-\frac{1}{k+1}\right)\right)+\lambda\\ &=\lim_{n\to\infty} \frac{ax}{n}\left(n+\frac{1}{n+1}-2\right)+\lambda\\ &=ax+\lambda \end{align}


What's wrong with the code? Or how can I fix it so that the equations are properly aligned?

• When I view the linked Answer on the main site, the equations appear properly aligned. I use Chromium on Linux (Fedora 31), and my Math Settings are Math Renderer HTML+CSS with Fast Preview enabled. Jun 19 '20 at 4:10
• Ok! I'm using mobile site. It might be because of that as the features are optimized on mobile site. Jun 19 '20 at 4:40
• I don't know what is causing the problem, but there are two potential issues: (1) you put the align environment into an inline math environment. Don't do this. MathJax detects \begin{align} and \end{align} all on its own; you don't need (or want) to put this environment into another math environment; (2) it is possible that the "responsive design" is causing problems---phone screens are quite narrow, and it may be that your lines are too long to render nicely on a phone screen.
– Xander Henderson Mod
Jun 19 '20 at 5:15

## 1 Answer

StackExchange has automatic line breaking turned on so that long expressions are broken, when possible. For mobile devices, the width is small, and so this can occur more often than on desktop computers. MathJax tries to avoid breaking within parentheses, and so the "natural" breakpoint is at the equal sign. That is what you are seeing, here, with the equal sign on one line and the limit on another.

• Does using one dollar versus two make a difference here? In particular, I notice that the new lines start in the middle of the line, so would this change if the number of dollars used was changed? (Xander Henderson's comment to the original post made me wonder about this, and if we should indeed care about the number of dollars used with \align.) Jun 21 '20 at 18:55
• No, when line breaking is enabled, both in-line (single dollar) and displayed math (double dollar and \begin{...}) will cause line breaking to occur when the math is wider than the container width. You should not enclose \begin{align}...\end{align} in dollar signs of any type, as they represent an AMS math environment that is intended to be a top-level math container. MathJax doesn't prevent you from putting them in dollar signs, but that would cause an error in actual LaTeX. Jun 22 '20 at 12:39
• Thanks, I never knew that! (I knew that LaTeX doesn't require, or indeed like, dollars around the align environment, but I've always assumed that MathJax needs dollars around all the maths, even fancy environments. I don't know why I though that.) Jun 22 '20 at 16:00
• If I recall correctly, that was true of the very earliest versions of MathJax, but hasn't been in some time. Probably not since v2.0.0 Jun 22 '20 at 16:49
• That makes sense then. I seem to have been using it since before that release, and my knowledge had not been updated at the same rate as MathJax. Jun 22 '20 at 16:55
• @Davide Cervone, using {align*} can make the equations perfectly aligned, right? Jun 22 '20 at 17:57
• @doubtnut, the star does not change the alignment of the equation. In AMS document in LaTeX, alignments and other top-level tabular math environments produce equations with automatically generated equation numbers. The starred form produces the same alignment without those automatic numbers. In MathJax, automatic numbering is off by default (for historical reasons), but can be activated in the MathJax configuration. So by default the starred and unstarred versions should be the same. Jun 22 '20 at 18:23