summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/calculation/calculation.sty
blob: 8653e0b3433b886fd10f0d3ceaa5e3e9549102c3 (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
%%
%% This is file `calculation.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% calculation.dtx  (with options: `exit')
%%    Calculations, in the style of Feijen-Dijkstra
%%    Copyright 2014, Maarten Fokkinga (m.m.fokkinga@gmail.com)
%% 
%%    This work may be distributed and/or modified under the
%%    conditions of the LaTeX Project Public License, either version 1.3
%%    of this license or (at your option) any later version.
%%    The latest version of this license is in
%%      http://www.latex-project.org/lppl.txt
%%    and version 1.3 or later is part of all distributions of LaTeX
%%    version 2005/12/01 or later.
%% 
%%    This work has the LPPL maintenance status `maintained'.
%% 
%%    The Current Maintainer of this work is Maarten Fokkinga.
%% 
%%    This work consists of the files calculation.dtx and calculation.ins
%%    and the derived file calculation.sty.

%% ==================== Usage ====================================
%%
%%    \begin{calculation}[SYMBOL]
%%       EXPRESSION   <-- may contain \doNumber and \comment{TEXT}
%%    \step*[SYMBOL]{HINT}
%%       EXPRESSION   <-- may contain \doNumber and \comment{TEXT}
%%    \step*[SYMBOL]{HINT}
%%       EXPRESSION   <-- may contain \doNumber and \comment{TEXT}
%%       ...
%%    \end{calculation}
%%    Each asterisk "*" and each part "[...]" is optional.
%%
%% -------------- DOCUMENT STYLE PARAMETERS ------------------------
%% options:
%%    fleqn, leqno, block
%% commands:
%%    \newcommand{\stepsymbol}{=}
%%    \newcommand{\Hblockopen}{|}
%%    \newcommand{\Hblockclose}{.}
%%    \newcommand{\Hlineopen}{\lbrace}
%%    \newcommand{\Hlineclose}{\rbrace}
%%    \newcommand{\Hindent}{1em}
%%    \newcommand{\Hsep}{1ex}
%%    \newcommand{\calculcolsep}{\arraycolsep}
%%    \newcommand{\Hposv}{t}
%%     % vertical position of the step symbol in front of a block hint
%% These default values may be redefined by "\renewcommand".
%%
\iffalse ============================== code =======================\fi

\def\fileversion{1.00}
\def\filedate{5 Jan 2015}
\def\docdate{5 Jan 2015}

\ProvidesPackage{calculation}[2014/12/05 Format reasoned calculations]



\DeclareOption{fleqn}{\calc@fleqn}
\DeclareOption{leqno}{\calc@leqno}
\DeclareOption{block}{\calc@blocktrue}


\newcommand{\calc@fill}{\relax}
\newcommand{\calc@eqnoLfil}{\relax}
\newcommand{\calc@eqnoRfil}{\relax}


\newcommand{\calc@leqno}
    {\def\calc@eqnoLfil{}\def\calc@eqnoRfil{\hfil}}


\newcommand{\calc@reqno}
    {\def\calc@eqnoRfil{}\def\calc@eqnoLfil{\hfil}}


\newcommand{\calc@ceqn}{\def\calc@fill{fil}}


\newcommand{\calc@fleqn}
   {\@ifundefined{mathindent}
         {\calc@indent\leftmargini}{\calc@indent\mathindent}
    \def\calc@fill{fill}}


\calc@ceqn
\calc@reqno
\newif\ifcalc@block \calc@blockfalse
\newdimen\calc@indent\calc@indent\z@skip


\ProcessOptions


\RequirePackage{delarray}


\newcommand\@ifmtarg[3]
   {{\def\myempty{}\def\myarg{#1}\ifx\myempty\myarg{#2}\else{#3}\fi}}
   %% usage:  \@ifmtarg {arg} {then} {else}


\newcommand{\stepsymbol}{=}
\newcommand{\Hblockopen}{|}
\newcommand{\Hblockclose}{.}
\newcommand{\Hlineopen}{\lbrace}
\newcommand{\Hlineclose}{\rbrace}
\newcommand{\Hindent}{1em}
\newcommand{\Hsep}{1ex}
\newcommand{\Hposv}{t}
\newcommand{\calculcolsep}{\arraycolsep}


\newcommand{\Hsepskip}{\hbox to \Hsep {}}
\newcommand{\HlineOPEN}
  {\ifcalc@block\else\makebox[0pt][r]{\m@th$\Hlineopen$\Hsepskip}\fi
   \ignorespaces}
\newcommand{\HlineCLOSE}
  {\ifcalc@block\else\makebox[0pt][l]{\m@th\Hsepskip$\Hlineclose$}\fi}


\newcommand{\HblockOPEN}{\ifcalc@block\Hblockopen\else.\fi}
\newcommand{\HblockCLOSE}{\ifcalc@block\Hblockclose\else.\fi}

\newcommand{\calc@origmath}{\relax}
\newcommand{\calc@stepsymbol}{\relax}
\newlength\calc@math       % to store the value of \mathsurround
\newif\ifcalc@emptyH       % for temporary local use only
\newif\ifcalc@numberedstep % true if currently in \step*
\newif\ifcalc@eqnoswitch   % true if an eqno will be produced
\newcommand{\calc@emptyHskip}{-0.5\baselineskip}% for empty hints
\calc@eqnoswitchfalse
\newcommand{\calc@doNumber}
    {\ifcalc@eqnoswitch \else
       \global\calc@eqnoswitchtrue
       \stepcounter{equation}
       \gdef\@currentlabel{\p@equation\theequation}
     \fi}

\iffalse ================= calculation environment ===================\fi

\newenvironment{calculation}[1][\stepsymbol]
 {\setlength\calc@math\mathsurround
  \def\calc@origmath{\mathsurround\calc@math}%
  \abovedisplayskip\topsep
  \ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  \def\calc@stepsymbol{#1}%
  \tabskip\calc@indent plus 1fil
  \let\\=\calc@cr
  \def\step{\calc@step}% Make \step available inside calculations
  \def\subcalculation{\calc@subcalculation}% similarly subcalculation
  \def\endsubcalculation{\endcalc@subcalculation}%
  $$
    \halign to \displaywidth
    \bgroup
        \tabskip\z@ \hfil\m@th$ ## $\hskip\calculcolsep
     &  \m@th$ ## $\hfil\tabskip 0pt plus 1\calc@fill
     &  \hfil\llap{##}\tabskip\z@
    \cr
    \calc@beginEXPR
 }
 {\calc@endEXPR
  \egroup $$ \global\@ignoretrue\ignorespaces}


\newcommand\calc@cr{
     \calc@endEXPR
     \noalign{\nopagebreak\vskip\jot}
     \calc@beginEXPR}

\iffalse ================= step command ===================\fi

\newcommand{\calc@step}
  {\@ifstar{\global\calc@numberedsteptrue\calc@@step}
           {\global\calc@numberedstepfalse\calc@@step}}

\newcommand{\calc@@step}[1][\calc@stepsymbol]{\calc@@@step{#1}}


\newcommand{\calc@@@step}[2]
 { \@ifmtarg{#2}{\global\calc@emptyHtrue}{\global\calc@emptyHfalse}
   \calc@endEXPR
   \ifcalc@emptyH \noalign{\vskip \calc@emptyHskip}
     \else \noalign{\nopagebreak\vskip\jot}
   \fi
   \ifcalc@numberedstep
     \refstepcounter{equation}
     \gdef\@currentlabel{\p@equation\theequation}
     \gdef\calc@stepno{\theequation}
   \fi
   #1 &
   \ifcalc@emptyH
   \else
      \hskip\Hindent
      \begin{tabular}[\Hposv]
      \HblockOPEN{>{\calc@origmath\Hsepskip}l<{\Hsepskip}}\HblockCLOSE
      \HlineOPEN #2\HlineCLOSE
      \end{tabular}
   \fi
   &
   \ifcalc@numberedstep \calc@print@theequation \fi
   \cr
   \ifcalc@emptyH \noalign{\vskip \calc@emptyHskip} \fi
   \noalign{\vskip\jot}
   \calc@beginEXPR
 }

\iffalse ====================== Auxiliary commands ===================\fi

\newcommand{\calc@beginEXPR}
   {&
    \def\doNumber{\calc@doNumber}
    \def\comment{\gdef\calc@comment}
    \gdef\calc@comment{}
    }


\newcommand{\calc@endEXPR}
   { & \calc@@eqno\calc@@comment \cr }


\newcommand{\calc@@comment}
   {\llap{\hbox to\linewidth
       {\calc@eqnoRfil \normalfont\normalcolor\calc@comment
        \calc@eqnoLfil}}}
\newcommand{\calc@@eqno}
   {\ifcalc@eqnoswitch
     \calc@print@theequation
     \global\calc@eqnoswitchfalse
    \fi}
\newcommand{\calc@print@theequation}
   {\llap{\hbox to\displaywidth
      {\calc@eqnoLfil \m@th\normalfont\normalcolor (\theequation)%
       \calc@eqnoRfil}}}

\iffalse ===================== subcalculation ===================== \fi

\newif\ifsubcalc@emptyH
\newcommand{\subcalc@stepsymbol}{\relax}


\newenvironment{calc@subcalculation}[1][\stepsymbol]{%
  \begin{minipage}[c]{0pt}
  \abovedisplayskip 0pt
  \belowdisplayskip \abovedisplayskip
  \belowdisplayshortskip\belowdisplayskip
  \abovedisplayshortskip\abovedisplayskip
  \def\subcalc@stepsymbol{#1}%
  \@ifundefined{mathindent}{\calc@indent\leftmargini}{}%
  \divide\calc@indent by 2
  \tabskip\calc@indent plus 1fil
  \let\\=\subcalc@cr
  \def\step{\subcalc@step}% Make \step available
  \def\comment
     {\@latex@error{No \string\comment\space in subcalculations}}%
  $$
  \m@th
    \halign
    \bgroup
      \tabskip\z@ \hfil$ ## $\hskip\calculcolsep
     &
      $ ## $\hfil\tabskip 0pt plus 1\calc@fill
    \cr
    &
    \global\@ignoretrue
  }
  { \raisebox{0pt}[0pt][1.5ex]{}
    \cr \egroup $$\par\end{minipage}
    \ignorespaces}

\newcommand\subcalc@cr
    {\cr
     \noalign{\nopagebreak\vskip\jot}
     &
     \global\@ignoretrue\ignorespaces
    }


\newcommand{\subcalc@step}
    {\@ifstar{\subcalc@@stepSTAR}{\subcalc@@step}}

\newcommand{\subcalc@@stepSTAR}[1]
    {\@latex@error
     {No \string\step* in subcalculations; use \string\step.}}

\newcommand{\subcalc@@step}[1][\subcalc@stepsymbol]
     {\subcalc@@@step{#1}}

\newcommand{\subcalc@@@step}[2]
 {\@ifmtarg{#2}
      {\global\subcalc@emptyHtrue}{\global\subcalc@emptyHfalse}
  \cr
  \ifsubcalc@emptyH
    \noalign{\vskip \calc@emptyHskip}
  \else
    \noalign{\nopagebreak\vskip\jot}
  \fi
  #1 &
  \ifsubcalc@emptyH \else
    \hskip\Hindent
    \hbox to 0pt {%
    \begin{tabular}[\Hposv]
    \HblockOPEN{>{\calc@origmath\Hsepskip}l<{\Hsepskip}}\HblockCLOSE
        \HlineOPEN #2\HlineCLOSE
    \end{tabular}}
  \fi
  \cr
    \ifsubcalc@emptyH \noalign{\vskip \calc@emptyHskip} \fi
  & \global\@ignoretrue
  }
\endinput
%%
%% End of file `calculation.sty'.