0

I'm completely new to LaTeX, and I ran into an issue when trying to align an equation with amsmath's align* function.

My specific issue is that when trying to use multiple alignment levels, it sometimes leaves huge gaps in one line, but aligns fine in the next one.

Here's my code :

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\begin{document}
\begin{align*}
    u_n&=\displaystyle\frac{2n^2+5n+1}{n^2+n}\longrightarrow\
        \lim_{n\to\infty}u_n&&=\lim_{n\to\infty}\frac{2n^2+5n+1}{n^2+n}\\
        &&=\lim_{n\to\infty}\frac{n^2(2+\frac{5}{n}+\frac{1}{n^2})}{n^2(1+\frac{1}{n})}\\
        &&=\lim_{n\to\infty} \frac{2+\frac{5}{n}+\frac{1}{n^2}}{1+\frac{1}{n^2}}=2\\
\end{align*}
\end{document}

And here's a screenshot:

Example Screenshot

Notice the conspicuous blank space.

Help would be much appreciated, thanks in advance!

2
  • 1
    Off-topic: The \displaystyle instruction is redundant and does nothing but create code clutter. Delete it.
    – Mico
    May 5, 2020 at 14:32
  • Welcome to tex.sx. There's already a good answer, but it doesn't mention explicitly that the last line of an align shouldn't end with \\ . May 5, 2020 at 15:12

2 Answers 2

2

Edit :

As precised in the comments, the goal was to be able to align the equations on several levels. For that, the environment alignat is used. Here, I created 3 columns. Each &= denotes an alignment and each & is used to tell where the columns are split.

MWE :

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\begin{document}

\begin{alignat*}{3}
    u_n     &=\frac{2n^2+5n+1}{n^2+n}           &   &\implies\lim_{n\to\infty}u_n       &   &=\lim_{n\to\infty}\frac{2n^2+5n+1}{n^2+n}\\
            &                                   &   &                                   &   &=\lim_{n\to\infty}\frac{n^2(2+\frac{5}{n}+\frac{1}{n^2})}{n^2(1+\frac{1}{n})}\\
            &                                   &   &                                   &   &=\lim_{n\to\infty} \frac{2+\frac{5}{n}+\frac{1}{n^2}}{1+\frac{1}{n^2}}=2\\
    u_{n_k} &=\frac{2n_k^2+5n_k+1}{n_k^2+n_k}   &   &\implies\lim_{n\to\infty}u_{n_k}   &   &=\lim_{k\to\infty}\frac{2n_k^2+5n_k+1}{n_k^2+n_k}\\
            &                                   &   &                                   &   &=\lim_{k\to\infty}\frac{n_k^2(2+\frac{5}{n_k}+\frac{1}{n_k^2})}{n_k^2(1+\frac{1}{n_k})}\\
            &                                   &   &                                   &   &=\lim_{k\to\infty} \frac{2+\frac{5}{n_k}+\frac{1}{n_k^2}}{1+\frac{1}{n_k^2}}=2
\end{alignat*} 

\end{document}

Output :

enter image description here

Original answer

That is because your &'s were not well placed, along with breaking lines.

With align, you place the ampersand (the & symbol) just before the equal sign you want to align. At the end of each line, add a breaking line symbol (//) to go to the next line.

Note: I replaced \longrightarrow with \implies because I find it better for logical syntax.

MWE :

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\begin{document}
\begin{align*}
    u_n=\frac{2n^2+5n+1}{n^2+n}\implies\lim_{n\to\infty}u_n&=\lim_{n\to\infty}\frac{2n^2+5n+1}{n^2+n}\\
        &=\lim_{n\to\infty}\frac{n^2(2+\frac{5}{n}+\frac{1}{n^2})}{n^2(1+\frac{1}{n})}\\
        &=\lim_{n\to\infty} \frac{2+\frac{5}{n}+\frac{1}{n^2}}{1+\frac{1}{n^2}}=2\\
\end{align*} 
\end{document}

Output : enter image description here

5
  • That works, but what if I also want to align something with then original u_n? Like I have u_n = [...], u_n1 = [...], u_n2 = [...] etc. and I want them to be aligned too.
    – Parashoo
    May 5, 2020 at 14:10
  • 2
    Than try alignat.
    – Zarko
    May 5, 2020 at 14:12
  • @Parashoo Is this a good example of what you want to achieve ? imgur.com/a/Pu5nORR
    – Sileo
    May 5, 2020 at 14:33
  • @Zarko I tried alignat too, and it did not change anything. @Rphad Yes, that's what I'm going after.
    – Parashoo
    May 5, 2020 at 14:34
  • @Parashoo Please check my edited answer. Don't hesitate to mark it as accepted
    – Sileo
    May 5, 2020 at 14:54
0

There may be a problem if some equations have a too long part in the first alignment column. In this case a better solution uses a nested aligned environment.

Here are an illustrative examples: the first example, built with alignat*{2} works well, because the second equation is compatible. The second example uses the same structure, but has longer equation, and there is a blank space between the two columns. The last example use a single column, and a nested aligned in the r.h.s. of the first equation:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{alignat*}{2}
    u_n & =\frac{2n^2+5n+1}{n^2+n}\implies\lim_{n\to\infty}u_n& & =\lim_{n\to\infty}\frac{2n^2+5n+1}{n^2+n}\\
         & & &=\lim_{n\to\infty}\frac{n^2\bigl(2+\frac{5}{n}+\frac{1}{n^2}\bigr)}{n^2\bigl(1+\frac{1}{n}\bigr)}\\
        & & &=\lim_{n\to\infty} \frac{2+\frac{5}{n}+\frac{1}{n^2}}{1+\frac{1}{n}}=2 \\
   v_n & =\frac{2n^2+5n+1}{n^2+n}
\end{alignat*}
\vskip 1cm

\begin{alignat*}{2}
    u_n & =\frac{2n^2+5n+1}{n^2+n}\implies&\lim_{n\to\infty}u_n & =\lim_{n\to\infty}\frac{2n^2+5n+1}{n^2+n}\\
         & & &=\lim_{n\to\infty}\frac{n^2\bigl(2+\frac{5}{n}+\frac{1}{n^2}\bigr)}{n^2\bigl(1+\frac{1}{n}\bigr)}\\
        & & &=\lim_{n\to\infty} \frac{2+\frac{5}{n}+\frac{1}{n^2}}{1+\frac{1}{n}}=2 \\
   v_n & =\frac{2n^2+5n+1}{n^2+n} + \Bigl(\sqrt{n^2 + n + 1}\Bigr)^{\!3}
\end{alignat*}
\vskip 1cm

\begin{align*}
    u_n & =\frac{2n^2+5n+1}{n^2+n}\implies\lim_{n\to\infty}u_n \begin{aligned}[t] & =\lim_{n\to\infty}\frac{2n^2+5n+1}{n^2+n}\\
        &=\lim_{n\to\infty}\frac{n^2\bigl(2+\frac{5}{n}+\frac{1}{n^2}\bigr)}{n^2\bigl(1+\frac{1}{n}\bigr)}\\
       &=\lim_{n\to\infty} \frac{2+\frac{5}{n}+\frac{1}{n^2}}{1+\frac{1}{n}}=2
        \end{aligned} \\
   v_n & =\frac{2n^2+5n+1}{n^2+n} + \Bigl(\sqrt{n^2 + n + 1}\Bigr)^{\!3}
\end{align*}

\end{document} 

enter image description here

You must log in to answer this question.

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