| 1 |
% |
| 2 |
% \title{The calculation environment\\ |
| 3 |
% \normalsize formatting reasoned calculations and calculational proofs} |
| 4 |
% |
| 5 |
% \author{Maarten Fokkinga ({[email protected]})} |
| 6 |
% \maketitle |
| 7 |
% \MakeShortVerb{\"} |
| 8 |
% \begin{abstract} |
| 9 |
% \noindent |
| 10 |
% The "calculation" environment formats \emph{reasoned calculations}, |
| 11 |
% also called \emph{calculational proofs}. The notion of reasoned |
| 12 |
% calculations was originally advocated by Wim Feijen and Edsger |
| 13 |
% Dijkstra. The "calculation" package accepts options "fleqn" and |
| 14 |
% "leqno" (with the same effect as \LaTeX\ options "fleqn" and "leqno", |
| 15 |
% and inherits these from the document class), it allows steps and |
| 16 |
% expressions to be numbered (by \LaTeX\ equation numbers, obeying the |
| 17 |
% \LaTeX\ "\label" command to refer to these numbers), and a step |
| 18 |
% doesn't take vertical space if its hint is empty. An expression in a |
| 19 |
% calculation can be given a comment; it is placed at the side opposite |
| 20 |
% to the equation numbers. |
| 21 |
% \par |
| 22 |
% Calculations are allowed inside hints although numbering and |
| 23 |
% commenting is then disabled. |
| 24 |
% \end{abstract} |
| 25 |
% |
| 26 |
% \section{User manual} |
| 27 |
% This package provides the "calculation" environment, to format |
| 28 |
% \emph{reasoned calculations}, also called \emph{calculational proofs}. |
| 29 |
% The \emph{steps} in the calculation are vertically listed, and each step |
| 30 |
% is accompanied by a (possibly empty) \emph{hint}, explaining why the |
| 31 |
% step is valid. This style was originally advocated by Wim Feijen and |
| 32 |
% Edsger Dijkstra. The "calculation" package accepts options "fleqn" and |
| 33 |
% "leqno" (with the same effect as \LaTeX\ options "fleqn" and "leqno", |
| 34 |
% and inherits these from the document class), it allows steps and |
| 35 |
% expressions to be numbered (obeying \LaTeX's "\label" command to refer |
| 36 |
% to these numbers). A step doesn't take vertical space if its hint is |
| 37 |
% empty. An expression in a calculation can be given a comment; it is |
| 38 |
% placed at the side opposite to the equation numbers. |
| 39 |
% \par |
| 40 |
% Calculations are allowed inside hints by the "subcalculation" |
| 41 |
% environment (for small calculations which do not deserve a separate |
| 42 |
% discussion before or after the main calculation), although numbering |
| 43 |
% and commenting is then disabled. |
| 44 |
% |
| 45 |
% \iffalse ================= BEGIN example =============================== |
| 46 |
% Here is some stuff to generate file "calculation.expl", containing an |
| 47 |
% example of the "calculation" environment. This file will be read |
| 48 |
% twice: once to show it verbatim, and once to show \LaTeX's output. |
| 49 |
% |
| 50 |
% In order to process this example, this file that you are reading now |
| 51 |
% should be used as package. Thus, assuming that this file is called |
| 52 |
% calculation.dtx, the driver file should have in its prelude: |
| 53 |
% \makeatletter |
| 54 |
% \input{calculation.dtx} |
| 55 |
% \calc@fleqn\calc@leqno\calc@blocktrue % options: fleqn, leqno, block. |
| 56 |
% \makeatother |
| 57 |
% Once "calculation.sty" has been generated, you may use |
| 58 |
% \usepackage[fleqn,leqno,block]{calculation} |
| 59 |
% instead. |
| 60 |
% ==================================================================== \fi |
| 61 |
% \iffalse From the \TeX book, page 422: \fi |
| 62 |
% \chardef\other=12 |
| 63 |
% \newwrite\expl |
| 64 |
% \immediate\openout\expl=calculation.example |
| 65 |
% \def\copytoblankline{\begingroup\setupcopy\copyexpl} |
| 66 |
% \def\setupcopy{\def\do##1{\catcode`##1=\other}\dospecials |
| 67 |
% \catcode`\|=\other \obeylines} |
| 68 |
% {\obeylines \gdef\copyexpl#1 |
| 69 |
% {\def\next{#1}% |
| 70 |
% \ifx\next\empty\let\next=\endgroup % |
| 71 |
% \else\immediate\write\expl{\next} \let\next=\copyexpl\fi\next}} |
| 72 |
% \copytoblankline% |
| 73 |
% \begin{verbatim} |
| 74 |
% Demo: |
| 75 |
% \begin{calculation}[\approx] |
| 76 |
% expr \comment{cmnt!} |
| 77 |
% \step*{\label{N}% |
| 78 |
% This step has nbr~\ref{N}} |
| 79 |
% expr |
| 80 |
% \step*{} |
| 81 |
% \label{N2} |
| 82 |
% expr~left~part~~~ + ~~~ |
| 83 |
% \\ |
| 84 |
% expr~right~part |
| 85 |
% \step[=]{ |
| 86 |
% left part:\\ |
| 87 |
% \begin{subcalculation}[\leq] |
| 88 |
% expr |
| 89 |
% \step{hint} |
| 90 |
% expr |
| 91 |
% \step{hint} |
| 92 |
% expr |
| 93 |
% \end{subcalculation} |
| 94 |
% \\ |
| 95 |
% right part, similarly:\\ |
| 96 |
% \begin{subcalculation} |
| 97 |
% expr \step{} expr |
| 98 |
% \step{} expr |
| 99 |
% \end{subcalculation} |
| 100 |
% \\ |
| 101 |
% end of hint |
| 102 |
% } |
| 103 |
% \doNumber\label{E} |
| 104 |
% this~expr~has~number~\ref{E} |
| 105 |
% \step{hint 1st line\\ |
| 106 |
% hint 2nd line} |
| 107 |
% expr |
| 108 |
% \end{calculation} |
| 109 |
% Note: step (\ref{N2}) is empty. |
| 110 |
% \end{verbatim} |
| 111 |
|
| 112 |
% \iffalse Previous blank line is significant! \fi |
| 113 |
% \immediate\closeout\expl |
| 114 |
% \IfFileExists{calculation.example}{ |
| 115 |
% \subsection{Example} |
| 116 |
% \newsavebox{\fminibox} |
| 117 |
% \newlength{\fminilength} |
| 118 |
% \newenvironment{fminipage}[1][\linewidth] |
| 119 |
% {\setlength{\fminilength}{##1}% |
| 120 |
% \begin{lrbox}{\fminibox}\begin{minipage}{\fminilength}} |
| 121 |
% {\end{minipage}\end{lrbox}\noindent |
| 122 |
% \makebox[0pt][r]{\fbox{\usebox{\fminibox}}}\ignorespaces} |
| 123 |
% Here is an example (with options "fleqn", "leqno", and "block", |
| 124 |
% and default step symbol `$=$'); \LaTeX\ source on the left, |
| 125 |
% output on the right: |
| 126 |
% \par\noindent |
| 127 |
% \begin{center}\hspace*{-0.08\linewidth} |
| 128 |
% \begin{fminipage}[0.43\linewidth] |
| 129 |
% \input{calculation.example} |
| 130 |
% \end{fminipage}% |
| 131 |
% \renewenvironment{verbatim}{\relax}{\relax}% |
| 132 |
% \makebox[0pt][l]{\fbox{\begin{minipage}{0.5\linewidth} |
| 133 |
% \input{calculation.example} |
| 134 |
% \end{minipage}}} |
| 135 |
% \end{center}}{} |
| 136 |
% \iffalse ================= END of example ========================= \fi |
| 137 |
% \iffalse ========================================================== \fi |
| 138 |
% |
| 139 |
% \subsection{Usage} |
| 140 |
% The "calculation" environment is used in it most simple form as |
| 141 |
% follows: |
| 142 |
% \pagebreak[3] |
| 143 |
% \begin{verbatim} |
| 144 |
% \begin{calculation} |
| 145 |
% EXPRESSION |
| 146 |
% \step{HINT} |
| 147 |
% EXPRESSION |
| 148 |
% \step{HINT} |
| 149 |
% EXPRESSION |
| 150 |
% ... |
| 151 |
% \end{calculation}\end{verbatim} |
| 152 |
% Each "HINT" is normal text, each "EXPRESSION" is mathematical text.\par |
| 153 |
% Depending on various parameters, the output is, more or less, like |
| 154 |
% this: |
| 155 |
% \begin{verbatim} |
| 156 |
% EXPRESSION |
| 157 |
% = { HINT } |
| 158 |
% EXPRESSION |
| 159 |
% = { HINT } |
| 160 |
% EXPRESSION |
| 161 |
% ...\end{verbatim} |
| 162 |
% Command "\stepsymbol" defines the default step symbol, "=" in the |
| 163 |
% above example; it can be redefined with "\renewcommand". Moreover, the |
| 164 |
% "calculation" environment itself has an optional parameter, overruling |
| 165 |
% the default step symbol in this specific calculation, and even each |
| 166 |
% step has an optional parameter giving the step symbol for that |
| 167 |
% particular step. Command "\step*" produces a step that is numbered |
| 168 |
% with an equation number, as in \LaTeX; \LaTeX\ command "\label" may be |
| 169 |
% used to label the number so that references can be made to it. Command |
| 170 |
% "\doNumber" inside an EXPRESSION produces an equation number. Command |
| 171 |
% "\comment{TEXT}" inside an EXPRESSION produces TEXT at the side of the |
| 172 |
% paper opposite to the equation number side. So, in full glory, the |
| 173 |
% environment has the following appearance: |
| 174 |
% \begin{macrocode} |
| 175 |
%% ==================== Usage ==================================== |
| 176 |
%% |
| 177 |
%% \begin{calculation}[SYMBOL] |
| 178 |
%% EXPRESSION <-- may contain \doNumber and \comment{TEXT} |
| 179 |
%% \step*[SYMBOL]{HINT} |
| 180 |
%% EXPRESSION <-- may contain \doNumber and \comment{TEXT} |
| 181 |
%% \step*[SYMBOL]{HINT} |
| 182 |
%% EXPRESSION <-- may contain \doNumber and \comment{TEXT} |
| 183 |
%% ... |
| 184 |
%% \end{calculation} |
| 185 |
%% Each asterisk "*" and each part "[...]" is optional. |
| 186 |
%% |
| 187 |
% \end{macrocode} |
| 188 |
% An empty "HINT" |
| 189 |
% takes no vertical space while the "SYMBOL" is still vertically |
| 190 |
% centered in between the two "EXPRESSIONs". Both in "EXPRESSION" and in |
| 191 |
% "HINT" new lines may be generated by~"\\". As in \LaTeX, "EXPRESSION" |
| 192 |
% may not contain empty lines. Hints may have empty lines, even at the |
| 193 |
% beginning. |
| 194 |
% |
| 195 |
% Option "block" makes the entire hint (possibly several lines) into one |
| 196 |
% block which, as a whole, is surrounded by delimiters. |
| 197 |
% |
| 198 |
% Page breaks will occur only immediately after a step-plus-hint; a |
| 199 |
% page break within or after an expression would be confusing for the |
| 200 |
% reader. |
| 201 |
% |
| 202 |
% \subsection{Options and document style parameters} |
| 203 |
% By default, calculations are placed horizontally centered on the page, |
| 204 |
% but when the entire document has option "fleqn" (``flush left |
| 205 |
% equations''), or when the package is given this option explicitly, |
| 206 |
% then calculations are placed flush left. |
| 207 |
% |
| 208 |
% By default, calculations have their step and expression numbers at the |
| 209 |
% right side of the page (just as in \LaTeX), but when the entire |
| 210 |
% document has option "leqno" (``left equation numbers''), or when the |
| 211 |
% package is given this option explicitly, then calculations are placed |
| 212 |
% at the left side of the page. |
| 213 |
% |
| 214 |
% By default, the first line of a "HINT" is opened by "\Hlineopen" and |
| 215 |
% the last one is closed by "\Hlineclose" (braces in the above example), |
| 216 |
% but when option "block" is given to the package, then the entire hint |
| 217 |
% is made into one block surrounded by "\Hblockopen" and "\Hblockclose". |
| 218 |
% Both "\Hblockopen" and "\Hblockclose" must be ``delimiters'' so that |
| 219 |
% they can be stretched vertically. At least the following symbols are |
| 220 |
% delimiters: braces~$\{~\}$, brackets~$[~]$, vertical bar~$|$, vertical |
| 221 |
% double bar~$\|$, angles~$\langle~\rangle$, and ``nothing'' (in \LaTeX\ |
| 222 |
% indicated by a dot~"."). These commands can be redefined with |
| 223 |
% "\renewcommand". |
| 224 |
% |
| 225 |
% The amount of space between "\Hlineopen" and the first token of the |
| 226 |
% hint is given by "\Hsep". The amount of indentation of the text of a |
| 227 |
% hint, relative to the expressions, is given by "\Hindent" plus "\Hsep" |
| 228 |
% (plus the width of "\Hblockopen" if option "block" is valid). |
| 229 |
% |
| 230 |
% |
| 231 |
% In summary, the following are the style parameters and options: |
| 232 |
% \begin{macrocode} |
| 233 |
%% -------------- DOCUMENT STYLE PARAMETERS ------------------------ |
| 234 |
%% options: |
| 235 |
%% fleqn, leqno, block |
| 236 |
%% commands: |
| 237 |
%% \newcommand{\stepsymbol}{=} |
| 238 |
%% \newcommand{\Hblockopen}{|} |
| 239 |
%% \newcommand{\Hblockclose}{.} |
| 240 |
%% \newcommand{\Hlineopen}{\lbrace} |
| 241 |
%% \newcommand{\Hlineclose}{\rbrace} |
| 242 |
%% \newcommand{\Hindent}{1em} |
| 243 |
%% \newcommand{\Hsep}{1ex} |
| 244 |
%% \newcommand{\calculcolsep}{\arraycolsep} |
| 245 |
%% \newcommand{\Hposv}{t} |
| 246 |
%% % vertical position of the step symbol in front of a block hint |
| 247 |
%% These default values may be redefined by "\renewcommand". |
| 248 |
% \end{macrocode} |
| 249 |
%% |
| 250 |
\iffalse ============================== code =======================\fi |
| 251 |
% \section{The LaTeX code for the macros} |
| 252 |
% |
| 253 |
% \begin{macrocode} |
| 254 |
|
| 255 |
\def\fileversion{1.00} |
| 256 |
\def\filedate{5 Jan 2015} |
| 257 |
\def\docdate{5 Jan 2015} |
| 258 |
|
| 259 |
\ProvidesPackage{calculation}[2014/12/05 Format reasoned calculations] |
| 260 |
|
| 261 |
% \end{macrocode} |
| 262 |
% \changes{v0.x}{1991-2014}{Ugly constructed, ugly to use, nice output} |
| 263 |
% \changes{v1.00}{5 Jan 2015}{Original idea implemented anew} |
| 264 |
|
| 265 |
% |
| 266 |
% \noindent |
| 267 |
% All auxiliary variables specific to this package are named as follows: |
| 268 |
% \begin{verbatim} |
| 269 |
% \calc@.... or |
| 270 |
% \subcalc@... or |
| 271 |
% \ifcalc@... or |
| 272 |
% \endcalc@... |
| 273 |
% \end{verbatim} |
| 274 |
% Here are the options "fleqn", "leqno", and "block", and their effect: |
| 275 |
% \begin{macrocode} |
| 276 |
|
| 277 |
\DeclareOption{fleqn}{\calc@fleqn} |
| 278 |
\DeclareOption{leqno}{\calc@leqno} |
| 279 |
\DeclareOption{block}{\calc@blocktrue} |
| 280 |
|
| 281 |
% \end{macrocode} |
| 282 |
% |
| 283 |
% \subsection{Preliminary auxiliaries} |
| 284 |
% To place comments and step/expr numbers at opposite sides, we place |
| 285 |
% them in a very wide box at the right side of the page, and |
| 286 |
% surround them by suitable fill's: |
| 287 |
% \begin{verbatim} |
| 288 |
% ... \calc@eqnoLfil <exprno> \calc@eqnoRfil ... |
| 289 |
% ... \calc@eqnoLfil <stepno> \calc@eqnoRfil ... |
| 290 |
% ... \calc@eqnoRfil <comment> \calc@eqnoLfil ... |
| 291 |
% \end{verbatim} |
| 292 |
% Depending on the options, these ``fill''s are set to "\hfil" or "{}". |
| 293 |
% We first check that these fill commands are not yet in use: |
| 294 |
% \begin{macrocode} |
| 295 |
|
| 296 |
\newcommand{\calc@fill}{\relax} |
| 297 |
\newcommand{\calc@eqnoLfil}{\relax} |
| 298 |
\newcommand{\calc@eqnoRfil}{\relax} |
| 299 |
|
| 300 |
% \end{macrocode} |
| 301 |
% Eqno's at the left side means no fill at the L~side and a real fill at the R~side: |
| 302 |
% \begin{macrocode} |
| 303 |
|
| 304 |
\newcommand{\calc@leqno} |
| 305 |
{\def\calc@eqnoLfil{}\def\calc@eqnoRfil{\hfil}} |
| 306 |
|
| 307 |
% \end{macrocode} |
| 308 |
% Eqno's at the right side: |
| 309 |
% \begin{macrocode} |
| 310 |
|
| 311 |
\newcommand{\calc@reqno} |
| 312 |
{\def\calc@eqnoRfil{}\def\calc@eqnoLfil{\hfil}} |
| 313 |
|
| 314 |
% \end{macrocode} |
| 315 |
% Calculations horizontally centered on the page: |
| 316 |
% \begin{macrocode} |
| 317 |
|
| 318 |
\newcommand{\calc@ceqn}{\def\calc@fill{fil}} |
| 319 |
|
| 320 |
% \end{macrocode} |
| 321 |
% Calculations flush left: set "\calc@indent" to "\mathindent". |
| 322 |
% However, global document option fleqn defines "\mathindent"; |
| 323 |
% if global option fleqn is not used, "\mathindent" is defined now: |
| 324 |
% \begin{macrocode} |
| 325 |
|
| 326 |
\newcommand{\calc@fleqn} |
| 327 |
{\@ifundefined{mathindent} |
| 328 |
{\calc@indent\leftmargini}{\calc@indent\mathindent} |
| 329 |
\def\calc@fill{fill}} |
| 330 |
|
| 331 |
% \end{macrocode} |
| 332 |
% \subsection{Default settings related to the options} |
| 333 |
% |
| 334 |
% By default, calculations are centered and eqno's at the right (LaTeX' |
| 335 |
% default); and by default no block hints (so "\Hlineopen" at the very |
| 336 |
% first line and "\Hlineclose" at the very last line): |
| 337 |
% \begin{macrocode} |
| 338 |
|
| 339 |
\calc@ceqn |
| 340 |
\calc@reqno |
| 341 |
\newif\ifcalc@block \calc@blockfalse |
| 342 |
\newdimen\calc@indent\calc@indent\z@skip |
| 343 |
|
| 344 |
% \end{macrocode} |
| 345 |
% Recall that "\calc@indent" is set to "\mathindent" if "fleqn" is |
| 346 |
% valid. |
| 347 |
% \begin{macrocode} |
| 348 |
|
| 349 |
\ProcessOptions |
| 350 |
|
| 351 |
% \end{macrocode} |
| 352 |
% \subsection{Auxiliary commands of general use} |
| 353 |
% \begin{macrocode} |
| 354 |
|
| 355 |
\RequirePackage{delarray} |
| 356 |
|
| 357 |
% \end{macrocode} |
| 358 |
% Package "delarray" (and hence package "array") is required for "\left" |
| 359 |
% and "\right" delimiters for array's and tabulars --- which we will |
| 360 |
% exploit for hints. With this package, the delimiters come out well if |
| 361 |
% the array (tabular, in our case) has option "[t]" (top alignment). |
| 362 |
% Command "\@ifmtarg" is my poor man's way to test for an empty |
| 363 |
% argument; it should be "\protect"ed in moving arguments. Note: the |
| 364 |
% command "\@ifmtarg" provided by "\RequirePackage{ifmtarg}" doesn't |
| 365 |
% work if the argument (a HINT in our case) contains `"&"' symbols. |
| 366 |
% \begin{macrocode} |
| 367 |
|
| 368 |
\newcommand\@ifmtarg[3] |
| 369 |
{{\def\myempty{}\def\myarg{#1}\ifx\myempty\myarg{#2}\else{#3}\fi}} |
| 370 |
%% usage: \@ifmtarg {arg} {then} {else} |
| 371 |
|
| 372 |
% \end{macrocode} |
| 373 |
% |
| 374 |
% \subsection{Default values for the document style parameters} |
| 375 |
% \noindent |
| 376 |
% \iffalse===========================================================\fi |
| 377 |
% \fbox{NAMING CONVENTION: EXPR = Expression, H = Hint} |
| 378 |
% \iffalse===========================================================\fi |
| 379 |
% \begin{macrocode} |
| 380 |
|
| 381 |
\newcommand{\stepsymbol}{=} |
| 382 |
\newcommand{\Hblockopen}{|} |
| 383 |
\newcommand{\Hblockclose}{.} |
| 384 |
\newcommand{\Hlineopen}{\lbrace} |
| 385 |
\newcommand{\Hlineclose}{\rbrace} |
| 386 |
\newcommand{\Hindent}{1em} |
| 387 |
\newcommand{\Hsep}{1ex} |
| 388 |
\newcommand{\Hposv}{t} |
| 389 |
\newcommand{\calculcolsep}{\arraycolsep} |
| 390 |
|
| 391 |
% \end{macrocode} |
| 392 |
% The delimiters need some preprocessing in view of the way they will be used (in command "\calc@@@step"). In particular, the spaces at the beginning of a hint will be ignored (see "\HlineOPEN" below). |
| 393 |
% \begin{macrocode} |
| 394 |
|
| 395 |
\newcommand{\Hsepskip}{\hbox to \Hsep {}} |
| 396 |
\newcommand{\HlineOPEN} |
| 397 |
{\ifcalc@block\else\makebox[0pt][r]{\m@th$\Hlineopen$\Hsepskip}\fi |
| 398 |
\ignorespaces} |
| 399 |
\newcommand{\HlineCLOSE} |
| 400 |
{\ifcalc@block\else\makebox[0pt][l]{\m@th\Hsepskip$\Hlineclose$}\fi} |
| 401 |
|
| 402 |
% \end{macrocode} |
| 403 |
% NOTE: |
| 404 |
% The else clause in "\HlineCLOSE", above, assumes that the last hint |
| 405 |
% line is not empty. In order to get "\Hlineopen" and "\Hlineclose" |
| 406 |
% vertically aligned in case the last hint line is empty, the else |
| 407 |
% clause should read in that case: |
| 408 |
% |
| 409 |
% ``"\makebox[0pt][r]{\m@th$\Hlineclose$\Hsepskip}"''. |
| 410 |
% |
| 411 |
% \noindent |
| 412 |
% Alas, I don't know how to check ``the preceding line is empty'' in |
| 413 |
% the definition of "\calc@@@step" below, just in front of |
| 414 |
% "\HlineCLOSE". |
| 415 |
% \begin{macrocode} |
| 416 |
|
| 417 |
\newcommand{\HblockOPEN}{\ifcalc@block\Hblockopen\else.\fi} |
| 418 |
\newcommand{\HblockCLOSE}{\ifcalc@block\Hblockclose\else.\fi} |
| 419 |
|
| 420 |
% \end{macrocode} |
| 421 |
% |
| 422 |
% \subsection{Specific auxiliaries} |
| 423 |
% Some sanity checks, and several auxiliaries: |
| 424 |
% \begin{macrocode} |
| 425 |
\newcommand{\calc@origmath}{\relax} |
| 426 |
\newcommand{\calc@stepsymbol}{\relax} |
| 427 |
\newlength\calc@math % to store the value of \mathsurround |
| 428 |
\newif\ifcalc@emptyH % for temporary local use only |
| 429 |
\newif\ifcalc@numberedstep % true if currently in \step* |
| 430 |
\newif\ifcalc@eqnoswitch % true if an eqno will be produced |
| 431 |
\newcommand{\calc@emptyHskip}{-0.5\baselineskip}% for empty hints |
| 432 |
\calc@eqnoswitchfalse |
| 433 |
% \end{macrocode} |
| 434 |
% By default the expressions in a calculation are not numbered. |
| 435 |
% Whenever an expression is to be numbered, the switch is set true. |
| 436 |
% This is done by "\doNumber", which will be made available inside expressions of a calculation. Doing "\doNumber" twice should have the same effect as doing it once! |
| 437 |
% \begin{macrocode} |
| 438 |
\newcommand{\calc@doNumber} |
| 439 |
{\ifcalc@eqnoswitch \else |
| 440 |
\global\calc@eqnoswitchtrue |
| 441 |
\stepcounter{equation} |
| 442 |
\gdef\@currentlabel{\p@equation\theequation} |
| 443 |
\fi} |
| 444 |
% \end{macrocode} |
| 445 |
|
| 446 |
\iffalse ================= calculation environment ===================\fi |
| 447 |
% \subsection{Main code: calculation and step} |
| 448 |
% The main idea of the calculation environment is to adapt \LaTeX's way |
| 449 |
% of formatting math expressions, and eqnarray in particular. Thus |
| 450 |
% "calculation" sets up a "\halign" with three columns: |
| 451 |
% \begin{itemize}\item[] |
| 452 |
% The 1st one for the step symbol (math mode) \\ |
| 453 |
% The 2nd for the hint and expression (math mode) \\ |
| 454 |
% The 3rd one for the comment and step/expr number (LR mode) |
| 455 |
% \end{itemize} |
| 456 |
% |
| 457 |
% \noindent |
| 458 |
% An eqno is placed in a large "\hbox" of length "\linewidth" which |
| 459 |
% itself is considered of zero length and placed at the right in the |
| 460 |
% last column. By suitable fill's the eqno then appears either at the |
| 461 |
% left or right side of that large "\hbox", and thus at the left or |
| 462 |
% right side of the page. |
| 463 |
% |
| 464 |
% Command "\step" will be invoked inside an expression; thus, briefly |
| 465 |
% said, it should have this effect: ``end the expression, begin a new |
| 466 |
% line, print the step symbol and hint, and begin a new expression''. |
| 467 |
% |
| 468 |
% \subsubsection{The calculation environment} |
| 469 |
% \begin{environment}{calculation} |
| 470 |
% The "calculation" environment has one optional argument, for the step |
| 471 |
% symbol, the default being "\stepsymbol"; within the code for the |
| 472 |
% environment, the step symbol is known as "\calc@stepsymbol". |
| 473 |
% Because the step symbol and expr/hint must be set with zero |
| 474 |
% mathsurround, we set mathsurround to zero (by \TeX's "\m@th"), but |
| 475 |
% take measures to reset "\mathsurround" to its original value inside |
| 476 |
% hints. |
| 477 |
% |
| 478 |
% A newline command "\\" inside expressions is delegated to "\calc@cr" |
| 479 |
% (defined below). |
| 480 |
% |
| 481 |
% Inside the environment, command "\step" and environment |
| 482 |
% "subcalculation" are made available; outside the environment "\step" |
| 483 |
% and "subcalculation" may have another meaning. |
| 484 |
% |
| 485 |
% \begin{macrocode} |
| 486 |
|
| 487 |
\newenvironment{calculation}[1][\stepsymbol] |
| 488 |
{\setlength\calc@math\mathsurround |
| 489 |
\def\calc@origmath{\mathsurround\calc@math}% |
| 490 |
\abovedisplayskip\topsep |
| 491 |
\ifvmode\advance\abovedisplayskip\partopsep\fi |
| 492 |
\belowdisplayskip\abovedisplayskip |
| 493 |
\belowdisplayshortskip\abovedisplayskip |
| 494 |
\abovedisplayshortskip\abovedisplayskip |
| 495 |
\def\calc@stepsymbol{#1}% |
| 496 |
\tabskip\calc@indent plus 1fil |
| 497 |
\let\\=\calc@cr |
| 498 |
\def\step{\calc@step}% Make \step available inside calculations |
| 499 |
\def\subcalculation{\calc@subcalculation}% similarly subcalculation |
| 500 |
\def\endsubcalculation{\endcalc@subcalculation}% |
| 501 |
$$ |
| 502 |
\halign to \displaywidth |
| 503 |
\bgroup |
| 504 |
\tabskip\z@ \hfil\m@th$ ## $\hskip\calculcolsep |
| 505 |
& \m@th$ ## $\hfil\tabskip 0pt plus 1\calc@fill |
| 506 |
& \hfil\llap{##}\tabskip\z@ |
| 507 |
\cr |
| 508 |
\calc@beginEXPR |
| 509 |
} |
| 510 |
% \end{macrocode} |
| 511 |
% In the preceding line, the "\halign" declaration ends with the |
| 512 |
% beginning of a math expression ("\calc@beginEXPR", which will skip to |
| 513 |
% the the 2nd column, that is, the column for expressions). The |
| 514 |
% following line closes the environment; it ends the last math |
| 515 |
% expression ("\calc@endEXPR", which will step over to the last column, |
| 516 |
% and print the eqno and comment): |
| 517 |
% \begin{macrocode} |
| 518 |
{\calc@endEXPR |
| 519 |
\egroup $$ \global\@ignoretrue\ignorespaces} |
| 520 |
|
| 521 |
% \end{macrocode} |
| 522 |
% \end{environment} |
| 523 |
% |
| 524 |
% \begin{macro}{\calc@cr} |
| 525 |
% The command "\\" within expressions is delegated to "\calc@cr"; it |
| 526 |
% ends the current expression, gives a little extra vspace, |
| 527 |
% and begins a new line of the expression: |
| 528 |
% \begin{macrocode} |
| 529 |
|
| 530 |
\newcommand\calc@cr{ |
| 531 |
\calc@endEXPR |
| 532 |
\noalign{\nopagebreak\vskip\jot} |
| 533 |
\calc@beginEXPR} |
| 534 |
|
| 535 |
% \end{macrocode} |
| 536 |
% From \LaTeX\ we've taken over the idea of extra "\jot" vertical space |
| 537 |
% between lines of one mathematical expression. The "\nopagebreak" |
| 538 |
% prevents a calculation to be split within or just after an expression; |
| 539 |
% that would be too confusing for the reader. |
| 540 |
% \end{macro} |
| 541 |
% |
| 542 |
\iffalse ================= step command ===================\fi |
| 543 |
% \subsubsection{The step command} |
| 544 |
% \begin{macro}{\...step} |
| 545 |
% Most of the work for the calculation environment, is done in command |
| 546 |
% "\step" or its companions "\calc@step", "\cal@@step" and |
| 547 |
% "\calc@@@step". |
| 548 |
% Recall that "\step" has been made available inside "calculation" by a |
| 549 |
% local definition that simply calls "\calc@step". This latter one first |
| 550 |
% checks whether the next token is a star~"*" (and stores this |
| 551 |
% information in the global "\calc@numberedstep") and then calls |
| 552 |
% "\calc@@step". This "\calc@@step" has one optional argument, the |
| 553 |
% default being "\calc@stepsymbol" (set by the calculation environment); |
| 554 |
% it calls "\calc@@@step" with the step symbol: |
| 555 |
% \begin{macrocode} |
| 556 |
|
| 557 |
\newcommand{\calc@step} |
| 558 |
{\@ifstar{\global\calc@numberedsteptrue\calc@@step} |
| 559 |
{\global\calc@numberedstepfalse\calc@@step}} |
| 560 |
|
| 561 |
\newcommand{\calc@@step}[1][\calc@stepsymbol]{\calc@@@step{#1}} |
| 562 |
|
| 563 |
% \end{macrocode} |
| 564 |
% |
| 565 |
% \noindent |
| 566 |
% \DescribeMacro{\calc@@@step} |
| 567 |
% Now, the final "\calc@@@step" has two obligatory arguments: |
| 568 |
% \begin{itemize}\item[] |
| 569 |
% "#1" = step symbol\\ |
| 570 |
% "#2" = hint lines possibly including several "\\"'s |
| 571 |
% \end{itemize} |
| 572 |
% As a preparation it checks whether the hint is empty and stores this |
| 573 |
% in "\calc@Hempty". Then it ends the current expression, does a |
| 574 |
% negative vskip if the hint is empty, and increases the equation |
| 575 |
% counter if the step is numbered: |
| 576 |
% \begin{macrocode} |
| 577 |
|
| 578 |
\newcommand{\calc@@@step}[2] |
| 579 |
{ \@ifmtarg{#2}{\global\calc@emptyHtrue}{\global\calc@emptyHfalse} |
| 580 |
\calc@endEXPR |
| 581 |
\ifcalc@emptyH \noalign{\vskip \calc@emptyHskip} |
| 582 |
\else \noalign{\nopagebreak\vskip\jot} |
| 583 |
\fi |
| 584 |
\ifcalc@numberedstep |
| 585 |
\refstepcounter{equation} |
| 586 |
\gdef\@currentlabel{\p@equation\theequation} |
| 587 |
\gdef\calc@stepno{\theequation} |
| 588 |
\fi |
| 589 |
% \end{macrocode} |
| 590 |
% After these preparations, print the step symbol in the 1st column: |
| 591 |
% \begin{macrocode} |
| 592 |
#1 & |
| 593 |
% \end{macrocode} |
| 594 |
% Now print the hint, if not empty. First take care of the |
| 595 |
% indentation, then take the hint lines as body of a tabular which |
| 596 |
% has "\HblockOPEN" and "\HblockCLOSE" as delimiters and put |
| 597 |
% "\HlineOPEN" in front of the 1st line and "\HlineCLOSE" after the |
| 598 |
% last line. These delimiters have been defined to be null depending |
| 599 |
% on the validity of option "block". The "tabular" comes from package |
| 600 |
% "delarray", and thus has the feature of allowing the delimiters |
| 601 |
% around the column specification. We also take care to restore |
| 602 |
% "\mathsurround" to its original value within each hint line. |
| 603 |
% \begin{macrocode} |
| 604 |
\ifcalc@emptyH |
| 605 |
\else |
| 606 |
\hskip\Hindent |
| 607 |
\begin{tabular}[\Hposv] |
| 608 |
\HblockOPEN{>{\calc@origmath\Hsepskip}l<{\Hsepskip}}\HblockCLOSE |
| 609 |
\HlineOPEN #2\HlineCLOSE |
| 610 |
\end{tabular} |
| 611 |
\fi |
| 612 |
& |
| 613 |
\ifcalc@numberedstep \calc@print@theequation \fi |
| 614 |
\cr |
| 615 |
% \end{macrocode} |
| 616 |
% Now, having completed the step-and-hint line by "\cr" and before |
| 617 |
% beginning the next math expression, do a negative vskip if the hint |
| 618 |
% was empty, and put some extra space between the hint and expression |
| 619 |
% (just as within expressions): |
| 620 |
% \begin{macrocode} |
| 621 |
\ifcalc@emptyH \noalign{\vskip \calc@emptyHskip} \fi |
| 622 |
\noalign{\vskip\jot} |
| 623 |
\calc@beginEXPR |
| 624 |
} |
| 625 |
|
| 626 |
% \end{macrocode} |
| 627 |
% \end{macro} |
| 628 |
% |
| 629 |
\iffalse ====================== Auxiliary commands ===================\fi |
| 630 |
% \subsubsection{Remaining auxiliary commands} |
| 631 |
% \begin{macro}{\calc@beginEXPR} |
| 632 |
% When "\calc@beginEXPR" is called, a new line of the "\halign" of |
| 633 |
% "calculation" is to be filled. Since no step symbol has to be printed, |
| 634 |
% we skip over to the next column (the 2nd one). This column is |
| 635 |
% processed in math mode, so nothing has to be done, except for making |
| 636 |
% "\doNumber" and "\comment" available and resetting the current comment |
| 637 |
% to ``nothing, yet'': |
| 638 |
% \begin{macrocode} |
| 639 |
|
| 640 |
\newcommand{\calc@beginEXPR} |
| 641 |
{& |
| 642 |
\def\doNumber{\calc@doNumber} |
| 643 |
\def\comment{\gdef\calc@comment} |
| 644 |
\gdef\calc@comment{} |
| 645 |
} |
| 646 |
|
| 647 |
% \end{macrocode} |
| 648 |
% \end{macro} |
| 649 |
% \begin{macro}{\calc@endEXPR} |
| 650 |
% When "\calc@endEXPR" is called, we simply step over to the last (3rd) |
| 651 |
% column by~"&" and print the eqno and comment (possibly null), and |
| 652 |
% close the line with "\cr": |
| 653 |
% \begin{macrocode} |
| 654 |
|
| 655 |
\newcommand{\calc@endEXPR} |
| 656 |
{ & \calc@@eqno\calc@@comment \cr } |
| 657 |
|
| 658 |
% \end{macrocode} |
| 659 |
% \end{macro} |
| 660 |
% \begin{macro}{\calc@@comment} |
| 661 |
% The type setting of comments and numbers is rather straightforward: at |
| 662 |
% the very right end of a wide "\hbox", which is pretended to be of zero |
| 663 |
% width, and surrounded by suitable fill's to shift them to the other |
| 664 |
% side, if needed. |
| 665 |
% \begin{macrocode} |
| 666 |
|
| 667 |
\newcommand{\calc@@comment} |
| 668 |
{\llap{\hbox to\linewidth |
| 669 |
{\calc@eqnoRfil \normalfont\normalcolor\calc@comment |
| 670 |
\calc@eqnoLfil}}} |
| 671 |
\newcommand{\calc@@eqno} |
| 672 |
{\ifcalc@eqnoswitch |
| 673 |
\calc@print@theequation |
| 674 |
\global\calc@eqnoswitchfalse |
| 675 |
\fi} |
| 676 |
\newcommand{\calc@print@theequation} |
| 677 |
{\llap{\hbox to\displaywidth |
| 678 |
{\calc@eqnoLfil \m@th\normalfont\normalcolor (\theequation)% |
| 679 |
\calc@eqnoRfil}}} |
| 680 |
|
| 681 |
% \end{macrocode} |
| 682 |
% \end{macro} |
| 683 |
% |
| 684 |
\iffalse ===================== subcalculation ===================== \fi |
| 685 |
% \section{Subcalculation} |
| 686 |
% |
| 687 |
% There are several problems in using the "calculation" environment |
| 688 |
% within hints. First, the width need be determined. (When option "fleqn" |
| 689 |
% is valid, then the width is more or less the line length minus these |
| 690 |
% three: "\calc@indent", width of widest step symbol, "\Hindent".) |
| 691 |
% Second, even if the width for the sub calculation is known, it is hard |
| 692 |
% to get the eqno and comment at the right place on the page. Third, in |
| 693 |
% all my attempts, a sub calculation in the hint of a numbered step |
| 694 |
% takes the number for it self. (This could be solved by implementing a |
| 695 |
% stack or push down store in \TeX\ or a kind of recursive commands that |
| 696 |
% build their own stack.) In view of all this, we forbid sub |
| 697 |
% calculations to have numbered steps, numbered expressions, and |
| 698 |
% comments in expressions. (This seems reasonable; after all, a |
| 699 |
% subcalculation within a hint should be very simple and not |
| 700 |
% ``view-able'' from the outside.) Also, we pretend the calculation to |
| 701 |
% have zero width; it is the users responsibility to observe overfull |
| 702 |
% lines! Finally, as a kind of fine tuning the lay-out, we halve the |
| 703 |
% indentation for the calculation, and make sub calculations flush left. |
| 704 |
% Actions within "subcalculation" must not overwrite gobal |
| 705 |
% variables of environment "calculation", of course. So, we have to |
| 706 |
% introduce some new global variables: |
| 707 |
% \begin{macrocode} |
| 708 |
|
| 709 |
\newif\ifsubcalc@emptyH |
| 710 |
\newcommand{\subcalc@stepsymbol}{\relax} |
| 711 |
|
| 712 |
% \end{macrocode} |
| 713 |
% |
| 714 |
% \begin{environment}{subcalculation} |
| 715 |
% Since numbering and comments are not allowed, we use only two columns. |
| 716 |
% |
| 717 |
% \begin{macrocode} |
| 718 |
|
| 719 |
\newenvironment{calc@subcalculation}[1][\stepsymbol]{% |
| 720 |
\begin{minipage}[c]{0pt} |
| 721 |
\abovedisplayskip 0pt |
| 722 |
\belowdisplayskip \abovedisplayskip |
| 723 |
\belowdisplayshortskip\belowdisplayskip |
| 724 |
\abovedisplayshortskip\abovedisplayskip |
| 725 |
\def\subcalc@stepsymbol{#1}% |
| 726 |
\@ifundefined{mathindent}{\calc@indent\leftmargini}{}% |
| 727 |
\divide\calc@indent by 2 |
| 728 |
\tabskip\calc@indent plus 1fil |
| 729 |
\let\\=\subcalc@cr |
| 730 |
\def\step{\subcalc@step}% Make \step available |
| 731 |
\def\comment |
| 732 |
{\@latex@error{No \string\comment\space in subcalculations}}% |
| 733 |
$$ |
| 734 |
\m@th |
| 735 |
\halign |
| 736 |
\bgroup |
| 737 |
\tabskip\z@ \hfil$ ## $\hskip\calculcolsep |
| 738 |
& |
| 739 |
$ ## $\hfil\tabskip 0pt plus 1\calc@fill |
| 740 |
\cr |
| 741 |
& |
| 742 |
\global\@ignoretrue |
| 743 |
} |
| 744 |
{ \raisebox{0pt}[0pt][1.5ex]{} |
| 745 |
\cr \egroup $$\par\end{minipage} |
| 746 |
\ignorespaces} |
| 747 |
% \end{macrocode} |
| 748 |
% \end{environment} |
| 749 |
% |
| 750 |
% \noindent |
| 751 |
% How to deal with "\\" (which will invoke "\subcalc@cr") in hints and |
| 752 |
% exprs: Close the current line (of the "\halign") and in the next line |
| 753 |
% skip the column for the step symbol: |
| 754 |
% \begin{macrocode} |
| 755 |
|
| 756 |
\newcommand\subcalc@cr |
| 757 |
{\cr |
| 758 |
\noalign{\nopagebreak\vskip\jot} |
| 759 |
& |
| 760 |
\global\@ignoretrue\ignorespaces |
| 761 |
} |
| 762 |
|
| 763 |
% \end{macrocode} |
| 764 |
% |
| 765 |
% \begin{macro}{\subcalc@step} |
| 766 |
% A step in a subcalculation is more or less the same as a step in the |
| 767 |
% normal calculation, except that numbering is not allowed: |
| 768 |
% \begin{macrocode} |
| 769 |
|
| 770 |
\newcommand{\subcalc@step} |
| 771 |
{\@ifstar{\subcalc@@stepSTAR}{\subcalc@@step}} |
| 772 |
|
| 773 |
\newcommand{\subcalc@@stepSTAR}[1] |
| 774 |
{\@latex@error |
| 775 |
{No \string\step* in subcalculations; use \string\step.}} |
| 776 |
|
| 777 |
\newcommand{\subcalc@@step}[1][\subcalc@stepsymbol] |
| 778 |
{\subcalc@@@step{#1}} |
| 779 |
|
| 780 |
\newcommand{\subcalc@@@step}[2] |
| 781 |
{\@ifmtarg{#2} |
| 782 |
{\global\subcalc@emptyHtrue}{\global\subcalc@emptyHfalse} |
| 783 |
\cr |
| 784 |
\ifsubcalc@emptyH |
| 785 |
\noalign{\vskip \calc@emptyHskip} |
| 786 |
\else |
| 787 |
\noalign{\nopagebreak\vskip\jot} |
| 788 |
\fi |
| 789 |
#1 & |
| 790 |
\ifsubcalc@emptyH \else |
| 791 |
\hskip\Hindent |
| 792 |
\hbox to 0pt {% |
| 793 |
\begin{tabular}[\Hposv] |
| 794 |
\HblockOPEN{>{\calc@origmath\Hsepskip}l<{\Hsepskip}}\HblockCLOSE |
| 795 |
\HlineOPEN #2\HlineCLOSE |
| 796 |
\end{tabular}} |
| 797 |
\fi |
| 798 |
\cr |
| 799 |
\ifsubcalc@emptyH \noalign{\vskip \calc@emptyHskip} \fi |
| 800 |
& \global\@ignoretrue |
| 801 |
} |
| 802 |
% \end{macrocode} |
| 803 |
% \end{macro} |
| 804 |
% \iffalse =============== end of calculation.dtx =====================\fi |