1 % Mathpartir --- Math Paragraph for Typesetting Inference Rules |
|
2 % |
|
3 % Copyright (C) 2001, 2002, 2003 Didier Rémy |
|
4 % |
|
5 % Author : Didier Remy |
|
6 % Version : 1.1.1 |
|
7 % Bug Reports : to author |
|
8 % Web Site : http://pauillac.inria.fr/~remy/latex/ |
|
9 % |
|
10 % WhizzyTeX is free software; you can redistribute it and/or modify |
|
11 % it under the terms of the GNU General Public License as published by |
|
12 % the Free Software Foundation; either version 2, or (at your option) |
|
13 % any later version. |
|
14 % |
|
15 % Mathpartir is distributed in the hope that it will be useful, |
|
16 % but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18 % GNU General Public License for more details |
|
19 % (http://pauillac.inria.fr/~remy/license/GPL). |
|
20 % |
|
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
22 % File mathpartir.sty (LaTeX macros) |
|
23 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
24 |
|
25 \NeedsTeXFormat{LaTeX2e} |
|
26 \ProvidesPackage{mathpartir} |
|
27 [2003/07/10 version 1.1.1 Math Paragraph for Typesetting Inference Rules] |
|
28 |
|
29 %% |
|
30 |
|
31 %% Identification |
|
32 %% Preliminary declarations |
|
33 |
|
34 \RequirePackage {keyval} |
|
35 |
|
36 %% Options |
|
37 %% More declarations |
|
38 |
|
39 %% PART I: Typesetting maths in paragraphe mode |
|
40 |
|
41 \newdimen \mpr@tmpdim |
|
42 |
|
43 % To ensure hevea \hva compatibility, \hva should expands to nothing |
|
44 % in mathpar or in inferrule |
|
45 \let \mpr@hva \empty |
|
46 |
|
47 %% normal paragraph parametters, should rather be taken dynamically |
|
48 \def \mpr@savepar {% |
|
49 \edef \MathparNormalpar |
|
50 {\noexpand \lineskiplimit \the\lineskiplimit |
|
51 \noexpand \lineskip \the\lineskip}% |
|
52 } |
|
53 |
|
54 \def \mpr@rulelineskip {\lineskiplimit=0.3em\lineskip=0.2em plus 0.1em} |
|
55 \def \mpr@lesslineskip {\lineskiplimit=0.6em\lineskip=0.5em plus 0.2em} |
|
56 \def \mpr@lineskip {\lineskiplimit=1.2em\lineskip=1.2em plus 0.2em} |
|
57 \let \MathparLineskip \mpr@lineskip |
|
58 \def \mpr@paroptions {\MathparLineskip} |
|
59 \let \mpr@prebindings \relax |
|
60 |
|
61 \newskip \mpr@andskip \mpr@andskip 2em plus 0.5fil minus 0.5em |
|
62 |
|
63 \def \mpr@goodbreakand |
|
64 {\hskip -\mpr@andskip \penalty -1000\hskip \mpr@andskip} |
|
65 \def \mpr@and {\hskip \mpr@andskip} |
|
66 \def \mpr@andcr {\penalty 50\mpr@and} |
|
67 \def \mpr@cr {\penalty -10000\mpr@and} |
|
68 \def \mpr@eqno #1{\mpr@andcr #1\hskip 0em plus -1fil \penalty 10} |
|
69 |
|
70 \def \mpr@bindings {% |
|
71 \let \and \mpr@andcr |
|
72 \let \par \mpr@andcr |
|
73 \let \\\mpr@cr |
|
74 \let \eqno \mpr@eqno |
|
75 \let \hva \mpr@hva |
|
76 } |
|
77 \let \MathparBindings \mpr@bindings |
|
78 |
|
79 % \@ifundefined {ignorespacesafterend} |
|
80 % {\def \ignorespacesafterend {\aftergroup \ignorespaces} |
|
81 |
|
82 \newenvironment{mathpar}[1][] |
|
83 {$$\mpr@savepar \parskip 0em \hsize \linewidth \centering |
|
84 \vbox \bgroup \mpr@prebindings \mpr@paroptions #1\ifmmode $\else |
|
85 \noindent $\displaystyle\fi |
|
86 \MathparBindings} |
|
87 {\unskip \ifmmode $\fi\egroup $$\ignorespacesafterend} |
|
88 |
|
89 % \def \math@mathpar #1{\setbox0 \hbox {$\displaystyle #1$}\ifnum |
|
90 % \wd0 < \hsize $$\box0$$\else \bmathpar #1\emathpar \fi} |
|
91 |
|
92 %%% HOV BOXES |
|
93 |
|
94 \def \mathvbox@ #1{\hbox \bgroup \mpr@normallineskip |
|
95 \vbox \bgroup \tabskip 0em \let \+ \cr |
|
96 \halign \bgroup \hfil $##$\hfil\cr #1\crcr \egroup \egroup |
|
97 \egroup} |
|
98 |
|
99 \def \mathhvbox@ #1{\setbox0 \hbox {\let \+\qquad $#1$}\ifnum \wd0 < \hsize |
|
100 \box0\else \mathvbox {#1}\fi} |
|
101 |
|
102 |
|
103 %% Part II -- operations on lists |
|
104 |
|
105 \newtoks \mpr@lista |
|
106 \newtoks \mpr@listb |
|
107 |
|
108 \long \def\mpr@cons #1\mpr@to#2{\mpr@lista {\+{#1}}\mpr@listb \expandafter |
|
109 {#2}\edef #2{\the \mpr@lista \the \mpr@listb}} |
|
110 |
|
111 \long \def\mpr@snoc #1\mpr@to#2{\mpr@lista {\+{#1}}\mpr@listb \expandafter |
|
112 {#2}\edef #2{\the \mpr@listb\the\mpr@lista}} |
|
113 |
|
114 \long \def \mpr@concat#1=#2\mpr@to#3{\mpr@lista \expandafter {#2}\mpr@listb |
|
115 \expandafter {#3}\edef #1{\the \mpr@listb\the\mpr@lista}} |
|
116 |
|
117 \def \mpr@head #1\mpr@to #2{\expandafter \mpr@head@ #1\mpr@head@ #1#2} |
|
118 \long \def \mpr@head@ #1#2\mpr@head@ #3#4{\def #4{#1}\def#3{#2}} |
|
119 |
|
120 \def \mpr@flatten #1\mpr@to #2{\expandafter \mpr@flatten@ #1\mpr@flatten@ #1#2} |
|
121 \long \def \mpr@flatten@ \+#1\+#2\mpr@flatten@ #3#4{\def #4{#1}\def #3{\+#2}} |
|
122 |
|
123 \def \mpr@makelist #1\mpr@to #2{\def \mpr@all {#1}% |
|
124 \mpr@lista {\+}\mpr@listb \expandafter {\mpr@all}\edef \mpr@all {\the |
|
125 \mpr@lista \the \mpr@listb \the \mpr@lista}\let #2\empty |
|
126 \def \mpr@stripof ##1##2\mpr@stripend{\def \mpr@stripped{##2}}\loop |
|
127 \mpr@flatten \mpr@all \mpr@to \mpr@one |
|
128 \expandafter \mpr@snoc \mpr@one \mpr@to #2\expandafter \mpr@stripof |
|
129 \mpr@all \mpr@stripend |
|
130 \ifx \mpr@stripped \empty \let \mpr@isempty 0\else \let \mpr@isempty 1\fi |
|
131 \ifx 1\mpr@isempty |
|
132 \repeat |
|
133 } |
|
134 |
|
135 %% Part III -- Type inference rules |
|
136 |
|
137 \def \mpr@rev #1\mpr@to #2{\let \mpr@tmp \empty |
|
138 \def \+##1{\mpr@cons ##1\mpr@to \mpr@tmp}#1\let #2\mpr@tmp} |
|
139 |
|
140 \newif \if@premisse |
|
141 \newbox \mpr@hlist |
|
142 \newbox \mpr@vlist |
|
143 \newif \ifmpr@center \mpr@centertrue |
|
144 \def \mpr@htovlist {% |
|
145 \setbox \mpr@hlist |
|
146 \hbox {\strut |
|
147 \ifmpr@center \hskip -0.5\wd\mpr@hlist\fi |
|
148 \unhbox \mpr@hlist}% |
|
149 \setbox \mpr@vlist |
|
150 \vbox {\if@premisse \box \mpr@hlist \unvbox \mpr@vlist |
|
151 \else \unvbox \mpr@vlist \box \mpr@hlist |
|
152 \fi}% |
|
153 } |
|
154 % OLD version |
|
155 % \def \mpr@htovlist {% |
|
156 % \setbox \mpr@hlist |
|
157 % \hbox {\strut \hskip -0.5\wd\mpr@hlist \unhbox \mpr@hlist}% |
|
158 % \setbox \mpr@vlist |
|
159 % \vbox {\if@premisse \box \mpr@hlist \unvbox \mpr@vlist |
|
160 % \else \unvbox \mpr@vlist \box \mpr@hlist |
|
161 % \fi}% |
|
162 % } |
|
163 |
|
164 \def \mpr@sep{2em} |
|
165 \def \mpr@blank { } |
|
166 \def \mpr@hovbox #1#2{\hbox |
|
167 \bgroup |
|
168 \ifx #1T\@premissetrue |
|
169 \else \ifx #1B\@premissefalse |
|
170 \else |
|
171 \PackageError{mathpartir} |
|
172 {Premisse orientation should either be P or B} |
|
173 {Fatal error in Package}% |
|
174 \fi \fi |
|
175 \def \@test {#2}\ifx \@test \mpr@blank\else |
|
176 \setbox \mpr@hlist \hbox {}% |
|
177 \setbox \mpr@vlist \vbox {}% |
|
178 \if@premisse \let \snoc \mpr@cons \else \let \snoc \mpr@snoc \fi |
|
179 \let \@hvlist \empty \let \@rev \empty |
|
180 \mpr@tmpdim 0em |
|
181 \expandafter \mpr@makelist #2\mpr@to \mpr@flat |
|
182 \if@premisse \mpr@rev \mpr@flat \mpr@to \@rev \else \let \@rev \mpr@flat \fi |
|
183 \def \+##1{% |
|
184 \def \@test {##1}\ifx \@test \empty |
|
185 \mpr@htovlist |
|
186 \mpr@tmpdim 0em %%% last bug fix not extensively checked |
|
187 \else |
|
188 \setbox0 \hbox{$\displaystyle {##1}$}\relax |
|
189 \advance \mpr@tmpdim by \wd0 |
|
190 %\mpr@tmpdim 1.02\mpr@tmpdim |
|
191 \ifnum \mpr@tmpdim < \hsize |
|
192 \ifnum \wd\mpr@hlist > 0 |
|
193 \if@premisse |
|
194 \setbox \mpr@hlist |
|
195 \hbox {\unhbox0 \hskip \mpr@sep \unhbox \mpr@hlist}% |
|
196 \else |
|
197 \setbox \mpr@hlist |
|
198 \hbox {\unhbox \mpr@hlist \hskip \mpr@sep \unhbox0}% |
|
199 \fi |
|
200 \else |
|
201 \setbox \mpr@hlist \hbox {\unhbox0}% |
|
202 \fi |
|
203 \else |
|
204 \ifnum \wd \mpr@hlist > 0 |
|
205 \mpr@htovlist |
|
206 \mpr@tmpdim \wd0 |
|
207 \fi |
|
208 \setbox \mpr@hlist \hbox {\unhbox0}% |
|
209 \fi |
|
210 \advance \mpr@tmpdim by \mpr@sep |
|
211 \fi |
|
212 }% |
|
213 \@rev |
|
214 \mpr@htovlist |
|
215 \ifmpr@center \hskip \wd\mpr@vlist\fi \box \mpr@vlist |
|
216 \fi |
|
217 \egroup |
|
218 } |
|
219 |
|
220 %%% INFERENCE RULES |
|
221 |
|
222 \@ifundefined{@@over}{% |
|
223 \let\@@over\over % fallback if amsmath is not loaded |
|
224 \let\@@overwithdelims\overwithdelims |
|
225 \let\@@atop\atop \let\@@atopwithdelims\atopwithdelims |
|
226 \let\@@above\above \let\@@abovewithdelims\abovewithdelims |
|
227 }{} |
|
228 |
|
229 |
|
230 \def \mpr@@fraction #1#2{\hbox {\advance \hsize by -0.5em |
|
231 $\displaystyle {#1\@@over #2}$}} |
|
232 \let \mpr@fraction \mpr@@fraction |
|
233 \def \mpr@@reduce #1#2{\hbox |
|
234 {$\lower 0.01pt \mpr@@fraction {#1}{#2}\mkern -15mu\rightarrow$}} |
|
235 \def \mpr@@rewrite #1#2#3{\hbox |
|
236 {$\lower 0.01pt \mpr@@fraction {#2}{#3}\mkern -8mu#1$}} |
|
237 \def \mpr@infercenter #1{\vcenter {\mpr@hovbox{T}{#1}}} |
|
238 |
|
239 \def \mpr@empty {} |
|
240 \def \mpr@inferrule |
|
241 {\bgroup |
|
242 \ifnum \linewidth<\hsize \hsize \linewidth\fi |
|
243 \mpr@rulelineskip |
|
244 \let \and \qquad |
|
245 \let \hva \mpr@hva |
|
246 \let \@rulename \mpr@empty |
|
247 \let \@rule@options \mpr@empty |
|
248 \mpr@inferrule@} |
|
249 \newcommand {\mpr@inferrule@}[3][] |
|
250 {\everymath={\displaystyle}% |
|
251 \def \@test {#2}\ifx \empty \@test |
|
252 \setbox0 \hbox {$\vcenter {\mpr@hovbox{B}{#3}}$}% |
|
253 \else |
|
254 \def \@test {#3}\ifx \empty \@test |
|
255 \setbox0 \hbox {$\vcenter {\mpr@hovbox{T}{#2}}$}% |
|
256 \else |
|
257 \setbox0 \mpr@fraction {\mpr@hovbox{T}{#2}}{\mpr@hovbox{B}{#3}}% |
|
258 \fi \fi |
|
259 \def \@test {#1}\ifx \@test\empty \box0 |
|
260 \else \vbox |
|
261 %%% Suggestion de Francois pour les etiquettes longues |
|
262 %%% {\hbox to \wd0 {\RefTirName {#1}\hfil}\box0}\fi |
|
263 {\hbox {\RefTirName {#1}}\box0}\fi |
|
264 \egroup} |
|
265 |
|
266 \def \mpr@vdotfil #1{\vbox to #1{\leaders \hbox{$\cdot$} \vfil}} |
|
267 |
|
268 % They are two forms |
|
269 % \inferrule [label]{[premisses}{conclusions} |
|
270 % or |
|
271 % \inferrule* [options]{[premisses}{conclusions} |
|
272 % |
|
273 % Premisses and conclusions are lists of elements separated by \+ |
|
274 % Each \+ produces a break, attempting horizontal breaks if possible, |
|
275 % and vertical breaks if needed. |
|
276 % |
|
277 % An empty element obtained by \+\+ produces a vertical break in all cases. |
|
278 % |
|
279 % The former rule is aligned on the fraction bar. |
|
280 % The optional label appears on top of the rule |
|
281 % The second form to be used in a derivation tree is aligned on the last |
|
282 % line of its conclusion |
|
283 % |
|
284 % The second form can be parameterized, using the key=val interface. The |
|
285 % folloiwng keys are recognized: |
|
286 % |
|
287 % width set the width of the rule to val |
|
288 % narrower set the width of the rule to val\hsize |
|
289 % before execute val at the beginning/left |
|
290 % lab put a label [Val] on top of the rule |
|
291 % lskip add negative skip on the right |
|
292 % left put a left label [Val] |
|
293 % Left put a left label [Val], ignoring its width |
|
294 % right put a right label [Val] |
|
295 % Right put a right label [Val], ignoring its width |
|
296 % leftskip skip negative space on the left-hand side |
|
297 % rightskip skip negative space on the right-hand side |
|
298 % vdots lift the rule by val and fill vertical space with dots |
|
299 % after execute val at the end/right |
|
300 % |
|
301 % Note that most options must come in this order to avoid strange |
|
302 % typesetting (in particular leftskip must preceed left and Left and |
|
303 % rightskip must follow Right or right; vdots must come last |
|
304 % or be only followed by rightskip. |
|
305 % |
|
306 |
|
307 \define@key {mprset}{flushleft}[]{\mpr@centerfalse} |
|
308 \define@key {mprset}{center}[]{\mpr@centertrue} |
|
309 \def \mprset #1{\setkeys{mprset}{#1}} |
|
310 |
|
311 \newbox \mpr@right |
|
312 \define@key {mpr}{flushleft}[]{\mpr@centerfalse} |
|
313 \define@key {mpr}{center}[]{\mpr@centertrue} |
|
314 \define@key {mpr}{left}{\setbox0 \hbox {$\TirName {#1}\;$}\relax |
|
315 \advance \hsize by -\wd0\box0} |
|
316 \define@key {mpr}{width}{\hsize #1} |
|
317 \define@key {mpr}{sep}{\def\mpr@sep{#1}} |
|
318 \define@key {mpr}{before}{#1} |
|
319 \define@key {mpr}{lab}{\let \RefTirName \TirName \def \mpr@rulename {#1}} |
|
320 \define@key {mpr}{Lab}{\let \RefTirName \TirName \def \mpr@rulename {#1}} |
|
321 \define@key {mpr}{narrower}{\hsize #1\hsize} |
|
322 \define@key {mpr}{leftskip}{\hskip -#1} |
|
323 \define@key {mpr}{reduce}[]{\let \mpr@fraction \mpr@@reduce} |
|
324 \define@key {mpr}{rightskip} |
|
325 {\setbox \mpr@right \hbox {\unhbox \mpr@right \hskip -#1}} |
|
326 \define@key {mpr}{LEFT}{\setbox0 \hbox {$#1$}\relax |
|
327 \advance \hsize by -\wd0\box0} |
|
328 \define@key {mpr}{left}{\setbox0 \hbox {$\TirName {#1}\;$}\relax |
|
329 \advance \hsize by -\wd0\box0} |
|
330 \define@key {mpr}{Left}{\llap{$\TirName {#1}\;$}} |
|
331 \define@key {mpr}{right} |
|
332 {\setbox0 \hbox {$\;\TirName {#1}$}\relax \advance \hsize by -\wd0 |
|
333 \setbox \mpr@right \hbox {\unhbox \mpr@right \unhbox0}} |
|
334 \define@key {mpr}{RIGHT} |
|
335 {\setbox0 \hbox {$#1$}\relax \advance \hsize by -\wd0 |
|
336 \setbox \mpr@right \hbox {\unhbox \mpr@right \unhbox0}} |
|
337 \define@key {mpr}{Right} |
|
338 {\setbox \mpr@right \hbox {\unhbox \mpr@right \rlap {$\;\TirName {#1}$}}} |
|
339 \define@key {mpr}{vdots}{\def \mpr@vdots {\@@atop \mpr@vdotfil{#1}}} |
|
340 \define@key {mpr}{after}{\edef \mpr@after {\mpr@after #1}} |
|
341 |
|
342 \newdimen \rule@dimen |
|
343 \newcommand \mpr@inferstar@ [3][]{\setbox0 |
|
344 \hbox {\let \mpr@rulename \mpr@empty \let \mpr@vdots \relax |
|
345 \setbox \mpr@right \hbox{}% |
|
346 $\setkeys{mpr}{#1}% |
|
347 \ifx \mpr@rulename \mpr@empty \mpr@inferrule {#2}{#3}\else |
|
348 \mpr@inferrule [{\mpr@rulename}]{#2}{#3}\fi |
|
349 \box \mpr@right \mpr@vdots$} |
|
350 \setbox1 \hbox {\strut} |
|
351 \rule@dimen \dp0 \advance \rule@dimen by -\dp1 |
|
352 \raise \rule@dimen \box0} |
|
353 |
|
354 \def \mpr@infer {\@ifnextchar *{\mpr@inferstar}{\mpr@inferrule}} |
|
355 \newcommand \mpr@err@skipargs[3][]{} |
|
356 \def \mpr@inferstar*{\ifmmode |
|
357 \let \@do \mpr@inferstar@ |
|
358 \else |
|
359 \let \@do \mpr@err@skipargs |
|
360 \PackageError {mathpartir} |
|
361 {\string\inferrule* can only be used in math mode}{}% |
|
362 \fi \@do} |
|
363 |
|
364 |
|
365 %%% Exports |
|
366 |
|
367 % Envirnonment mathpar |
|
368 |
|
369 \let \inferrule \mpr@infer |
|
370 |
|
371 % make a short name \infer is not already defined |
|
372 \@ifundefined {infer}{\let \infer \mpr@infer}{} |
|
373 |
|
374 \def \tir@name #1{\hbox {\small \sc #1}} |
|
375 \let \TirName \tir@name |
|
376 \let \RefTirName \tir@name |
|
377 |
|
378 %%% Other Exports |
|
379 |
|
380 % \let \listcons \mpr@cons |
|
381 % \let \listsnoc \mpr@snoc |
|
382 % \let \listhead \mpr@head |
|
383 % \let \listmake \mpr@makelist |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 \endinput |
|