[Tex/LaTex] How to reduce hspace between columns in align environment

alignspacing

With the following MWE, I have 2 columns:

\documentclass[12pt, a4paper]{scrartcl}
\usepackage{amsmath}
\begin{document}
\begin{align*}
4   &= 4 && \text{yes}\\
0   &= 0 && \text{no}\\
1+1 &= 2 && \text{maybe}
\end{align*}
\end{document}

But I would like to control the hspace between the 2 columns, possibly, with only one command call for the whole align, rather than one for each line.

Best Answer

You can use the alignat environment, that gives full control over the alignment:

\begin{alignat*}{2}
4   &= 4\qquad && \text{yes}\\
0   &= 0\qquad && \text{no}\\
1+1 &= 2\qquad && \text{maybe}
\end{alignat*}

enter image description here

Actually only the widest entry in the second column needs the padding, but it's easier to specify it on each one.

The "2" refers to the number of "left hand side-right hand side" groups you need.

A more general solution is to define a new environment:

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{environ}
\makeatletter
\newdimen\royalignsep@
\def\royalign@preamble{%
   &\hfil
    \strut@
    \setboxz@h{\@lign$\m@th\displaystyle{##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \tabskip\z@skip
   &\setboxz@h{\@lign$\m@th\displaystyle{{}##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \hfil
    \tabskip\royalignsep@
}
\NewEnviron{royalign}[1]{%
  \royalignsep@=#1\let\align@preamble=\royalign@preamble
  \begin{align}\BODY\end{align}}
\NewEnviron{royalign*}[1]{%
  \royalignsep@=#1\let\align@preamble=\royalign@preamble
  \begin{align*}\BODY\end{align*}}
\makeatother
\pagestyle{empty}
\begin{document}

\begin{royalign*}{1cm}
4   &= 4 & 1+3+5 &= 9\\
0   &= 0 & 2+1   &= 3\\
1+1 &= 2 & 1     &= 1
\end{royalign*}
\end{document}

You have both royalign and royalign*; you can check that the spacing is exactly what you specify in the argument.