[Tex/LaTex] Automatic line break in alignat

alignautomationbreqnline-breaking

Consider the following example from https://tex.stackexchange.com/a/12782/4011

\documentclass{article}
\usepackage{amsmath}
\newcounter{eqn}
\renewcommand*{\theeqn}{\alph{eqn})}
\newcommand{\num}{\refstepcounter{eqn}\text{\theeqn}\quad}
\begin{document}
\begin{alignat*}{6}
  \num&& x^2 + y^2 &= 1 \qquad& \num&& a + b &= c
          &  \num&& r-x &= y+z \\
  \num&\quad& f - y &= z      & \num&& a - b &= 2d
    \qquad&  \num&& r+x &= 2y-3z
\end{alignat*}
\end{document}

output

Here one has (as usual) to specify where to break the line via \\. However is there a way to do the linebreak automatically, such that you give the space between the equations and TeX automatically decides how much equations fit (under the constraint of the given spaces) into the fullest row (say n) and breaks the lines so that you get n equations in each row alignet like in the example above.

Best Answer

You can use a vertical alignment to get the measuring done and then re-flow them in a horizontal layout:

enter image description here

\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}
Related Question