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

  \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


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.

  • 1
    Is it really a good idea ? I often have to break some long equations in my LaTeX code, so I prefer to indicate the break via `\`.
    – projetmbc
    Apr 11, 2012 at 10:31
  • @projetmbc My question is not about how to break long equations, it's about how to automatically break such things like a list of simple math exercises as in my example in my post (and get the aligment as above)
    – student
    Apr 11, 2012 at 10:37
  • 1
    have you looked at the breqn package?
    – cmhughes
    Apr 11, 2012 at 10:41
  • @cmhughes Thanks, but I don't see, how to use breqn for this. I guess I should be able to specify somehow where it is allowed to break (I don't want to break up a unit like \num&& x^2 + y^2 &= 1). I don't see as well how to use breqn to get the aligment right
    – student
    Apr 11, 2012 at 11:38

1 Answer 1


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

enter image description here



% Do everything an a box, as you can not remove
% things from the main vertical list.
% Allow \\ for ends of row.
% 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.
% Finish the \halign.
% 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.
% Then loop backwards up the vertical list discarding any glue
% and penalties and grabbing hold of each box.
% This combination removes two adjacent glue items
% or glue followed by penalty, it may be more than needed
% but better be safe than sorry.
% 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.
% If there was no box box4 is void and we reached the top
% of the original vertical alignent so stop.
% 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.
% 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.
% 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


  x^2 + y^2 &= 1\\
  a + b &= c\\
  r-x &= y+z \\
  f - y &= z\\
  a - b &= 2d\\
  r+x &= 2y-3z

  • 5
    This is a really really cool solution. I was thinking about something much more complicated. This is just beautiful. Apr 11, 2012 at 16:34
  • Thanks, this is great. However the code seems to be a bit above my current LaTeX level. So it would be nice if you could add some comments to your code which makes it easier to understand and modify. This would be also helpful for others interested in your solution.
    – student
    Apr 11, 2012 at 18:28
  • 3
    Comments added as requested. Apr 11, 2012 at 18:59
  • OMG!!! sooo nice :)
    – yo'
    Apr 12, 2012 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.