3

I want to write chains of equations like ugly formula = expanded = moving things around = simplified = nice formula and to have linebreaks in space-efficient places regardless of margin size and the number of columns. I feel like the formulas shouldn't be broken up so that the reader can parse them without moving their eyes all the time. I was looking around and stumbled upon breqn, which seems to be powerful enough but somehow not willing to fulfill my wishes. It linebreaks on any =, for example

\documentclass{article}
\usepackage{breqn}
\begin{document}
\begin{dmath*}
a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a
\end{dmath*}
\end{document}

will make a tower of =as. The =s can be substituted with \hiderel{=}s but then it never linebreaks. I thought for a moment that breqn is just a scam and only changes the default between "linebreak" and "no linebreak", but then I discovered that something like \begin{dmath*} a+a+...\end{dmath*} actually produces what I'd want - it tries to fit as many as in a row as possible, but if there's too much of them moves them to the next line. Could it do the same for me but with =s?

acupoftea
  • 145
  • @Schrödinger'scat is this ok? What do you mean by "what you have tried", I've only really tried googling, I have no idea how to do anything with the deep arcane tex stuff. – acupoftea Dec 25 '19 at 03:30
  • 2
    I must confess to possessing no enthusiasm for helping anybody typeset a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a -- with "linebreaks in space-efficient places" or in any other manner. Might you be willing to provide a slightly more realistic use case? Oh, and why did you think, even for a moment, that "that breqn is just a scam"? – Mico Dec 25 '19 at 05:14
  • 1
    @Mico I could, but I don't see how that would help - I don't want to just typeset a specific math expression, I want to find a general, automatic solution for the problem described at the beginning of my post. The a=a=... was just an example to illustrate how breqn works. I thought it actually can't automatically break lines, but it can, I just don't know how to make it do that at =. – acupoftea Dec 25 '19 at 16:52

1 Answers1

3

You can use inline formulas, that are broken into lines:

\documentclass{article}
\usepackage{amsmath,varwidth}

\newenvironment{breakmath}[1][0.8]
 {\begin{equation*}\begin{varwidth}{#1\columnwidth}\raggedright$\displaystyle}
 {$\end{varwidth}\end{equation*}}

\begin{document}

\begin{breakmath}
a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a
\end{breakmath}

\end{document}

The optional parameter specifies the maximum width allowed, default 80% of the column width. So, if you call \begin{breakmath}[0.7] you get maximum width 70%.

enter image description here

If you prefer equals signs at the beginning of the line, you can do as follows:

\documentclass{article}
\usepackage{amsmath,varwidth}

\newenvironment{breakmath}[1][0.8\columnwidth]
 {\begin{equation*}\begin{varwidth}{#1}\raggedright\makebreakequals$\displaystyle}
 {$\end{varwidth}\end{equation*}}

\newcommand{\breakequals}{%
  \penalty\relpenalty\stdequals\nobreak
}
\newcommand{\makebreakequals}{%
  \edef\stdequals{\mathchar\the\mathcode`=\relax}%
  \begingroup\lccode`~=`=\lowercase{\endgroup\let~}\breakequals
  \mathcode`=="8000
}

\begin{document}

\begin{breakmath}
a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a=a
\end{breakmath}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Such a nice suggest...XMAS wishes – MadyYuvi Dec 25 '19 at 10:28
  • Thanks! I noticed that when the formulas are high (like with fractions and power towers), the vertical space between them is very thin. Is there a way to increase it? – acupoftea Dec 25 '19 at 17:29
  • @acupoftea You can add \linespread{1.2} or something like that before \raggedright – egreg Dec 25 '19 at 17:49
  • @egreg Thanks, the "something like that" that seems to work is \lineskip=9\lineskip – acupoftea Dec 25 '19 at 18:47
  • @egreg yet another wish, is it possible to encourage it to place the = signs at the beginnings of lines instead of at the ends? I tried =~ and =\linebreak instead of = and also \mbox{$=a$} but then it doesn't break at all. – acupoftea Dec 25 '19 at 19:22
  • @acupoftea Added. – egreg Dec 25 '19 at 19:42