4

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:

alignment

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).

3 Answers 3

3

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).

enter image description here

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}

enter image description here

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*}.

enter image description here

5
  • 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
    Commented Apr 11, 2014 at 10:10
  • 1
    alignedat would be better
    – egreg
    Commented Apr 11, 2014 at 10:13
  • @egreg Ah! missed it. Thanks and I have changed accordingly.
    – user11232
    Commented Apr 11, 2014 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
    Commented Apr 11, 2014 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
    Commented Apr 11, 2014 at 11:00
1

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

enter image description here

\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}
1

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}
\usepackage[overload]{empheq}
\usepackage[svgnames, x11names]{xcolor}
\usepackage{bm}

\begin{document}

    \begin{alignat*}{2}[right =\color{IndianRed1}\vrule width1pt]
    \text{first value} &=   &12   \\
    \text{second value} & ={} &   1234  \\
    \text{third one} &=  & 1234567
    \end{alignat*}

\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 .