4

Some time ago David Carlisle wrote an awesome brqalign function in this answer: https://tex.stackexchange.com/a/51543/4011. See the cited code below.

How to modify the brqalign environment such that one can specify in an optional argument the number of columns or at least the maximal number of columns? As in the original brqalign the columns should also be equally distributed over the whole textwidth.

\documentclass{article}
\usepackage[leqno]{amsmath}

\renewcommand\theequation{\alph{equation}}

\newenvironment{brqalign}{%
% START CODE
%%%%%%%%%%%%
% Do everything an a box, as you can not remove
% things from the main vertical list.
\setbox0\vbox\bgroup
% Allow \\ for ends of row.
\let\\\cr
% Use an \halign (I experimented with using one
% of the AMS alignments but they tend to want to 
% be full width, or already in math mode, so just
% use a primitive \halign here with two display math
% entries in its preamble.
\halign\bgroup
  \refstepcounter{equation}\theequation)\quad
  \hfil$\displaystyle##$&$\displaystyle{}##$\hfil\cr}%
{%
% END CODE
%%%%%%%%%%
% Finish the \halign.
\crcr\egroup
% Now, at this point the vertical list being assembled in
% box0 contains all the rows of the alignment these will be
% a sequence of \hboxes (which will all be forced to be the
% same width) and baselineskip glue. (While testing with the
% AMS alignments there was additional glue and penalties
% as well.
%
% We will discard all the glue and penalties but assemble
% a horizontal list of the boxes in box1.
% start by making box1 and empty hbox.
\global\setbox1\null
% Then loop backwards up the vertical list discarding any glue
% and penalties and grabbing hold of each box.
\loop
% This combination removes two adjacent glue items
% or glue followed by penalty, it may be more than needed
% but better be safe than sorry.
\unskip\unskip\unpenalty\unskip\unpenalty
% Glue and penalties we discard, but the boxes have the
% formula so remove this one from the current list and
% save it in box 4.
\setbox4\lastbox
% If there was no box box4 is void and we reached the top
% of the original vertical alignent so stop.
\ifvoid4
\else
% Otherwise Put this box4 at the front of the horizontal
% list being built in box1, followed by an em of space.
\global\setbox1\hbox{\box4\hskip 1em\unhbox1}%
% Go round the loop again.
\repeat
% End the group for the working box 0.
% The horizontal list is globally assigned
% to box 1 so will be visible after this group end.
\egroup
% Now put out all the boxes flush left, the usual paragraph breaker
% will fit as many on a line as it can. As each box is the same width
% vertical alignment is automatic.
\begin{flushleft}\unhbox1 \end{flushleft}%
% Ignore white space after the end of the environment
\ignorespacesafterend
}

\begin{document}

\begin{brqalign}
  x^2 + y^2 &= 1\\
  a + b &= c\\
  r-x &= y+z \\
  f - y &= z\\
  a - b &= 2d\\
  r+x &= 2y-3z
\end{brqalign}


\end{document}
Moriambar
  • 11,466
student
  • 29,003

1 Answers1

6

I guess that question was aimed at me...

enter image description here

\documentclass{article}
\usepackage[leqno]{amsmath}

\renewcommand\theequation{\alph{equation}}

\newenvironment{brqalign}[1][]{%
\ifx\relax#1\relax
  \def\colwidth{}%
\else
  \def\colwidth{to \dimexpr(\linewidth/#1)-1em\relax}%
\fi
% START CODE
%%%%%%%%%%%%
% Do everything an a box, as you can not remove
% things from the main vertical list.
\setbox0\vbox\bgroup
% Allow \\ for ends of row.
\let\\\cr
% Use an \halign (I experimented with using one
% of the AMS alignments but they tend to want to 
% be full width, or already in math mode, so just
% use a primitive \halign here with two display math
% entries in its preamble.
\halign\bgroup
  \refstepcounter{equation}\theequation)\quad
  \hfil$\displaystyle##$&$\displaystyle{}##$\hfil\cr}%
{%
% END CODE
%%%%%%%%%%
% Finish the \halign.
\crcr\egroup
% Now, at this point the vertical list being assembled in
% box0 contains all the rows of the alignment these will be
% a sequence of \hboxes (which will all be forced to be the
% same width) and baselineskip glue. (While testing with the
% AMS alignments there was additional glue and penalties
% as well.
%
% We will discard all the glue and penalties but assemble
% a horizontal list of the boxes in box1.
% start by making box1 and empty hbox.
\global\setbox1\null
% Then loop backwards up the vertical list discarding any glue
% and penalties and grabbing hold of each box.
\loop
% This combination removes two adjacent glue items
% or glue followed by penalty, it may be more than needed
% but better be safe than sorry.
\unskip\unskip\unpenalty\unskip\unpenalty
% Glue and penalties we discard, but the boxes have the
% formula so remove this one from the current list and
% save it in box 4.
\setbox4\lastbox
% If there was no box box4 is void and we reached the top
% of the original vertical alignent so stop.
\ifvoid4
\else
% Otherwise Put this box4 at the front of the horizontal
% list being built in box1, followed by an em of space.
\global\setbox1\hbox{\hbox\colwidth{\box4\hfil}\hskip 1em\unhbox1}%
% Go round the loop again.
\repeat
% End the group for the working box 0.
% The horizontal list is globally assigned
% to box 1 so will be visible after this group end.
\egroup
% Now put out all the boxes flush left, the usual paragraph breaker
% will fit as many on a line as it can. As each box is the same width
% vertical alignment is automatic.
\begin{flushleft}\unhbox1 \end{flushleft}%
% Ignore white space after the end of the environment
\ignorespacesafterend
}

\begin{document}

\begin{brqalign}
  x^2 + y^2 &= 1\\
  a + b &= c\\
  r-x &= y+z \\
  f - y &= z\\
  a - b &= 2d\\
  r+x &= 2y-3z
\end{brqalign}

\begin{brqalign}[2]
  x^2 + y^2 &= 1\\
  a + b &= c\\
  r-x &= y+z \\
  f - y &= z\\
  a - b &= 2d\\
  r+x &= 2y-3z
\end{brqalign}

\begin{brqalign}[1]
  x^2 + y^2 &= 1\\
  a + b &= c\\
  r-x &= y+z \\
  f - y &= z\\
  a - b &= 2d\\
  r+x &= 2y-3z
\end{brqalign}


\end{document}
David Carlisle
  • 757,742