summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/mh/breqn-technotes.tex
blob: d87e2c538bad007ecb1660653b23b9fa2e23a6f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
\documentclass[twocolumn]{report}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{breqn}
\def\eqnumside{L}
\providecommand*\cs[1]{\texttt{\textbackslash#1}}

\providecommand{\qq}[1]{\textquotedblleft#1\textquotedblright}
\providecommand{\mdash}{\textemdash}
\providecommand{\ndash}{\textendash}

\newcommand{\ititle}[1]{\textit{#1}}

\newcommand{\LR}[2][.4]{%
  \framebox[#1\displaywidth]{$\displaystyle{}#2$}%
}

\newcommand{\LHS}[1]{\LR[\relifactor]{#1}}

\newdimen\relindent \newdimen\rhswd

\newcommand{\dwline}{%
  \hbox to\curdw{\vrule height1ex
    \leaders\hrule height.55ex depth-.45ex\hfil
    \tiny \space display width
    \leaders\hrule height.55ex depth-.45ex\hfil
    \vrule height1ex}%
}

\newenvironment{layout}[1][.15]{%
  \noindent
  $$\edef\curdw{\the\displaywidth}%
    \def\relifactor{#1}%
    \gdef\layoutcr{\cr}\def\\{\layoutcr}%
    \binoppenalty 10000 \relpenalty 10000
  \setbox8\vbox\bgroup
    \advance\baselineskip .35\baselineskip
    \advance\lineskip .35\baselineskip \lineskiplimit\lineskip
    \relindent=#1\displaywidth
    \rhswd=\displaywidth \advance\rhswd-\relindent
    \global\row 0 \gdef\rhsskew{}%
    \halign\bgroup \global\advance\row 1 $\hfil\displaystyle{}##$&%
      \ifnum\row>1 \rhsskew \fi $\displaystyle{}##\hfil$\cr
}{%
  \crcr\egroup\egroup
  \vcenter{\halign{\hfil##\hfil\cr
    \hbox{\hss\dwline\hss}\cr\noalign{\vskip.6\baselineskip}\box8 \cr}}%
  $$\relax
  \ignorespacesafterend
}

\newcommand{\stagger}{\omit\span\gdef\layoutcr{\cr\omit\span}}
  
\newcount\row

\newcommand{\rhsskew}{}
\newcommand{\skewleft}[1]{\gdef\rhsskew{\kern-#1\relax}}


\usepackage{hyperref}

\title{Technical notes on equation breaking}
\author{Michael J.\ Downes \and Morten H\o gholm}
\begin{document}

\maketitle

\onecolumn

\chapter{Tag placement}

The method used by the breqn package to place the equation number is
rather more complicated than you might think, and the whole reason is
to allow the number to stay properly centered on the total height even
when the height fluctuates due to stretching or shrinking of the page.


Consider the following equation: 
\begin{dmath}[number={3.15}]
  N_{0} \simeq \left( \frac{\nu}{\lVert u\rVert_{H^{i}}} \right) 
  \lvert I\rvert^{-1/2}
\end{dmath}
It will have only one line, if the column width is not too narrow. 

Scrutinizing the vertical list will shed light on some of the basic properties 
shared by all breqn equations. After that we will look at what would happen if 
two or more lines were needed. The numbers added on the left in the following 
\cs{showlists} output mark the points of interest. 
\begin{verbatim}
[1] \penalty 10000 
    \glue(\abovedisplayskip) 0.0 
    \penalty 10000 
    \glue(\belowdisplayskip) 0.0 
[2] \glue 4.0 plus 4.0 
    \glue(\lineskip) 1.0 
[3] \vbox(16.53902+0.0)x0.0, glue set 16.53902fil 
    .\glue 0.0 plus 1.0fil minus 1.0fil 
    \penalty 10000 
[4] \glue -8.51945 
[5] \hbox(7.5+2.5)x25.55563 
    .\OT1/cmr/m/n/10 ( 
    .\OT1/cmr/m/n/10 3 
    .\OT1/cmr/m/n/10 . 
    .\OT1/cmr/m/n/10 1 
    .\OT1/cmr/m/n/10 5 
    .\kern 0.0 
    .\OT1/cmr/m/n/10 ) 
    \penalty 10000 
[6] \glue(\parskip) -18.01956 
[7] \hbox(16.53902+9.50012)x360.0, glue set 1.78647 
\end{verbatim}
\begin{enumerate}
\item These four lines are a hidden display structure from \TeX's
  primitive \texttt{\$\$} mechanism. It is used only to get the value
  of \cs{predisplaysize} so that we can later calculate by hand
  whether to use the short display skips or the regular ones. (The
  reason that we have to do it by hand traces back to the fact that
  \TeX\ 3.x does not allow unhboxing in math mode.) The penalties come
  from \cs{predisplaypenalty} and \cs{postdisplaypenalty}, which were
  locally set to 10000 to ensure there would be no unintended page
  breaks at these glue nodes.

\item These two glue nodes are the ones that would normally have been
  produced at the top of a display; the first one is the above-display
  skip node (though we had to put it in by hand with \cs{vskip}) and
  the second one is the usual baselineskip/lineskip node.

\item This is a dummy copy of the equation's first line, which is
  thrown in here to get the proper value of baselineskip (or lineskip
  in this case). Why do we need this? Because this ensures that we get
  the top spacing right before we fiddle with the glue nodes
  surrounding the equation number. And if the equation has a frame,
  this box is a good place to add it from.

\item This is a special glue node that brings us to the right vertical
  position for adding the equation number. Its value is calculated
  from the variables that you would expect, given the presence of the
  dummy first line above the num- ber: starting position of the
  equation, height of first line, total height of equation body. If
  the equation body had more than one line, with stretchable glue
  between the lines, half of the stretch would be added in this glue
  node.

\item The hbox containing the equation number.

\item Backspace to bring the equation body to the right starting point. We use 
  \cs{parskip} to put this glue in place because we're going to get a
  parskip node here in any case when we add the equation body with (in
  essence). If we didn't do this we'd get two glue nodes instead of
  one, to no purpose.

\cs{\noindent} \cs{unhbox}\cs{EQ@box}.


\item And lastly we see here the first line of the equation body,
  which appears to have height 16.5pt and depth 9.5pt.
\end{enumerate}

For comparison, the vertical list produced from the above equation in
standard \LaTeX\ would look like this, if the same values of
columnwidth and abovedisplayskip are used:
\begin{verbatim}
[1] \penalty 10000 
[2] \glue(\abovedisplayskip) 4.0 plus 4.0 
    \glue(\lineskip) 1.0 
    \hbox(16.53902+9.50012)x232.94844 
[3] .\hbox(7.5+2.5)x25.55563 
    ..\hbox(7.5+2.5)x25.55563 
    ...\OT1/cmr/m/n/10 ( 
    ...\OT1/cmr/m/n/10 3 
    ...\OT1/cmr/m/n/10 . 
    ...\OT1/cmr/m/n/10 1 
    ...\OT1/cmr/m/n/10 5 
    ...\kern 0.0 
    ...\OT1/cmr/m/n/10 ) 
    .\kern101.49591 
[4] .\hbox(16.53902+9.50012)x105.8969 
    ... 
[5] \penalty 0 
[6] \glue(\belowdisplayskip) 4.0 plus 4.0 
    \glue(\lineskip) 1.0 
    \hbox(6.94444+1.94444)x345.0, glue set 62.1106fil
\end{verbatim}

\begin{enumerate}
  \item \cs{predisplaypenalty}
  \item \cs{abovedisplayskip} 
  \item  equation number box
  \item equation body 
  \item \cs{postdisplaypenaltly}  
  \item \cs{belowdisplayskip}
\end{enumerate}


\twocolumn
\chapter{Equation Layouts}

\section{Misc examples}

Let us consider which of these have 50\% or more of wasted whitespace
\emph{within the bounding box of the visible material}.
\begin{layout}[.4]
\LHS{L}&=\LR[.35]{R_{1}}\\
&=\LR[.25]{R_{1}}
\end{layout}

\section{Ladder and step layouts}

\subsection{Straight ladder layout}
This is distinguished by a relatively short LHS and one or more RHS's of
any length.
\begin{layout}
\LHS{L} &= \LR[.5]{R_{1}}\\
&=\LR[.3]{R_{2}}\\
&=\LR[.25]{R_{3}}\\
&\qquad\ldots
\end{layout}
The simplest kind of equation that fits on one line and has only one RHS
may be viewed as a trivial subcase of the straight ladder layout:
\begin{layout}
\LHS{L} &= \LR[.5]{R}
\end{layout}
If some of the RHS's are too wide to fit on a single line they may be
broken at binary operator symbols such as plus or minus. This is still
classified as a straight ladder layout if none of the fragments intrude
into the LHS column, because the underlying parshape is the same.
\begin{layout}
\LHS{L} &= \LR[.4]{R_{1a}}\\
&\quad +\LR[.6]{R_{1b}}\\
&=\LR[.3]{R_{2}}\\
&=\LR[.25]{R_{3a}}\\
&\quad +\LR[.45]{R_{3b}}\\
&\quad +\LR[.54]{R_{3c}}\\
&\qquad\ldots
\end{layout}

\subsection{Skew ladder layout}
\begin{layout}[.5]
\skewleft{.35\displaywidth}
\LHS{L}&= \LR[.3]{R_{1}}\\
&=\LR[.6]{R_{2}}\\
&=\LR[.25]{R_{3}}\\
&\qquad\ldots
\end{layout}
In a skew ladder layout, the combined LHS width plus width of $R_{1}$
does not exceed the available width, but one of the other RHS's is so
wide that aligning its relation symbol with the others cannot be done
without making it run over the right margin: $\mbox{width}(L) +
\mbox{width}_{\mathrm{max}}(R_{i})>\mbox{width}_{\mathrm{avail}}$. In
that case we next try aligning all but the first relation symbol,
allowing all the $R_{i}$ after $R_1$ to shift leftward.

\subsection{Drop ladder layout}
\begin{layout}[.6]
\makebox[.15\displaywidth][l]{\LHS{L}}\\
&= \LR[.6]{R_{1}}\\
&=\LR[.3]{R_{2}}\\
&=\LR[.25]{R_{3}}\\
&\qquad\ldots
\end{layout}
The drop ladder layout is similar to the skew ladder layout but with the
width of $R_1$ too large for it to fit on the same line as the LHS. Then
we move $R_1$ down to a separate line and try again to align all the
relation symbols. Note that this layout consumes more vertical space
than the skew ladder layout.

\subsection{Step layout}
\begin{layout}[.6]
\stagger
\LHS{R_{a}}\\
\qquad + \LR[.7]{R_{b}}\\
\qquad\qquad + \LR[.6]{R_{c}}\\
\qquad\qquad\qquad + \LR[.45]{R_{d}}\\
\qquad\qquad\qquad\qquad\ldots
\end{layout}
The chief characteristic of the step layout is that there is no relation
symbol, so that the available line breaks are (usually) all at binary
operator symbols. Let $w_1$ and $w_l$ be the widths of the first and
last fragments. We postulate that the ideal presentation is as follows:
Choose a small stairstep indent $I$ (let's say 1 or 2 em). We want the
last fragment to be offset at least $I$ from the start of the first
fragment, and to end at least $I$ past the end of the first fragment. If
there are only two lines these requirements determine a target width
$w_T=\max(w_1+I,w_l+I)$. If there are more than two lines ($l>2$) then
use $w_T = \max(w_1 + (l-1)I, w_l+I, w_{\mathrm{avail}}$ and reset $I$
to $w_T/(l-1)$ if $w_T = w_{\mathrm{avail}}$.

Furthermore, we would like the material to be distributed as evenly as
possible over all the lines rather than leave the last line exceedingly
short. If the total width is $1.1(\mbox{width}_{\mathrm{avail}})$, we
don't want to have .9 of that on line 1 and .2 of it on line 2:
\begin{layout}[.9]
\stagger
\LHS{R_{a}\hfil+\hfil R_{b}\hfil+\hfil R_{c}}\\
\qquad + \LR[.1]{R_{d}}
\end{layout}
Better to split it as evenly as possible, if the available breakpoints
permit.
\begin{layout}[.5]
\stagger
\LHS{R_{a}\hfil+\hfil R_{b}}\\
\qquad + \LR[.5]{R_{c}\hfil+\hfil R_d}
\end{layout}
A degenerate step layout may arise if an unbreakable fragment of
the equation is so wide that indenting it to its appointed starting
point would cause it to run over the right margin. In that case, we want
to shift the fragment leftward just enough to bring it within the right
margin:
\begin{layout}[.6]
\stagger
\LHS{L_{a}}\\
\quad + \LR[.8]{L_{b}}\\
\qquad + \LR[.7]{L_{c}}\\
\; + \LR[.87]{L_{d}}\\
\qquad\ldots
\end{layout}
And then we may want to regularize the indents as in the drop ladder
layout. Let's call this a dropped step layout:
\begin{layout}[.6]
\stagger
\LHS{L_{a}}\\
\quad + \LR[.8]{L_{b}}\\
\quad + \LR[.7]{L_{c}}\\
\quad + \LR[.87]{L_{d}}\\
\qquad\ldots
\end{layout}

\section{Strategy}

Here is the basic procedure for deciding which equation layout to use,
before complications like equation numbers and delimiter clearance come
into the picture. Let $A$ be the available width, $w_{\mathrm{total}}$
the total width of the equation contents, $w(L)$ the width of the
left-hand side, $w_{\max}(R)$ the max width of the right-hand sides, $I$
the standard indent for step layout, and $O$ the standard offset for
binary operators if a break occurs in the middle of an RHS. Also let
$t_L$ and $t_R$ represent certain thresholds for the width of the LHS or
the RHS at which a layout decision may change, as explained below.

\newpage
\begin{small}
\begin{enumerate}
\renewcommand{\labelenumi}{(\theenumi)}
\item \ititle{Does everything fit on one line?}\label{i:LR}
  $w_{\mathrm{total}}\leq A$?

Yes: print the equation on a single line (done).

No: Check whether the equation has both LHS and RHS (\ref{i:lhs-check}).

\item \ititle{Is there a left-hand side?}\label{i:lhs-check}
Are there any relation symbols in the equation?

Yes: Try a ladder layout (\ref{i:ladder}).

No: Try a step layout (\ref{i:step}).

\item\ititle{Does the LHS leave room to fit the widest RHS?}\label{i:ladder}
  $w(L)+w_{\max}(R)<A$?

Yes: Use a straight ladder layout (\ref{i:straight-ladder}).

No: Check the width of the LHS (\ref{i:check-lhs}).

\item\ititle{Is the LHS relatively short?}\label{i:check-lhs}
$w(L)\leq t_L$? (where $t_L$ is typically $0.4A$).

Yes: Subdividing one or more of the RHS's may permit us to use a
straight ladder layout (\ref{i:straight-ladder}).

No: The straight ladder layout is unlikely to work.
Try a skew or drop ladder layout (\ref{i:skew-drop}).

\item\ititle{Straight ladder layout}\label{i:straight-ladder}
Set up a straight ladder parshape [0pt $A$ $w(L)$ $A-w(L)$] and run
a trial break. If the combined width of the LHS plus the longest RHS is
no greater than $A$ then we should get a satisfactory layout with all
line breaks occurring at major division points (relation symbols).
Otherwise, we hope, some additional line breaks at minor division points
will allow everything to fit within the text column.

\ititle{Line breaks OK?}

\begingroup \hbadness=9999
Yes: The straight ladder layout succeeded
  (done).\par\endgroup

No: Try a skew or drop ladder layout (\ref{i:skew-drop}).

\item\ititle{Do the LHS and the first RHS fit on one
    line?}\label{i:skew-drop} $w(L)+w(R_1) \leq A$?

Yes: Try a skew ladder layout (\ref{i:skew}).

No: Try a drop ladder layout (\ref{i:drop}).

\item\ititle{Skew ladder layout}\label{i:skew}
Set up a parshape [0pt $A$ $I$ $A-I$] and run a trial break.

\ititle{Line breaks OK?}

Yes: Skew ladder layout succeeded (done).

No: One of the unbreakable fragments of the $R_i$ ($i>1$) is wider than
$A-I$; try an almost-columnar layout (\ref{i:almost-columnar}).

\item\ititle{Drop ladder layout}\label{i:drop}
Set up a parshape [0pt $w(L)$ $I$ $A-I$] and run a trial break.
This is the same parshape as for a skew ladder layout except that the
width of the first line is limited to the LHS width, so that the RHS is
forced to drop down to the next line.

\ititle{Line breaks OK?}

Yes: Drop ladder layout succeeded (done).

No: One of the unbreakable fragments of the $R_i$ ($i>1$) is wider than
$A-I$; try an almost-columnar layout (\ref{i:almost-columnar}).

\item\ititle{Almost-columnar layout}\label{i:almost-columnar}
This presupposes a trial break that yielded a series of expressions or
fragments, one per line. Let $w(F)$ denote the width of the first
fragment and $w(R_i)$ the widths of the remaining fragments.
Set up a parshape [0pt $w(F)$ $A-w_{\max}(R_i)$ $w_{\max}(R_i)$]: in other
words, set the first line flush left and the longest line flush right
and all other lines indented to the same position as the longest line.
But as a matter of fact there is one other refinement for extreme cases:
if $w_{\max}(R_i)>A$ then the parshape can be simplified without loss to
[0pt $w(F)$ 0pt $A$]\mdash for that is the net effect of substituting
$\min(A,w_{\max})$ in stead of $w_{\max}$.
(Done.)

\item\ititle{Step layout}\label{i:step} Set target width $w_T$ to $A -
  2I$.  Set parshape to [0pt $w_T$ $I$ $w_T -I$ $2I$ $w_T -2I$ \ldots\ 
  $(l-1)I$ $w_T - (l-1)I$], where $l=\lceil w_{\mathrm{total}}/A\rceil$
  is the expected number of lines that will be required.  Trial break
  with that parshape in order to find out the width of the last line.

\ititle{Indents OK?}

Yes: Step layout succeeded (done).

No: One of the fragments is too wide to fit in
the allotted line width, after subtracting the indent specified by the
parshape. Try a dropped step layout (\ref{i:drop-step})

\item\ititle{Dropped step layout}\label{i:drop-step} Set up a parshape
  [0pt $A$ $I$ $A-I$] and run a trial break.  Note that this is actually
  the same parshape as for a skew ladder layout.

\ititle{Line breaks OK?}

Yes: Dropped step layout succeeded (done).

No: One of the unbreakable fragments of the $R_i$ ($i>1$) is wider than
$A-I$; as a last resort try an almost-columnar layout (\ref{i:almost-columnar}).

\end{enumerate}
\par\end{small}


\end{document}