# aligned environment, align numbers to right

I have an "equation", where I simply want to give some constant values. I'm aligning on the equation symbols, but as the numbers have different amounts of digits, I would also like to align the numbers to the right:

LaTeX currently looks as follows:

\documentclass[ngerman]{scrbook}
\usepackage{amssymb,amsmath,amsthm}
\begin{document}
\begin{equation*}
\begin{aligned}
\text{first value} &= 12 & \\
\text{second value} &= 1234 & \\
\text{third one} &= 1234567 &
\end{aligned}
\end{equation*}
\end{document}


Is it possible to align to the right with aligned? Are there any other packages for achieving this (I would like to stick to the equation if possible, for consistency reasons).

Like this?

\documentclass{scrbook}
\usepackage{amssymb,amsmath,amsthm}
\begin{document}
\begin{equation*}
\begin{alignedat}{2}
\text{first value} &={}& 12  \\
\text{second value} &={}& 1234  \\
\text{third one} &={}& 1234567
\end{alignedat}
\end{equation*}
\end{document}


I have just moved the & before the numbers and used alignedat (thanks to egreg).

This is with \makebox:

\documentclass{scrbook}
\usepackage{mathtools}
\newcommand\mybox[1]{\makebox[1.5cm][r]{$#1$}}  %% change 1.5cm to fit in the largest integer
\begin{document}
\begin{equation*}
\begin{aligned}
\text{first value} &= \mybox{12}  \\
\text{second value} &= \mybox{1234}  \\
\text{third one} &= \mybox{1234567}
\end{aligned}
\end{equation*}
\end{document}


Another option with a tabular:

\documentclass{scrbook}
\usepackage{amsmath,array}
\begin{document}
\begin{equation*}      %% or \begin{center}
\begin{tabular}{r!{$=$}r}
first value & 12  \\
second value & 1234  \\
third one & 1234567
\end{tabular}      %% or \end{center}
\end{equation*}
\end{document}


Here you can use begin{center} or \centering (within a group) instead of \begin{equation*}.

• Your first and second solutions seem to insert far more white space to the right of the = sign than to the left (even when looking at just the third row). Is there a way to make the position of the = sign more centered? – Mico Apr 11 '14 at 10:10
• alignedat would be better – egreg Apr 11 '14 at 10:13
• @egreg Ah! missed it. Thanks and I have changed accordingly. – user11232 Apr 11 '14 at 10:53
• @HarishKumar The difference, as you know, is that aligned adds a padding between rl groups of columns, while alignedat doesn't; \begin{alignedat}{2} would be sufficient, the argument is the number of rl column groups. – egreg Apr 11 '14 at 10:55
• @egreg Yes, I apologise. I wrote in a hurry (as always) and missed the fine details. Thanks (as always) and corrected. :) – user11232 Apr 11 '14 at 11:00

You could use an array environment inside an equation* environment and auto-generate the = symbols.

\documentclass{scrbook}
\usepackage{amsmath} % for "\text" macro and "equation*" environment
\begin{document}
\begin{equation*}
\begin{array}{r@{{}={}}r} % "@{{}={}}" inserts correctly-spaced equal sign between the columns
\text{first value}  &      12  \\
\text{second value} &    1234  \\
\text{third one}    & 1234567
\end{array}
\end{equation*}
\end{document}


The simplest way is using align*. In order to have a correct spacing around the = sign, you need to ass a pair of{} at least in the line that contains the longest number.

I loaded empheqto have an easy way of adding the vertical line at the right of the equations. Ot loads mathtools, that in turn loads (and corrects 2 bugs of) amsmath.

\documentclass[ngerman]{scrbook}

\usepackage{amssymb, amsthm}