Debian Codegraph @

texmf-dist/source/latex/autobreak/autobreak.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2016-2017 by Takahiro Ueda <[email protected]>
4 %
5 % This file may be distributed and/or modified under the conditions of
6 % the LaTeX Project Public License, either version 1.3 of this license
7 % or (at your option) any later version. The latest version of this
8 % license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.3 or later is part of all distributions of LaTeX
13 % version 2005/12/01 or later.
14 %
15 % To typeset this document, type the following commands:
16 % latex autobreak.dtx
17 % makeindex -s gind.ist autobreak.idx
18 % makeindex -s gglo.ist -o autobreak.gls autobreak.glo
19 % latex autobreak.dtx
20 % latex autobreak.dtx
21 %
22 %<*ignore>
23 \def\nameofplainTeX{plain}
24 \ifx\fmtname\nameofplainTeX\else
25 \expandafter\begingroup
26 \fi
27 %</ignore>
28 %<*install>
29
30 \input docstrip.tex
31 \keepsilent
32 \askforoverwritefalse
33 \preamble
34
35 This is a generated file.
36
37 Copyright (C) 2016-2017 by Takahiro Ueda <[email protected]>
38
39 This file may be distributed and/or modified under the conditions of
40 the LaTeX Project Public License, either version 1.3 of this license
41 or (at your option) any later version. The latest version of this
42 license is in:
43
44 http://www.latex-project.org/lppl.txt
45
46 and version 1.3 or later is part of all distributions of LaTeX
47 version 2005/12/01 or later.
48
49 \endpreamble
50
51 \generate{
52 \usedir{tex/latex/autobreak}
53 \file{\jobname.sty}{\from{\jobname.dtx}{package}}
54 }
55
56 %</install>
57 %<*ignore>
58 \iffalse
59 %</ignore>
60 %<*install>
61
62 \obeyspaces
63 \Msg{*************************************************************}
64 \Msg{* *}
65 \Msg{* To finish the installation you have to move the following *}
66 \Msg{* file into a directory searched by TeX: *}
67 \Msg{* *}
68 \Msg{* autobreak.sty *}
69 \Msg{* *}
70 \Msg{* To produce the documentation run the file autobreak.dtx *}
71 \Msg{* through LaTeX. *}
72 \Msg{* *}
73 \Msg{* Happy TeXing! *}
74 \Msg{* *}
75 \Msg{*************************************************************}
76
77 \endbatchfile
78
79 %</install>
80 %<*ignore>
81 \fi
82 \generate{
83 \file{\jobname.ins}{\from{\jobname.dtx}{install}}
84 }
85 \ifx\fmtname\nameofplainTeX
86 \expandafter\endbatchfile
87 \else
88 \expandafter\endgroup
89 \fi
90 %</ignore>
91 %<*driver>
92 \documentclass{ltxdoc}
93 \usepackage[T1]{fontenc}
94 \usepackage{lmodern}
95 \usepackage{color}
96 \usepackage{hypdoc}
97 \usepackage{amsmath}
98 \usepackage{autobreak}
99 \EnableCrossrefs
100 \CodelineIndex
101 \RecordChanges
102
103 \allowdisplaybreaks
104
105 % The default value of IndexColumns=3 never fits in pages.
106 \setcounter{IndexColumns}{2}
107
108 \definecolor{hilite}{rgb}{0.2,0.4,0.7}
109 \makeatletter
110 \renewcommand{\HDorg@theCodelineNo}{%
111 \textcolor{hilite}{\rmfamily\scriptsize\arabic{CodelineNo}}}
112 \makeatother
113
114 \providecommand\env[1]{\texttt{#1}}
115 \providecommand\pkg[1]{\textsf{#1}}
116
117 \usepackage{fancyvrb}
118
119 \edef\examplefilename{\jobname.tmp}
120
121 \newenvironment{example}{%
122 \VerbatimEnvironment
123 \begin{VerbatimOut}[gobble=2]{\examplefilename}%
124 }{%
125 \end{VerbatimOut}%
126 \smallskip
127 \noindent
128 \begin{list}{}{}\item
129 \begin{minipage}[t]{0.45\linewidth}%
130 \VerbatimInput{\examplefilename}%
131 \end{minipage}%
132 \begin{minipage}[t]{0.5\linewidth}%
133 \input{\examplefilename}%
134 \end{minipage}%
135 \end{list}%
136 \smallskip
137 \noindent
138 }
139
140 \newenvironment{example*}{%
141 \VerbatimEnvironment
142 \begin{VerbatimOut}[gobble=2]{\examplefilename}%
143 }{%
144 \end{VerbatimOut}%
145 \smallskip
146 \noindent
147 \begin{list}{}{}\item
148 \begin{minipage}[t]{0.95\linewidth}%
149 \VerbatimInput{\examplefilename}%
150 \end{minipage}%
151 \end{list}%
152 \smallskip
153 \noindent
154 }
155
156 \newenvironment{example**}{%
157 \VerbatimEnvironment
158 \begin{VerbatimOut}[gobble=2]{\examplefilename}%
159 }{%
160 \end{VerbatimOut}%
161 \smallskip
162 \noindent
163 \begin{list}{}{}\item
164 \begin{minipage}[t]{0.95\linewidth}%
165 \VerbatimInput{\examplefilename}%
166 \end{minipage} \\
167 \begin{minipage}[t]{0.95\linewidth}%
168 \input{\examplefilename}%
169 \end{minipage}%
170 \end{list}%
171 \smallskip
172 \noindent
173 }
174
175 % decl from ltxguide[2001/05/28]
176 \newenvironment{decl}[1][]%
177 {\par\small\addvspace{4.5ex plus 1ex}%
178 \vskip -\parskip
179 \ifx\relax#1\relax
180 \def\@decl@date{}%
181 \else
182 \def\@decl@date{\NEWfeature{#1}}%
183 \fi
184 \noindent\hspace{-\leftmargini}%
185 \begin{tabular}{|l|}\hline\ignorespaces}%
186 {\\\hline\end{tabular}\nobreak\@decl@date\par\nobreak
187 \vspace{2.3ex}\vskip -\parskip}
188
189 \newcommand\NumberedBox[1]{%
190 \, \framebox[25pt]{#1} \,%
191 }
192
193 \newcommand\LongerNumberedBox[1]{%
194 \, \framebox[30pt]{#1} \,%
195 }
196
197 \begin{document}
198 \DocInput{\jobname.dtx}
199 \end{document}
200 %</driver>
201 % \fi
202 %
203 % \CheckSum{409}
204 %
205 % \CharacterTable
206 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
207 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
208 % Digits \0\1\2\3\4\5\6\7\8\9
209 % Exclamation \! Double quote \" Hash (number) \#
210 % Dollar \$ Percent \% Ampersand \&
211 % Acute accent \' Left paren \( Right paren \)
212 % Asterisk \* Plus \+ Comma \,
213 % Minus \- Point \. Solidus \/
214 % Colon \: Semicolon \; Less than \<
215 % Equals \= Greater than \> Question mark \?
216 % Commercial at \@ Left bracket \[ Backslash \\
217 % Right bracket \] Circumflex \^ Underscore \_
218 % Grave accent \` Left brace \{ Vertical bar \|
219 % Right brace \} Tilde \~}
220 %
221 % \GetFileInfo{\jobname.sty}
222 %
223 % \title{The \pkg{autobreak} package\thanks{This document corresponds
224 % to \pkg{autobreak}~\fileversion, dated \filedate.}}
225 % \author{Takahiro Ueda}
226 %
227 % \date{23 February 2017}
228 %
229 % \maketitle
230 %
231 % \begin{abstract}
232 % This package implements a simple mechanism of line/page breaking
233 % within the \env{align} environment of the \pkg{amsmath} package; new
234 % line characters are considered as possible candidates for the breaks
235 % and the package tries to put breaks at adequate places.
236 % It is suitable for computer-generated long formulae with many terms.
237 % \end{abstract}
238 %
239 % \changes{v0.1}{2016/06/03}{Initial version}
240 %
241 % \tableofcontents
242 %
243 % \section{Introduction}
244 %
245 % Sometimes people want to put long formulae in their documents, which
246 % do not fit in a line and may span over multiple pages.
247 % The following is an equation of explicitly writing down the first 50
248 % terms in the sum of the well-known Basel problem:
249 % \begin{align}
250 % \begin{autobreak}
251 % \zeta(2) =
252 % 1
253 % + \frac{1}{4}
254 % + \frac{1}{9}
255 % + \frac{1}{16}
256 % + \frac{1}{25}
257 % + \frac{1}{36}
258 % + \frac{1}{49}
259 % + \frac{1}{64}
260 % + \frac{1}{81}
261 % + \frac{1}{100}
262 % + \frac{1}{121}
263 % + \frac{1}{144}
264 % + \frac{1}{169}
265 % + \frac{1}{196}
266 % + \frac{1}{225}
267 % + \frac{1}{256}
268 % + \frac{1}{289}
269 % + \frac{1}{324}
270 % + \frac{1}{361}
271 % + \frac{1}{400}
272 % + \frac{1}{441}
273 % + \frac{1}{484}
274 % + \frac{1}{529}
275 % + \frac{1}{576}
276 % + \frac{1}{625}
277 % + \frac{1}{676}
278 % + \frac{1}{729}
279 % + \frac{1}{784}
280 % + \frac{1}{841}
281 % + \frac{1}{900}
282 % + \frac{1}{961}
283 % + \frac{1}{1024}
284 % + \frac{1}{1089}
285 % + \frac{1}{1156}
286 % + \frac{1}{1225}
287 % + \frac{1}{1296}
288 % + \frac{1}{1369}
289 % + \frac{1}{1444}
290 % + \frac{1}{1521}
291 % + \frac{1}{1600}
292 % + \frac{1}{1681}
293 % + \frac{1}{1764}
294 % + \frac{1}{1849}
295 % + \frac{1}{1936}
296 % + \frac{1}{2025}
297 % + \frac{1}{2116}
298 % + \frac{1}{2209}
299 % + \frac{1}{2304}
300 % + \frac{1}{2401}
301 % + \frac{1}{2500}
302 % + \dots
303 % \end{autobreak}
304 % \end{align}
305 % The above example might seem nonsense, but putting long formulae may
306 % have a meaning in some cases and become inevitable for completeness of
307 % documents, writing self-contained papers, or just to impress readers.
308 % They are typically generated as outputs of computer algebra systems,
309 % and would have the form of a sum of many terms while each term is
310 % short.
311 %
312 % Then, the question is how to break long formulae in such a way that
313 % the expressions do not make any overfull lines for \LaTeX{}.
314 % Certainly, one can attempt to manually insert line breaks by trial and
315 % error, checking whether \LaTeX{} warns overfull lines, and this
316 % process could be automatized by external scripts at some extent.
317 % A shortcoming of such `manual' approaches is that line breaks have to
318 % be reexamined whenever the layout of the document is changed, e.g.,
319 % replacing the document class or reusing existing equations into
320 % another document with a different format.
321 %
322 % The goal of the \pkg{autobreak} package is to give a reasonably simple
323 % solution for (semi-)automatic line breaking of long formulae within
324 % \LaTeX{}^^A
325 % \footnote{^^A
326 % There is another package \pkg{breqn}
327 % (\url{https://www.ctan.org/pkg/breqn}), which adopts a more
328 % automatic fashion and is useful for more sophisticated line
329 % breaking, unless you get ``Dimension too large'' error for really
330 % big expressions.
331 % }.
332 %
333 % \section{Usage}
334 %
335 % The \pkg{autobreak} package is supposed to be used together with the
336 % \pkg{amsmath} package^^A
337 % \footnote{^^A
338 % \url{https://www.ctan.org/pkg/amsmath}.
339 % }^^A
340 % \footnote{^^A
341 % Actually \pkg{autobreak} internally loads \pkg{amsmath}, but it is
342 % still a good practice to explicitly include all packages providing
343 % macros used in your document.
344 % }:
345 % \begin{example*}
346 % \usepackage{amsmath}
347 % \usepackage{autobreak}
348 % \end{example*}
349 % When your document contains long equations over multiple pages,
350 % you might want to use \cs{allowdisplaybreaks} of \pkg{amsmath}
351 % package:
352 % \begin{example*}
353 % \allowdisplaybreaks
354 % \end{example*}
355 %
356 % \begin{decl}
357 % |\begin{autobreak}| \\
358 % \meta{long-equations} \\
359 % |\end{autobreak}|
360 % \end{decl}
361 % \SpecialEnvIndex{autobreak}
362 % The \env{autobreak} environment is used for breaking lines in long
363 % formulae in the \env{align} environment of \pkg{amsmath}^^A
364 % \footnote{^^A
365 % Technically, \env{align} (with \cs{notag} to suppress equation
366 % numbers except the last line) is the only option we can use for
367 % page-break aligned equations within \pkg{amsmath} because
368 % \env{split}, \env{gathered}, \env{aligned} and \env{alginedat} do
369 % not allow page breaking.
370 % \env{dmath} of \env{breqn} with \cs{eqinterlinepenalty}\texttt{=0}
371 % allows page breaking, but may fail to find a reasonable tag place.
372 % }.
373 % \begin{example}
374 % \begin{align}
375 % \begin{autobreak}
376 % \zeta(3) =
377 % 1
378 % + \frac{1}{8}
379 % + \frac{1}{27}
380 % + \frac{1}{64}
381 % + \frac{1}{125}
382 % + \frac{1}{216}
383 % + \frac{1}{343}
384 % + \frac{1}{512}
385 % + \frac{1}{729}
386 % + \frac{1}{1000}
387 % + \frac{1}{1331}
388 % + \frac{1}{1728}
389 % + \frac{1}{2197}
390 % + \frac{1}{2744}
391 % + \frac{1}{3375}
392 % + \frac{1}{4096}
393 % + \frac{1}{4913}
394 % + \frac{1}{5832}
395 % + \frac{1}{6859}
396 % + \frac{1}{8000}
397 % + \dots
398 % \end{autobreak}
399 % \end{align}
400 % \end{example}
401 % The magic happens from the simple fact that \env{autobreak} interprets
402 % all new line characters appearing between |\begin{autobreak}| and
403 % |\end{autobreak}| as \textit{breakable points}, at which any line
404 % breaks can be logically inserted. To be more exact, the first
405 % non-empty block, separated from the rest by a new line character,
406 % determines the indentation of the successive lines. Then
407 % \env{autobreak} tries to fill the line with the rest of the blocks,
408 % and puts a line break when they do not fit in a line. This is
409 % clarified by the following example:
410 % \begin{example}
411 % \begin{align}
412 % \begin{autobreak}
413 % \NumberedBox{1}
414 % \NumberedBox{2}
415 % \NumberedBox{3}
416 % \NumberedBox{4}
417 % \NumberedBox{5}
418 % \NumberedBox{6}
419 % \NumberedBox{7}
420 % \NumberedBox{8}
421 % \NumberedBox{9}
422 % \NumberedBox{10}
423 % \NumberedBox{11}
424 % \end{autobreak}
425 % \end{align}
426 % \end{example}
427 %
428 % It is also possible to put more than one \env{autobreak} in one
429 % \env{align}:
430 % \begin{example}
431 % \begin{align}
432 % \begin{autobreak}
433 % \NumberedBox{1} =
434 % \NumberedBox{2}
435 % + \NumberedBox{3}
436 % + \NumberedBox{4}
437 % + \NumberedBox{5}
438 % + \NumberedBox{6}
439 % + \NumberedBox{7}
440 % + \NumberedBox{8}
441 % + \NumberedBox{9}
442 % + \NumberedBox{10} ,
443 % \end{autobreak}
444 % \\
445 % \begin{autobreak}
446 % \LongerNumberedBox{1} =
447 % \NumberedBox{2}
448 % + \NumberedBox{3}
449 % + \NumberedBox{4}
450 % + \NumberedBox{5}
451 % + \NumberedBox{6}
452 % + \NumberedBox{7}
453 % + \NumberedBox{8}
454 % + \NumberedBox{9}
455 % + \NumberedBox{10} .
456 % \end{autobreak}
457 % \end{align}
458 % \end{example}
459 % For a technical reason, it often requires more than one run of
460 % \LaTeX{}, and in such cases one will get informed by the following
461 % warning:
462 % \begin{example*}
463 % Package autobreak Warning: Layout may have changed.
464 % (autobreak) Rerun to get layout correct.
465 % \end{example*}
466 % In the next run, the layout of the equations will be corrected.
467 %
468 % \begin{decl}
469 % |\MoveEqLeft|\oarg{number}
470 % \end{decl}
471 % This command is desinged to work like \cs{MoveEqLeft} of
472 % the \pkg{mathtools} package^^A
473 % \footnote{^^A
474 % \url{https://www.ctan.org/pkg/mathtools}.
475 % }. If it is put at the beginning of an \env{autobreak} environment,
476 % then all subsequent lines after the first line are indented by 2 em
477 % (the default value).
478 % \begin{example}
479 % \begin{align}
480 % \begin{autobreak}
481 % \MoveEqLeft
482 % (n_1+n_2+n_3+n_4)^3 =
483 % n_1^3
484 % + 3 n_1^2 n_2
485 % + 3 n_1 n_2^2
486 % + n_2^3
487 % + 3 n_1^2 n_3
488 % + 6 n_1 n_2 n_3
489 % + 3 n_2^2 n_3
490 % + 3 n_1 n_3^2
491 % + 3 n_2 n_3^2
492 % + n_3^3
493 % + 3 n_1^2 n_4
494 % + 6 n_1 n_2 n_4
495 % + 3 n_2^2 n_4
496 % + 6 n_1 n_3 n_4
497 % + 6 n_2 n_3 n_4
498 % + 3 n_3^2 n_4
499 % + 3 n_1 n_4^2
500 % + 3 n_2 n_4^2
501 % + 3 n_3 n_4^2
502 % + n_4^3 .
503 % \end{autobreak}
504 % \end{align}
505 % \end{example}
506 % The indent width can be changed by an optional argument of
507 % the command.
508 %
509 % \begin{decl}
510 % |\everybeforeautobreak| \marg{tokens} \\
511 % |\everyaftereautobreak| \marg{tokens}
512 % \end{decl}
513 % \SpecialUsageIndex{\everybeforeautobreak}
514 % \SpecialUsageIndex{\everyafterautobreak}
515 % They specify token lists inserted before and after automatically
516 % inserted line breaks in \env{autobreak}. For example,
517 % \begin{example**}
518 % \begin{align}
519 % \everyafterautobreak{\times}
520 % \begin{autobreak}
521 % \cos\left(\frac{\pi x}{2}\right) =
522 % \left(1-x^2\right)
523 % \left(1-\frac{x^2}{9}\right)
524 % \left(1-\frac{x^2}{25}\right)
525 % \left(1-\frac{x^2}{49}\right)
526 % \left(1-\frac{x^2}{81}\right)
527 % \left(1-\frac{x^2}{121}\right)
528 % \left(1-\frac{x^2}{169}\right)
529 % \left(1-\frac{x^2}{225}\right)
530 % \left(1-\frac{x^2}{289}\right)
531 % \left(1-\frac{x^2}{361}\right)
532 % \left(1-\frac{x^2}{441}\right)
533 % \dots
534 % \end{autobreak}
535 % \end{align}
536 % \end{example**}
537 %
538 % \section{Caveats}
539 %
540 % Because \env{autobreak} tries to insert line breaks at any of new line
541 % characters, you must not make any new lines at which the line cannot
542 % be broken^^A
543 % \footnote{^^A
544 % You may put "\%" at the end of the line to avoid a new line.
545 % }.
546 % For example
547 % \begin{example*}
548 % \begin{align}
549 % \begin{autobreak}
550 % x =
551 % % A problematic line break.
552 % \frac{1}
553 % {2} .
554 % \end{autobreak}
555 % \end{align}
556 % \end{example*}
557 % gives an error in the typesetting:
558 % \begin{example*}
559 % ! Missing } inserted.
560 % <inserted text>
561 % }
562 % l.8 \end{align}
563 % \end{example*}
564 %
565 % Putting `|\\|' or `|&|' inside \env{autobreak}, which tries to insert
566 % these special stuffs automatically, also causes typesetting errors.
567 %
568 % The \env{autobreak} environment uses \cs{linewidth} as the maximum
569 % width that expressions in its body can occupy. There is no way for
570 % \env{autobreak} to know how much other formulae consume the space
571 % outside it. Therefore it fails to determine the adequate maximum
572 % width when there are expressions outside \env{autobreak} and
573 % then \LaTeX{} gives overfull line warnings:
574 % \begin{example*}
575 % \begin{align}
576 % \text{some stuff outside autobreak}
577 % \begin{autobreak}
578 % \text{LHS} =
579 % ...
580 % \end{autobreak} . % Even just a "." can be problematic.
581 % \end{align}
582 % % May give overfull line warnings
583 % \end{example*}
584 %
585 % One may want to separate long formulae from the main document file to
586 % other files and include them via \cs{input}\marg{file}, for example,
587 % \begin{example*}
588 % \begin{align}
589 % \begin{autobreak}
590 % \input{longeqn.inc} % It works!
591 % \end{autobreak}
592 % \end{align}
593 %
594 % \begin{align}
595 % \begin{autobreak}
596 % lhs =
597 % \input{longrhs.inc} % It also works!
598 % .
599 % \end{autobreak}
600 % \end{align}
601 % \end{example*}
602 % The current version of \env{autobreak} supports these cases: the file
603 % content of \cs{input}\marg{file} is expanded before recognizing the
604 % lines, with the help of the \pkg{catchfile} package^^A
605 % \footnote{^^A
606 % \url{https://www.ctan.org/pkg/catchfile}.
607 % },
608 % when it appears at the beginning of each line. But it does not support
609 % \cs{input}\marg{file} in the middle of the lines:
610 % \begin{example*}
611 % \begin{align}
612 % \begin{autobreak}
613 % x + \input{longexpr.inc} % Sorry, it does not work.
614 % \end{autobreak}
615 % \end{align}
616 % \end{example*}
617 % The difficulty comes from the fact that it needs to be expanded before
618 % \env{autobreak} scans lines. By the same reason, \env{autobreak} fails
619 % to detect new lines defined inside macros^^A
620 % \footnote{^^A
621 % Actually, when the definition of \cs{foo} is parsed, the new line
622 % characters inside it are usually lost.
623 % }:
624 % \begin{example*}
625 % \newcommand{\foo}{
626 % a
627 % + b
628 % + c
629 % + d
630 % }
631 % \begin{align}
632 % \begin{autobreak}
633 % \foo + \foo + \foo + \foo % No new lines can be detected.
634 % \end{autobreak}
635 % \end{align}
636 % \end{example*}
637 %
638 % \StopEventually{^^A
639 % \PrintChanges
640 % \PrintIndex
641 % }
642 %
643 % \section{Implementation}
644 %
645 % \begin{macrocode}
646 %<*package>
647 \NeedsTeXFormat{LaTeX2e}
648 \ProvidesPackage{autobreak}%
649 [2017/02/23 v0.3 simple line breaking of long formulae]
650 % \end{macrocode}
651 %
652 % \subsection{Registers and constants}
653 %
654 % \begin{macro}{\everybeforeautobreak}
655 % The list of tokens that gets inserted before every line break generated
656 % by \env{autobreak}.
657 % \begin{macrocode}
658 \newtoks\everybeforeautobreak
659 % \end{macrocode}
660 % \end{macro}
661 %
662 % \begin{macro}{\everyafterautobreak}
663 % The list of tokens that gets inserted after every line break generated
664 % by \env{autobreak}.
665 % \begin{macrocode}
666 \newtoks\everyafterautobreak
667 % \end{macrocode}
668 % \end{macro}
669 %
670 % \begin{macro}{\@autobreak@alltoks}
671 % The token register to store the whole result of \env{autobreak}.
672 % \begin{macrocode}
673 \newtoks\@autobreak@alltoks
674 % \end{macrocode}
675 % \end{macro}
676 %
677 % \begin{macro}{\@autobreak@linetoks}
678 % The token register for the current line.
679 % \begin{macrocode}
680 \newtoks\@autobreak@linetoks
681 % \end{macrocode}
682 % \end{macro}
683 %
684 % \begin{macro}{\@autobreak@lhswidth}
685 % The width of the current left-hand side.
686 % \begin{macrocode}
687 \newdimen\@autobreak@lhswidth
688 % \end{macrocode}
689 % \end{macro}
690 %
691 % \begin{macro}{\@autobreak@rhswidth}
692 % The width of the current right-hand side.
693 % \begin{macrocode}
694 \newdimen\@autobreak@rhswidth
695 % \end{macrocode}
696 % \end{macro}
697 %
698 % \begin{macro}{\@autobreak@maxlhswidth}
699 % The width of the longest left-hand side occupied. Affected by the
700 % |.aux| file generated in the previous run.
701 % \begin{macrocode}
702 \newdimen\@autobreak@maxlhswidth
703 % \end{macrocode}
704 % \end{macro}
705 %
706 % \begin{macro}{\@autobreak@realmaxlhswidth}
707 % The width of the longest left-hand side occupied. Not affected by the
708 % |.aux| file generated in the previous run.
709 % \begin{macrocode}
710 \newdimen\@autobreak@realmaxlhswidth
711 % \end{macrocode}
712 % \end{macro}
713 %
714 % \begin{macro}{\@autobreak@maxrhswidth}
715 % The maximum width that the right-hand sides can occupy.
716 % \begin{macrocode}
717 \newdimen\@autobreak@maxrhswidth
718 % \end{macrocode}
719 % \end{macro}
720 %
721 % \begin{macro}{\@autobreak@newlinechar}
722 % The macro representing an active |^^M|.
723 % \begin{macrocode}
724 \begingroup
725 \catcode`\^^M=\active
726 \gdef\@autobreak@newlinechar{^^M}
727 \endgroup
728 % \end{macrocode}
729 % \end{macro}
730 %
731 % \subsection{Interaction with \texttt{.aux} files}
732 %
733 % When there are two or more \env{autobreak} in one \env{align}, each
734 % \env{autobreak} has to know the maximum width of the left-hand side of
735 % the all \env{autobreak} in the same \env{align}. Instead of violating
736 % `causality' (e.g., how \LaTeX{} parses a file from the beginning to
737 % the end), we use |.aux| file to store the maximum left-hand side
738 % width, which provides the correct value in the next run.
739 %
740 % \begin{macro}{\if@autobreak@invalidlayout}
741 % The switch to be turned on when an invalid layout is detected.
742 % \begin{macrocode}
743 \newif\if@autobreak@invalidlayout
744 \@autobreak@invalidlayoutfalse
745 % \end{macrocode}
746 % \end{macro}
747 % Show a warning if the user needs to rerun.
748 % \begin{macrocode}
749 \AtEndDocument{%
750 \if@autobreak@invalidlayout
751 \if@filesw
752 \PackageWarningNoLine{autobreak}{Layout may have changed.
753 \MessageBreak Rerun to get layout correct}%
754 \else
755 \PackageWarningNoLine{autobreak}{Layout may be wrong}%
756 \fi
757 \fi
758 }
759 % \end{macrocode}
760 % \begin{macro}{\@autobreak@getmaxlhswidth}
761 % To be expanded to a value saved in |.aux| in the previous run, or
762 % |0pt| if not found.
763 % \begin{macrocode}
764 \def\@autobreak@getmaxlhswidth#1{%
765 \@ifundefined{@autobreak@w@#1}{%
766 \z@
767 }{%
768 \@nameuse{@autobreak@w@#1}%
769 }%
770 }
771 % \end{macrocode}
772 % \end{macro}
773 %
774 % \begin{macro}{\@autobreak@setmaxlhswidth}
775 % Called in |.aux|.
776 % \begin{macrocode}
777 \def\@autobreak@setmaxlhswidth#1#2{%
778 \global\@namedef{@autobreak@w@#1}{#2}%
779 }
780 % \end{macrocode}
781 % \end{macro}
782 %
783 % \begin{macro}{@autobreak@eqnindex}
784 % The counter to identify each \env{align}.
785 % \begin{macrocode}
786 \newcounter{@autobreak@eqnindex}
787 % \end{macrocode}
788 % \end{macro}
789 %
790 % \begin{macro}{@autobreak@subeqnindex}
791 % The counter to store the number of \env{autobreak} in an \env{align}.
792 % \begin{macrocode}
793 \newcounter{@autobreak@subeqnindex}[@autobreak@eqnindex]%
794 % \end{macrocode}
795 % \end{macro}
796 %
797 % \begin{macro}{\@autobreak@loadmaxlhswidth}
798 % Loads \cs{@autobreak@maxlhswidth} for the next \env{align}.
799 % \begin{macrocode}
800 \def\@autobreak@loadmaxlhswidth{%
801 \stepcounter{@autobreak@eqnindex}%
802 \@autobreak@maxlhswidth=%
803 \@autobreak@getmaxlhswidth{\arabic{@autobreak@eqnindex}}%
804 \@autobreak@realmaxlhswidth=\z@
805 }
806 % \end{macrocode}
807 % \end{macro}
808 %
809 % \begin{macro}{\@autobreak@savemaxlhswidth}
810 % Saves \cs{@autobreak@realmaxlhswidth} for the next run.
811 % \begin{macrocode}
812 \def\@autobreak@savemaxlhswidth{%
813 \ifnum\arabic{@autobreak@subeqnindex}>0
814 \ifdim\@autobreak@maxlhswidth=\@autobreak@realmaxlhswidth
815 \else
816 % \end{macrocode}
817 % We have used the wrong value of \cs{@autobreak@maxlhswidth} (was too
818 % much). Need to rerun.
819 % \begin{macrocode}
820 \global\@autobreak@invalidlayouttrue
821 \fi
822 % \end{macrocode}
823 % Note that \cs{@autobreak@maxlhswidth} becomes problematic only when
824 % two or more \env{autobreak} appear in one \env{align}. In the case
825 % with one \env{autobreak}, the default value |0pt| is safe for the next
826 % run.
827 % \begin{macrocode}
828 \ifnum\arabic{@autobreak@subeqnindex}>1
829 \if@filesw
830 % \end{macrocode}
831 % We should provide \cs{@autobreak@setmaxlhswidth} in |.aux|.
832 % \begin{macrocode}
833 \@ifundefined{@autobreak@auxinited}{%
834 \immediate\write\@mainaux{%
835 \string\providecommand
836 \string\@autobreak@setmaxlhswidth[2]{}%
837 }%
838 \gdef\@autobreak@auxinited{}%
839 }{}%
840 \immediate\write\@auxout{%
841 \string\@autobreak@setmaxlhswidth%
842 {\arabic{@autobreak@eqnindex}}%
843 {\the\@autobreak@realmaxlhswidth}%
844 }%
845 \fi
846 \fi
847 \fi
848 }
849 % \end{macrocode}
850 % \end{macro}
851 %
852 % \subsection{Hacking \pkg{amsmath}}
853 %
854 % \begin{macrocode}
855 \RequirePackage{amsmath}
856 % \end{macrocode}
857 %
858 % \begin{macro}{\if@autobreak@newlinedef}
859 % The switch to be turned on when \cs{@autobreak@newlinedef} applies.
860 % \begin{macrocode}
861 \newif\if@autobreak@newlinedef
862 \@autobreak@newlinedeffalse
863 % \end{macrocode}
864 % \end{macro}
865 %
866 % \begin{macro}{\@autobreak@newlinedef}
867 % Installs the definition of |^^M| as a space. This is virtually
868 % harmless in math mode.
869 % \begin{macrocode}
870 \begingroup
871 \catcode`\^^M=\active
872 \gdef\@autobreak@newlinedef{%
873 \def^^M{ }%
874 \@autobreak@newlinedeftrue
875 }
876 \endgroup
877 % \end{macrocode}
878 % \end{macro}
879 %
880 % \begin{macro}{\collect@body}
881 % We need to override \cs{collect@body} such that it keeps |^^M|.
882 % \begin{macrocode}
883 \def\collect@body#1{%
884 \@envbody={\expandafter#1\expandafter{\the\@envbody}}%
885 \edef\process@envbody{%
886 \the\@envbody\noexpand\end{\@currenvir}%
887 }%
888 \@envbody=\@emptytoks
889 \def\begin@stack{b}%
890 \begingroup
891 % \end{macrocode}
892 % Actually, the following three lines need to be inserted to the
893 % original code.
894 % \begin{macrocode}
895 \if@autobreak@newlinedef
896 \catcode`\^^M=\active
897 \fi
898 \expandafter\let\csname\@currenvir\endcsname=\collect@@body
899 \edef\process@envbody{%
900 \expandafter\noexpand\csname\@currenvir\endcsname
901 }%
902 \process@envbody
903 }
904 % \end{macrocode}
905 % \end{macro}
906 %
907 % \begin{environment}{align}
908 % Hack \env{align} of \pkg{amsmath}.
909 % \begin{macrocode}
910 \let\@autobreak@oldstart@align=\start@align
911 \def\start@align{%
912 \@autobreak@loadmaxlhswidth
913 \@autobreak@newlinedef
914 \@autobreak@oldstart@align
915 }
916 % \end{macrocode}
917 % \begin{macrocode}
918 \let\@autobreak@oldendalign=\endalign
919 \def\endalign{%
920 \@autobreak@savemaxlhswidth
921 \@autobreak@oldendalign
922 }
923 % \end{macrocode}
924 % \end{environment}
925 %
926 % \subsection{\env{autobreak} environment}
927 %
928 % \begin{environment}{autobreak}
929 % Checks if we are in \env{align} (and \cs{@autobreak@newlinedef} is
930 % applied), increments the counter and collects its body via
931 % \cs{collect@body}.
932 % \begin{macrocode}
933 \newenvironment{autobreak}{%
934 \if@autobreak@newlinedef
935 \else
936 \PackageError{autobreak}{%
937 autobreak is not allowed here%
938 }{%
939 Use autobreak inside align.
940 }%
941 \fi
942 \stepcounter{@autobreak@subeqnindex}%
943 \collect@body\@autobreak
944 }{}
945 % \end{macrocode}
946 % \end{environment}
947 %
948 % \begin{macro}{\@autobreak}
949 % Called from \cs{collect@body}. The parameter |#1| is the whole body.
950 % It takes also |#2| and |#3|, which are always \cs{end} and
951 % |autobreak|, to remove them from the successive tokens.
952 % \begin{macrocode}
953 \def\@autobreak#1#2#3{%
954 % \end{macrocode}
955 % First, close the group of \env{autobreak}.
956 % \begin{macrocode}
957 \end{autobreak}%
958 % \end{macrocode}
959 % Then parse the given body of the environment and construct lines to be
960 % passed to \env{align}.
961 % \begin{macrocode}
962 \@autobreak@init
963 \def\@tempa{\expandafter\@autobreak@scanline
964 \@autobreak@newlinechar#1}%
965 \expandafter\@tempa\@autobreak@newlinechar\@autobreak@end
966 }
967 % \end{macrocode}
968 % \end{macro}
969 %
970 % \begin{macro}{\@autobreak@init}
971 % Initialization.
972 % \begin{macrocode}
973 \def\@autobreak@init{%
974 \@autobreak@alltoks={}%
975 \@autobreak@linetoks={}%
976 \@autobreak@lhswidth=\z@
977 \let\MoveEqLeft=\@autobreak@MoveEqLeft
978 }
979 % \end{macrocode}
980 % \end{macro}
981 %
982 % \begin{macro}{\@autobreak@end}
983 % Finalization. It generates the whole lines in one go.
984 % \begin{macrocode}
985 \def\@autobreak@end{%
986 \expandafter\@autobreak@addtoks\expandafter\@autobreak@alltoks
987 \expandafter{\the\@autobreak@linetoks}%
988 \the\@autobreak@alltoks
989 }
990 % \end{macrocode}
991 % \end{macro}
992 %
993 % \begin{macro}{\@autobreak@scanline}
994 % Takes a line from the input stream. Here a line ends with |^^M|.
995 % \begin{macrocode}
996 \begingroup
997 \catcode`\^^M=\active
998 \gdef\@autobreak@scanline#1^^M{\@autobreak@scanline@{#1}}
999 \endgroup
1000 % \end{macrocode}
1001 % If the next token is a punctuation, then we merge it into the current
1002 % line. (Otherwise it can make a line only with a period, for example).
1003 % \begin{macrocode}
1004 \def\@autobreak@scanline@#1{%
1005 \@autobreak@ifnextpunct{%
1006 \@autobreak@scanline@gobble{#1}%
1007 }{%
1008 \@autobreak@scanline@@{#1}%
1009 }%
1010 }
1011 % \end{macrocode}
1012 % A helper macro of \cs{@ifnextpunct}\marg{if-yes}\marg{if-no}.
1013 % \begin{macrocode}
1014 \def\@autobreak@ifnextpunct#1#2{%
1015 \@ifnextchar.{%
1016 #1%
1017 }{%
1018 \@ifnextchar,{%
1019 #1%
1020 }{%
1021 \@ifnextchar;{%
1022 #1%
1023 }{%
1024 \@ifnextchar:{%
1025 #1%
1026 }{%
1027 #2%
1028 }%
1029 }%
1030 }%
1031 }%
1032 }
1033 % \end{macrocode}
1034 % Merge punctuations as possible (usually there is only one period in a
1035 % line, though).
1036 % \begin{macrocode}
1037 \def\@autobreak@scanline@gobble#1#2{%
1038 \@autobreak@ifnextpunct{%
1039 \@autobreak@scanline@gobble{#1#2}%
1040 }{%
1041 \@autobreak@scanline@@{#1#2}%
1042 }%
1043 }
1044 % \end{macrocode}
1045 % Pass the current line to \cs{@autobreak@processline}. Then, repeat
1046 % scanning lines until \cs{@autobreak@end} appears as the next token.
1047 % \begin{macrocode}
1048 \def\@autobreak@scanline@@#1{%
1049 \@autobreak@processline{#1}%
1050 \@ifnextchar\@autobreak@end{}{%
1051 \@autobreak@scanline@@@
1052 }%
1053 }
1054 % \end{macrocode}
1055 % Catch \cs{MoveEqLeft}.
1056 % \changes{v0.3}{2017/02/23}{Add \cs{MoveEqLeft} command}
1057 % \begin{macrocode}
1058 \def\@autobreak@scanline@@@{%
1059 \@ifnextchar\MoveEqLeft{%
1060 \@autobreak@scanline@MoveEqLeft
1061 }{%
1062 \@autobreak@scanline@@@@
1063 }%
1064 }
1065 % \end{macrocode}
1066 % The argument |#1| is \cs{MoveEqLeft}. This command accepts an
1067 % optional number.
1068 % \begin{macrocode}
1069 \def\@autobreak@scanline@MoveEqLeft#1{%
1070 \@ifnextchar[{%
1071 \@autobreak@scanline@MoveEqLeft@
1072 }{%
1073 \@autobreak@scanline@MoveEqLeft@[2]%
1074 }%
1075 }
1076 % \end{macrocode}
1077 % \begin{macrocode}
1078 \def\@autobreak@scanline@MoveEqLeft@[#1]{%
1079 \ifdim#1\p@>\z@
1080 \def\@tempa{\@autobreak@scanline@MvEqL@pos}%
1081 \else\ifdim#1\p@=\z@
1082 \def\@tempa{\@autobreak@scanline@MvEqL@zero}%
1083 \else
1084 \def\@tempa{\@autobreak@scanline@MvEqL@neg}%
1085 \fi\fi
1086 \@tempa{#1}%
1087 }
1088 % \end{macrocode}
1089 % \begin{macrocode}
1090 \def\@autobreak@scanline@MvEqL@pos#1{%
1091 \def\@tempa{\expandafter\@autobreak@scanline\kern#1em}%
1092 \expandafter\@tempa\@autobreak@newlinechar\kern-#1em%
1093 }
1094 % \end{macrocode}
1095 % In the case with |#1| = 0, a special treatment is required because \\
1096 % \cs{@autobreak@processline} ignores a zero width. Insert a very tiny
1097 % space.
1098 % \begin{macrocode}
1099 \def\@autobreak@scanline@MvEqL@zero#1{%
1100 \def\@tempa{\expandafter\@autobreak@scanline\kern1sp}%
1101 \expandafter\@tempa\@autobreak@newlinechar\kern-1sp%
1102 }
1103 % \end{macrocode}
1104 % In the case with |#1| < 0, put a very tiny space, and then put the
1105 % space with a positive width such that the first line is indented to
1106 % the right.
1107 % \begin{macrocode}
1108 \def\@autobreak@scanline@MvEqL@neg#1{%
1109 \def\@tempa{\expandafter\@autobreak@scanline\kern1sp}%
1110 \expandafter\@tempa\@autobreak@newlinechar\kern-1sp\kern-#1em%
1111 }
1112 % \end{macrocode}
1113 % One may expect \cs{input}\marg{file} in \env{autobreak} is expanded by
1114 % the file content and \env{autobreak} treats new lines in it correctly.
1115 % But it needs more work. Because handling of \cs{input} in the middle
1116 % of the lines is rather involved, for now we support only \cs{input} at
1117 % the beginning of each line (which is what sane people usually do).
1118 % This can be done via the \pkg{catchfile} package.
1119 % \begin{macrocode}
1120 \IfFileExists{catchfile.sty}{
1121 \RequirePackage{catchfile}
1122 \def\@autobreak@scanline@@@@{%
1123 \@ifnextchar\input{%
1124 \@autobreak@scanline@input
1125 }{%
1126 \@autobreak@scanline
1127 }%
1128 }%
1129 }{
1130 \def\@autobreak@scanline@@@@{%
1131 \@ifnextchar\input{%
1132 \PackageWarning{autobreak}{%
1133 Cannot handle new lines in a file via \protect\input,
1134 \MessageBreak which requires the catchfile package
1135 }%
1136 }%
1137 \@autobreak@scanline
1138 }%
1139 }
1140 % \end{macrocode}
1141 % The argument |#1| is \cs{input} and |#2| is the file name.
1142 % \begin{macrocode}
1143 \def\@autobreak@scanline@input#1#2{%
1144 \CatchFileDef\@tempa{#2}{\catcode`\^^M=\active}%
1145 \expandafter\@autobreak@scanline\@tempa
1146 }
1147 % \end{macrocode}
1148 % \end{macro}
1149 %
1150 % \begin{macro}{\@autobreak@MoveEqLeft}
1151 % This definition is expanded only when \cs{@autobreak@scanline} cannot
1152 % detect \cs{MoveEqLeft} in an \env{autobreak} environment, in other
1153 % words, when it appears in the middle of a line.
1154 % \begin{macrocode}
1155 \def\@autobreak@MoveEqLeft{%
1156 \PackageError{autobreak}{%
1157 \protect\MoveEqLeft\space is not allowed here%
1158 }{%
1159 \protect\MoveEqLeft\space must be put at the beginning of
1160 an autobreak environment.
1161 }%
1162 }
1163 % \end{macrocode}
1164 % \end{macro}
1165 %
1166 % \begin{macro}{\@autobreak@processline}
1167 % \changes{v0.2}{2016/07/03}{Fix space calculation around alignment
1168 % tabs}
1169 % Each line from \cs{autobreak@scanline} should be regarded as a `block'
1170 % in the equation. The first block (typically the left-hand side $+$
1171 % `=') determines the indentation for the successive lines. From the
1172 % second block, try to append the block to the end of the line and
1173 % insert a line break if it does not fit in a line. Note that we measure
1174 % the widths of the blocks with putting |{}| around alignment tabs.
1175 % \begin{macrocode}
1176 \def\@autobreak@processline#1{%
1177 \ifdim\@autobreak@lhswidth=\z@
1178 % \end{macrocode}
1179 % For the first block. The rest of the width for the right-hand sides
1180 % is determined from \cs{linewidth} and \cs{@autobreak@maxlhswidth}.
1181 % \begin{macrocode}
1182 \@autobreak@settowidth\@autobreak@lhswidth{#1{}}%
1183 \ifdim\@autobreak@lhswidth>\z@
1184 \ifdim\@autobreak@lhswidth>\@autobreak@maxlhswidth
1185 \ifdim\@autobreak@maxlhswidth>\z@
1186 % \end{macrocode}
1187 % The previous one used the wrong value of \cs{@autobreak@maxlhswidth}
1188 % (was too short). Need to rerun.
1189 % \begin{macrocode}
1190 \global\@autobreak@invalidlayouttrue
1191 \fi
1192 \global\@autobreak@maxlhswidth=\@autobreak@lhswidth
1193 \fi
1194 \ifdim\@autobreak@lhswidth>\@autobreak@realmaxlhswidth
1195 \global\@autobreak@realmaxlhswidth=\@autobreak@lhswidth
1196 \fi
1197 \@autobreak@maxrhswidth=\linewidth
1198 \advance\@autobreak@maxrhswidth by -\@autobreak@maxlhswidth
1199 \@autobreak@alltoks={#1{}&}%
1200 \fi
1201 \else
1202 % \end{macrocode}
1203 % For the rest of the blocks.
1204 % \begin{macrocode}
1205 \@autobreak@settowidth\@autobreak@rhswidth
1206 {{}\the\@autobreak@linetoks#1\the\everybeforeautobreak}%
1207 \ifdim\@autobreak@rhswidth>\@autobreak@maxrhswidth
1208 % \end{macrocode}
1209 % Adding the next block gives an overfull line. Need a line break.
1210 % \begin{macrocode}
1211 \edef\@tempa{\the\@autobreak@linetoks\the\everybeforeautobreak}%
1212 \expandafter\@autobreak@addtoks\expandafter\@autobreak@alltoks
1213 \expandafter{\@tempa\notag\\&}%
1214 \@autobreak@linetoks=\everyafterautobreak
1215 \fi
1216 \@autobreak@addtoks\@autobreak@linetoks{#1}%
1217 \fi
1218 }
1219 % \end{macrocode}
1220 % \end{macro}
1221 %
1222 % \begin{macro}{\@autobreak@addtoks}
1223 % Appends |#2| to the token register |#1|.
1224 % \begin{macrocode}
1225 \def\@autobreak@addtoks#1#2{%
1226 #1=\expandafter{\the#1#2}%
1227 }
1228 % \end{macrocode}
1229 % \end{macro}
1230 %
1231 % \begin{macro}{\@autobreak@settowidth}
1232 % Same as \cs{settowidth} but in math mode. We assume \cs{displaystyle}.
1233 % (Anyway \env{align} issues \cs{displaystyle} at the beginning of every
1234 % cell.)
1235 % \begin{macrocode}
1236 \def\@autobreak@settowidth#1#2{%
1237 \settowidth#1{$\displaystyle#2$}%
1238 }
1239 % \end{macrocode}
1240 % \end{macro}
1241 %
1242 % \begin{macrocode}
1243 %</package>
1244 % \end{macrocode}
1245 % \Finale
1246 \endinput