14

One thing that has infuriated me for a long time is this:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\begin{document}
    \begin{align}
        x &\coloneqq a^2 + b^2\\
        &= c^2
    \end{align}
\end{document}

I just want to introduce a definition and then proceed with a calculation. But it looks horrible:

enter image description here

The \coloneqq and = are not aligned on the right hand side, and the formulas following are neither.

(Note: Putting the & on the other side of the equations gives the correct alignment, but the wrong spacing to the formulas on the right hand side)

3

3 Answers 3

20

Since align right-aligns the first field and left aligns the second, one must place the \coloneqq and subsequent = in the first align field so that they get right aligned.

However, align is set up to provide proper \mathrel spacing for &= constructs, but not =& constructs. Thus, in addition to placing \coloneqq and subsequent = at the end of the first align field of their respective rows, they must be suffixed with an empty {} placeholder prior to the & separator, so that the \mathrel character of these two symbols can be forced to provide the proper spacing following the symbols.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
    \begin{align}
        x \coloneqq{}& a^2 + b^2\\
        ={}& c^2
    \end{align}
\end{document}

enter image description here

7
  • 3
    My life was futile without this. Thank you.
    – Turion
    Sep 12, 2016 at 10:45
  • 1
    @Turion THe {} placeholder on one, the other, or both side(s) of a \mathrel object tells the \mathrel object to provide spacing as if something were there. Sep 12, 2016 at 10:47
  • Then I wonder why no {} is needed when we put the & on the other side.
    – Turion
    Sep 12, 2016 at 10:52
  • 1
    @Turion the align environment is designed to "catch" the case where the = follows the & and insert space appropriately. It is not set up for the case where the = precedes the & and thus, for this case, the {} must be added manually. Sep 12, 2016 at 11:02
  • 2
    @Turion {} is automatically added at the beginning of the cell.
    – egreg
    Sep 12, 2016 at 11:02
5

Another simple solution with the very small package makebox, which puts its second argument in a box of the first argument width:

\documentclass{article}

\usepackage{mathtools}
\usepackage{makebox}

\begin{document}
\setlength\fboxsep{0pt}
\begin{align}
  x &\coloneqq a^2 + b^2\\
  &\mathrel{\makebox*{$\coloneqq$}{\hfill$=$}} c^2
\end{align}

\end{document}

enter image description here

4

Here is the definition of \coloneqq:

> \coloneqq=macro:
->\vcentcolon \mathrel {\mkern -1.2mu}=

We can place whatever precedes = inside \phantom:

enter image description here

\documentclass{article}

\usepackage{mathtools}

\begin{document}

\begin{align}
  x &\coloneqq a^2 + b^2\\
  &\phantom{\vcentcolon\mathrel{\mkern-1.2mu}}= c^2
\end{align}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.