# How to align right within equation array

Suppose I have something like the following:

    \begin{eqnarray*}
\mathrm{gcd}(x^3 + 6x^2 + 5x + 5, x^3 + 13x^2 + 6x + 3) &=& 102x^2 + 108x + 2 \ \mathrm{with} \ p = 109 \\
\mathrm{gcd}(x^3 + 2x^2 + 9x + 4, x^3 + 3x^2 + 7x + 9) &=& 1 \ \mathrm{with} \ p = 131 \\
\mathrm{gcd}(x^3 + 3x^2 + 9x + 12, x^3 + 6x^2 + 12x + 4) &=& 6x + 122 \ \mathrm{with} \ p = 157
\end{eqnarray*}


How do I align the statements at the end of each line (i.e. the "with p = ..." bit) to the right?

-
–  lockstep Mar 29 '12 at 20:04
–  Peter Grill Mar 29 '12 at 22:20

Never ever use eqnarray!

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
\gcd(x^3 + 6x^2 + 5x + 5, x^3 + 13x^2 + 6x + 3) &= 102x^2 + 108x + 2 &&\text{with } p = 109 \\
\gcd(x^3 + 2x^2 + 9x + 4, x^3 + 3x^2 + 7x + 9) &= 1 &&\text{with } p = 131 \\
\gcd(x^3 + 3x^2 + 9x + 12, x^3 + 6x^2 + 12x + 4) &= 6x + 122 &&\text{with } p = 157
\end{align*}
\end{document}


The align environment provided by amsmath (with the not numbered *-variant) provides for as many alignment points you want. The first column is right aligned, the second is left aligned, the third is right aligned and so on.

Notice that the & goes before the alignment point (usually a relation).

So we put the explanation text in the fourth column; the rl blocks are separated from one another by suitable space.

The \text command allows spaces in it (contrarywise to \mathrm). The \gcd operator is predefined in LaTeX.

-
Why not to use eqnarray? you may ask. So, they say "Avoid eqnarray" in many ways: PracTeX Journal, TeX Blog, and LaTeX Tips. –  adn Mar 30 '12 at 5:48