mathenv.tex 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. \documentclass{article}
  2. \usepackage{mdwtab}
  3. \errorcontextlines 999
  4. \showboxbreadth\maxdimen
  5. \showboxdepth=2
  6. \makeatletter
  7. % --- Switch allocations ---
  8. \newif\if@fleqn
  9. \newif\if@leqno
  10. % --- Dimen allocations ---
  11. \newdimen\eqa@thiscolwd
  12. \newdimen\eqa@maxcolwd
  13. \newdimen\eqa@maxeqwd
  14. \newdimen\eqa@maxcoleqwd
  15. % --- Main environments ---
  16. \def\equations{%
  17. \def\eqa@defnumber{(\theequation)\stepcounter{equation}}%
  18. \let\eqa@defmarker\eqa@fullmarker%
  19. \eqa@equations%
  20. }
  21. \def\endequations{%
  22. \\%
  23. \noalign{\global\dimen@i\prevdepth}%
  24. \multispan\tab@columns\hfill\vrule\@depth\dimen@i\cr%
  25. \egroup%
  26. \eqa@offsetcalc%
  27. \egroup%
  28. \eqa@restore%
  29. {\scrollmode\showbox\z@}%
  30. }
  31. \def\eqa@equations#1{%
  32. %
  33. % Set up restoring of things.
  34. %
  35. \toks@\expandafter{\eqa@number}%
  36. \toks\tw@\expandafter{\eqa@marker}%
  37. \edef\eqa@restore{%
  38. \gdef\noexpand\eqa@number{\the\toks@}%
  39. \gdef\noexpand\eqa@marker{\the\toks\tw@}%
  40. \eqa@maxcolwd\the\eqa@maxcolwd%
  41. \eqa@maxcoleqwd\the\eqa@maxcoleqwd%
  42. \eqa@maxeqwd\the\eqa@maxeqwd%
  43. }%
  44. %
  45. % Initialise numbering things.
  46. %
  47. \global\let\eqa@number\eqa@defnumber%
  48. \global\let\eqa@marker\eqa@defmarker%
  49. \let\eqa@markpen\@ne%
  50. %
  51. % Parse the preamble string. Put measuring things in the right places.
  52. %
  53. \tab@initread%
  54. \def\tab@tabtext{&\tabskip\z@skip}%
  55. \if@leqno%
  56. \tab@append\tab@preamble{\let\eqa@measure\eqa@domeasure}%
  57. \fi%
  58. \def\eqa@seteqcol##1{%
  59. \def\eqa@eqcol{##1}
  60. \if@leqno\let\eqa@seteqcol\relax\fi%
  61. }%
  62. \colset{equations}%
  63. \tab@doreadpream{#1}%
  64. \if@leqno\else%
  65. \tab@prepend\tab@pretext{\let\eqa@measure\eqa@domeasure}%
  66. \fi%
  67. \tab@readpreamble{}%
  68. %
  69. % Setting the newline command and some other initialisation.
  70. %
  71. \let\\\eqa@cr%
  72. \global\eqa@maxcolwd\z@%
  73. \global\eqa@maxcoleqwd\z@%
  74. \global\eqa@maxeqwd\z@%
  75. %
  76. % Start the box. Hacking to make \prevdepth work properly.
  77. %
  78. \setbox\z@\vbox\expandafter\bgroup%
  79. \expandafter\prevdepth\the\prevdepth%
  80. \relax%
  81. %
  82. % And now the alignment.
  83. %
  84. \tabskip\z@skip%
  85. \halign\expandafter\bgroup\the\tab@preamble\cr%
  86. }
  87. % --- Column building things ---
  88. \def\eqa@aligncol#1#2#3{%
  89. \eqa@seteqcol{#1}%
  90. \ifx l#1\eqa@aligncol@i{#2#3}{#2\hfil}\else%
  91. \ifx c#1\eqa@aligncol@i{\hfil#2#3}{#3#2\hfil}\else%
  92. \ifx r#1\eqa@aligncol@i{\hfil#2}{#3#2}%
  93. \fi\fi\fi%
  94. }
  95. \def\eqa@aligncol@i#1#2{%
  96. \tabcoltype%
  97. {\setbox\z@\hbox\bgroup#1}%
  98. {#2\egroup\eqa@measure}%
  99. }
  100. \colpush{equations}
  101. \coldef l{\eqa@aligncol l${{}}}
  102. \coldef c{\eqa@aligncol c${{}}}
  103. \coldef r{\eqa@aligncol r${{}}}
  104. \coldef M#1{\eqa@aligncol{#1}${{}}}
  105. \coldef T#1{\eqa@aligncol{#1}{}{}}
  106. \colpop
  107. % --- Equation measuring macros ---
  108. \let\eqa@number\relax
  109. \let\eqa@marker\relax
  110. \def\eqnumber#1{%
  111. \global\let\eqa@marker\eqa@fullmarker%
  112. \gdef\eqa@number{#1}%
  113. }
  114. \def\nonumber{%
  115. \global\let\eqa@marker\eqa@nonummarker%
  116. \global\let\eqa@number\@empty%
  117. }
  118. \def\eqa@measure{\unhbox\z@}
  119. \def\eqa@domeasure{%
  120. \global\eqa@thiscolwd\wd\z@%
  121. \ifdim\eqa@maxcolwd<\eqa@thiscolwd%
  122. \global\eqa@maxcolwd\eqa@thiscolwd%
  123. \fi%
  124. \unhbox\z@%
  125. }
  126. % --- The newline command ---
  127. \def\eqa@cr{\tab@cr\eqa@cr@i\z@\@M}
  128. \def\eqa@cr@i#1#2{%
  129. \cr%
  130. \noalign{%
  131. \eqa@marker{#1}{#2}%
  132. \global\let\eqa@number\eqa@defnumber%
  133. \global\let\eqa@marker\eqa@defmarker%
  134. \penalty\eqa@markpen%
  135. }%
  136. }
  137. \def\eqa@fullmarker#1#2{%
  138. \dimen@\prevdepth%
  139. \vskip\eqa@thiscolwd%
  140. \penalty#2%
  141. \skip@#1\advance\skip@\jot%
  142. \vskip\skip@%
  143. \setbox\z@\hbox{\eqa@number}%
  144. \dimen@\eqa@thiscolwd\advance\dimen@\wd\z@%
  145. \ifdim\dimen@\eqa@maxcoleqwd<\dimen@%
  146. \global\eqa@maxcoleqwd\dimen@%
  147. \global\eqa@maxeqwd\wd\z@%
  148. \fi%
  149. \nointerlineskip\hb@xt@\z@{\hbox{\eqa@number}}%
  150. \prevdepth\dimen@%
  151. }
  152. \def\eqa@nonummarker#1#2{%
  153. \penalty#2%
  154. \skip@#1\advance\skip@\jot%
  155. \vskip\skip@%
  156. \penalty\eqa@markpen%
  157. }
  158. % --- Offset calculation ---
  159. %
  160. % This stuff is sort of like the standard offset calculation, only it's
  161. % different.
  162. \def\eqa@offsetcalc{%
  163. \setbox\z@\lastbox%
  164. \unskip%
  165. \csname eqa@calc:\eqa@eqcol\expandafter\noexpand\if@leqno\endcsname%
  166. }
  167. \@namedef{eqa@calc:l\noexpand\iffalse}{%
  168. % --- Test document ---
  169. \makeatother
  170. \begin{document}
  171. \setcounter{equation}{23}
  172. \begin{equations}{rrl}
  173. x &= y^2 +& z^2 \\
  174. x-y &= 3y^2 -& 2z^2
  175. \end{equations}
  176. \end{document}