6

I want a environment for displaying rows and columns of mathematical expressions, such that the contents of each column is centered, and the columns are equally spaced. Rows must have optional equation numbers, as with the align* environment.

I posted a similar question a few months ago, answered here: Standard math environment for equally-spaced columns of expressions?

Although I accepted the answer there at the time, it has some shortcomings: I have to specify in advance how many columns I'm going to need (unlike the existing align* environment, for example), and it doesn't allow me to have equation numbers for rows in a straightforward way.

Edit on 3rd April 2012: The syntax has to be like this:

\begin{newenvironment}
A & B & C
\\
\nonumber
f & g & h
\end{newenvironment}

This should produce two lines of displayed mathematics in three columns, all equally-spaced from the page margins and from each other. The first line should have an equation number, the second shouldn't.

2
  • Have you tried looking at equationarray enviornment? Mar 23, 2012 at 13:15
  • I have now. It doesn't seem to provide the spacing that I need. Apr 3, 2012 at 10:48

2 Answers 2

5
+50

Ok, the following is probably as dirty as it could ever get, and I have to admit I patched it together by trial&error, but at least it might invoke the spirits-that-be to find a better solution.

Try

\documentclass{article}

\usepackage{amsmath}

\usepackage{etoolbox}

\makeatletter
\def\calign@preamble{%
   &\hfil\strut@
    \setboxz@h{\@lign$\m@th\displaystyle{##}$}%
    \ifmeasuring@\savefieldlength@\fi
    \set@field
    \hfil
    \tabskip\alignsep@
}
\let\cmeasure@\measure@
\patchcmd\cmeasure@{\divide\@tempcntb\tw@}{}{}{}
\patchcmd\cmeasure@{\divide\@tempcntb\tw@}{}{}{}
\patchcmd\cmeasure@{\ifodd\maxfields@
  \global\advance\maxfields@\@ne
  \fi}{}{}{}    
\newenvironment{calign}
{%
  \let\align@preamble\calign@preamble
  \let\measure@\cmeasure@
  \align
}
{%
  \endalign
}  
\makeatother

\begin{document}

\begin{calign}
  A & \min(B,Z) & C
\\
\nonumber
f & g & h
\end{calign}

\end{document}

calign example

7
  • I had tried something like that, but I didn't find the right trick. Nice job.
    – egreg
    Apr 3, 2012 at 14:24
  • Hi Stephen. Having played with this, it seems the spacing is only correct if there are an even number of columns. Try with a single column to see the effect at its most extreme. But it's looking great! Apr 3, 2012 at 15:40
  • @JamieVicary Can you describe the problem you are seeing in a little more detail? Btw, the example you gave doesn't have an even number of columns. Apr 3, 2012 at 16:03
  • You can see the issue in the screenshot you posted; there is more space between the rightmost column and the right-hand margin than between the leftmost column and the left-hand margin. Apr 3, 2012 at 16:38
  • Ok, found one more ;-) Unfortunately amsmath goes out of its way to catch if you've forgotten a trailing & :-) Try the updated code. Apr 3, 2012 at 16:50
0

Try this. Although you still need to specify beforehand how many columns you need, adding equation number is easy.

\documentclass{article}
\usepackage{lipsum,array,eqnarray,amsmath}

\begin{document}
\lipsum[1]
\newcolumntype{S}{@{}c@{}}
\newcolumntype{E}{@{}c@{\phantom{(1)}}} %Provision for eq number
\arraycolsep 0.2em
\begin{equationarray*}{@{\extracolsep{\fill}}S*{3}{c}E}
  & x+y & e^{2 \pi i} & \sigma & \\
  & \text{First} & \text{Second} & \text{Third} & \yesnumber \\
  & \sqrt{x+y+z} & \int{x^2} dx & \Omega & \\
\end{equationarray*}
\lipsum[2]

\end{document}

You then just use \yesnumber for each row(s) you want equation numbers inserted. If you want extra height between rows, you can use \setlength{\extrarowheight}{10pt} for example, but I would personally append to where the phantom command is with \vspace*{*max height*} because that'd ensure every row is the same height regardless of content. E.g. in above the integral stretches the vertical space. Redefining E with \newcolumntype{E}{@{}c@{\phantom{\int{x^2}}\vspace*{0.1cm}}} solves it.

3
  • Thanks Mobius Pizza! (Is that your real name?) I accept that this is an improvement on the status quo. But what I want is clearly possible, even if it might require something quite low level to achieve it - so I'm going to hold out in the hope that someone produces a full answer. Mar 23, 2012 at 17:14
  • lol no isn't my real name. May I ask what is it you want that the above couldn't achieve? Changing column size really doesn't take much work. The only problem is you can't mix different number of columns within one environment, but to that end you can always use multiple arrays. Mar 23, 2012 at 19:36
  • 1
    The difficulty is not achieving what I want; it's implementing it in a way that exposes an effortless syntax to the user, just like the amsmath align environment. In particular I don't want to have to specify the number of columns beforehand! Apr 3, 2012 at 10:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .