Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following example from http://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.

share|improve this question
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 '12 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 '12 at 10:37
have you looked at the breqn package? –  cmhughes Apr 11 '12 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 '12 at 11:38

1 Answer 1

up vote 25 down vote accepted

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

share|improve this answer
This is a really really cool solution. I was thinking about something much more complicated. This is just beautiful. –  Stephan Lehmke Apr 11 '12 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 '12 at 18:28
Comments added as requested. –  David Carlisle Apr 11 '12 at 18:59
OMG!!! sooo nice :) –  tohecz Apr 12 '12 at 11:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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