24

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.

David Carlisle
  • 757,742
Loic Rosnay
  • 8,167
  • You could use an array environment instead and then {rl@{\quad}l} for the column specificator. – Martin Scharrer Jan 05 '12 at 10:47
  • 1
    Have you seen this Question http://stackoverflow.com/questions/4361275/latex-ams-align-align-multiple-too-much-space ? it at least is able to reduce the space between the first 3 columns (your equations) and the second column, where you put the text. – Ronny Jan 05 '12 at 10:49
  • @Ronny The question is also on TeX.SX: http://tex.stackexchange.com/questions/6572/ams-align-align-multiple-too-much-space – egreg Jan 05 '12 at 11:56

2 Answers2

30

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.

David Carlisle
  • 757,742
egreg
  • 1,121,712
  • yes, this solution is good for the situation "one column of equations + one column of text". But for two column of equations, this is not optimal, since one has to put a hspace which depends on the largest entry of the right hand side terms of the left hand side column. I would prefer to say " i want 1 cm between the 2 columns of equations", independently of their content. Am i clear ? – Loic Rosnay Jan 05 '12 at 17:30
  • by the way, i don't see any influence of the parameter "2"... – Loic Rosnay Jan 05 '12 at 17:35
  • @nicolasroy See edit. – egreg Jan 05 '12 at 18:10
  • My apologize for being so late in answering. Thanks for the solution, it works very well. Could you just tell me, why you use \NewEnviron ? What does it bring in contrast to \newenvironment{royalign}{\royalignsep@ ... \begin{align}}{\end{align}} – Loic Rosnay Jan 09 '12 at 20:32
  • @nicolasroy It's the usual problem about align, where TeX has to "see" the precise sequence of tokens \end{align} in order to decide when the environment ends, since it has to collect the contents and measure it. Actually, \NewEnviron is inspired by amsmath's method: it collects the environments contents and, in this case, it passes it to align. – egreg Jan 09 '12 at 20:35
  • A detail: with your code, the bloc of equations is no more horizontally centered. Compare the equations of your solution (in the edit) for \begin{royalign*}{1cm}... and for \begin{align*}{3} – Loic Rosnay Jan 09 '12 at 21:10
  • Sorry: this would need to redesign completely the computations that amsmath performs. At least as far as I can see. – egreg Jan 09 '12 at 21:51
  • ok. No problem. Thanks a lot for the solution anyway. – Loic Rosnay Jan 09 '12 at 21:57
3

use the old xalignat environment if it could be left aligned

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