Debian Codegraph @

texmf-dist/tex/lualatex/longmath/longmath.sty

raw
1 %%
2 %% longmath.sty is part of longmath version 1.0.
3 %%
4 %% (c) 2024 Hans-Jürgen Matschull
5 %%
6 %% This work may be distributed and/or modified under the
7 %% conditions of the LaTeX Project Public License, either version 1.3
8 %% of this license or (at your option) any later version.
9 %% The latest version of this license is in
10 %% http://www.latex-project.org/lppl.txt
11 %% and version 1.3 or later is part of all distributions of LaTeX
12 %% version 2005/12/01 or later.
13 %%
14 %% This work has the LPPL maintenance status 'maintained'.
15 %%
16 %% The Current Maintainer of this work is Hans-Jürgen Matschull
17 %%
18 %% see README for a list of files belonging to longmath.
19 %%
20 \NeedsTeXFormat{LaTeX2e}
21 \ProvidesPackage{longmath}[2024/07/04]
22
23 \RequirePackage{luatexbase}
24
25 % generic attribute used to mark special objects and send mathstyle to Lua.
26 \newattribute\longmath@info
27
28 % attribute to tag large operators.
29 \newattribute\longmath@limits
30 \def\ignorelimits{\setattribute{\longmath@limits}{1}}
31 \def\includelimits{\unsetattribute{\longmath@limits}}
32
33 % Save original primitives.
34 \let \longmath@left \left
35 \let \longmath@middle \middle
36 \let \longmath@right \right
37
38 \directlua{ require( 'longmath.lua' ) }
39
40 % Registers.
41 \newcount\delimiterscale \delimiterscale 902
42 \newdimen\longmathlinesep \longmathlinesep 3pt
43
44 \newif\iflongmath@star
45 \newcount\longmath@lines
46 \newdimen\longmath@extra
47 \newdimen\longmath@width
48 \newcount\longmath@xscale
49 \newcount\longmath@yscale
50 \newbox\longmath@box
51
52 % The prefix for tags.
53 \def\longmath@prefix{}
54 \def\delimiterprefix#1{\def\longmath@prefix{#1}}
55
56 % User macros
57 \def\lleft {\longmath@delimA \mathopen {1000}{opn}}
58 \def\rright {\longmath@delimA \mathclose {1000}{cls}}
59 \def\mleft {\longmath@delimA \mathopen {1000}{mid}}
60 \def\mright {\longmath@delimA \mathclose {1000}{mid}}
61 \def\mmord {\longmath@delimA \mathord \delimiterscale {mid}}
62 \def\mmbin {\longmath@delimA \mathbin \delimiterscale {mid}}
63 \def\mmrel {\longmath@delimA \mathrel \delimiterscale {mid}}
64 \def\mmpunct{\longmath@delimA \mathpunct \delimiterscale {mid}}
65 \def\mmiddle{\longmath@delimA \mathinner \delimiterscale {mid}}
66 \def\mmop {\longmath@delimA \mathop \delimiterscale {mid}}
67
68 % This sets the math atom type, the default scale factor, and the command to be send to Lua,
69 % then checks if a tag in {} is following.
70 \def\longmath@delimA #1#2#3{\begingroup
71 \let\longmath@type #1 \longmath@xscale = #2 \relax \edef\longmath@comm{#3}
72 \futurelet \longmath@token \longmath@delimB }
73
74 % If a tag is following, read it. Otherwise, skip this step.
75 \def\longmath@delimB{\ifx\longmath@token\bgroup \expandafter \longmath@delimC
76 \else \expandafter \longmath@delimD \fi }
77
78 % Read the tag, prepend the prefix and append a @, then add it to the command.
79 \def\longmath@delimC #1{\edef\longmath@comm{\longmath@comm:\longmath@prefix#1@} \longmath@delimD }
80
81 % Read the optional scale factor. If none is given, the scale is set to 0.
82 \def\longmath@delimD #1{\afterassignment\longmath@delimE \longmath@yscale = 0#1}
83
84 % This creates a \special whatsit with the command as string argument, followed by
85 % a fake delimiter group with the requested delimiter always being the right one.
86 % For the delimiter nodes, the info attribute is set to the current math style.
87 % For the empty \hbox, it is set to the requested scale factor.
88 \def\longmath@delimE{
89 \setattribute\longmath@info{\mathstyle} \special{\longmath@comm}
90 \longmath@type\bgroup \longmath@left . \aftergroup\egroup \aftergroup\endgroup
91 \begingroup \setattribute\longmath@info{\ifnum \longmath@yscale < 1 \longmath@xscale \else \longmath@yscale \fi}
92 \hbox{} \endgroup \longmath@right }
93
94
95 % magic number to identify the variable delimiter.
96 \delcode`* = "FEFEFE %"
97
98 % scanner to read the delimiter sample and create a nested \left-\right-group.
99 \def\longmath@autoL #1#2\relax{\ifx *#1 \longmath@autoR #2\relax
100 \else \longmath@left #1 \longmath@autoL #2\relax \fi }
101 \def\longmath@autoR #1#2\relax{\ifx *#1
102 \else \longmath@right #1 \longmath@autoR #2\relax \fi}
103
104 % Read the sample provided by the user, insert a set command, and typeset it into a box.
105 \def\autodelimiters #1{\begingroup \longmath@info = 0
106 \setbox\longmath@box\hbox{$ \special{set} \longmath@autoL #1***\relax $}
107 \endgroup\aftergroup\longmath@autoreset}
108
109 % To make the setting local to the current TeX group, send a reset command at the end of the group.
110 \def\longmath@autoreset{\begingroup \longmath@info = 0
111 \setbox\longmath@box\hbox{$ \special{res} {} $}
112 \endgroup}
113
114
115 % Counter for automatic tag generation.
116 \newcount\longmath@tagnum
117
118 % Stack to store pushed tags, separated by spaces.
119 \def\longmath@stack{}
120
121 % Create a unique tag.
122 \def\longmath@unique{
123 \global\advance\longmath@tagnum\@ne
124 \global\edef\longmath@stack{@\the\longmath@tagnum\space\longmath@stack}
125 }
126
127 % Create an invisible closing delimiter.
128 \def\pushdelimiter{\longmath@unique
129 \expandafter\longmath@push\longmath@stack\relax}
130
131 \def\longmath@push #1 #2\relax{\ifmmode\begingroup
132 \def\longmath@comm{mid:#1} \let\longmath@type\mathclose \longmath@yscale=1\relax
133 \longmath@delimE . \fi}
134
135 % Create an invisible opening delimiter.
136 \def\pulldelimiter{\ifx\longmath@stack\empty \longmath@unique \fi
137 \expandafter\longmath@pull\longmath@stack\relax}
138
139 \def\longmath@pull #1 #2\relax{\global\edef\longmath@stack{#2}\ifmmode\begingroup
140 \def\longmath@comm{mid:#1} \let\longmath@type\mathopen \longmath@yscale=1\relax
141 \longmath@delimE . \fi}
142
143
144 % Read width argument for the longmath environment.
145 % Expects to numbers or dimensions terminted by +.
146 % Numbers are multplied by \hsize.
147 \def\longmath@gobble #1\relax{}
148 \def\longmath@setwidth #1+#2+#3\relax{
149 \afterassignment\longmath@gobble \longmath@width = #1 \hsize \relax
150 \afterassignment\longmath@gobble \longmath@extra = #2 \hsize \relax }
151
152 % Alignment for the longmath box.
153 \let\longmath@align@t@\vtop
154 \let\longmath@align@b@\vbox
155 \let\longmath@align@c@\vcenter
156
157 % Save and restore the current math style.
158 \newcount\longmath@style
159 \def\longmath@getstyle{\longmath@style = \mathstyle \relax}
160 \def\longmath@setstyle{\ifcase\longmath@style \relax \displaystyle \or \crampeddisplaystyle \or
161 \textstyle \or \crampedtextstyle \or \scriptstyle \or \crampedscriptstyle \or
162 \scriptscriptstyle \or \crampedscriptscriptstyle \else \textstyle \fi }
163
164 % Begin of longmath environment.
165 % Set alignment and width and save the current math style.
166 % Then start a horizontal box in math mode.
167 \def\longmath@begin #1#2{\begingroup
168 \expandafter\let\expandafter\longmath@align\csname longmath@align@#1@\endcsname
169 \ifx \longmath@align\relax \let\longmath@align\longmath@align@c@ \fi
170 \longmath@getstyle \longmath@setwidth #2+0+\relax
171 %\rlap{\textcolor{green}{\rule{\longmath@width}{0.1pt}}\textcolor{yellow}{\rule{\longmath@extra}{0.1pt}}}
172 \setbox\longmath@box\hbox\bgroup $ \mathpenaltiesmode = 1 \longmath@setstyle
173 }
174
175 % End of longmath environment.
176 % If the width of the box is smaller than the requested width, just use it as is.
177 % Otherwise, modify the requested width if the * version was used.
178 % Then \unhbox the content into a paragraph with the requested width.
179 % \box0 and \box1 are used to adapt the baselineskip to the math style.
180 % \leftskip and \rightskip are set such that lines are centered but not justified.
181 % The glue is removed from the beginning of the first line with \hskip,
182 % and from the end of the last line with \parfilskip.
183 % If \longmath@extra is non-zero, Lua will stack the lines of the paragraph.
184 \def\longmath@end{$\egroup
185 \ifdim \wd\longmath@box < \dimexpr \longmath@width + \longmath@extra \relax
186 \longmath@extra = \z@
187 \box\longmath@box
188 \else
189 \iflongmath@star
190 \longmath@lines = \dimexpr ( 1.1\wd\longmath@box + 0.5\longmath@width ) / \longmath@width \relax
191 \longmath@width = \dimexpr ( 1.1\wd\longmath@box / \longmath@lines )
192 \fi
193 \setbox0\hbox{$1$}
194 \setbox1\hbox{$\longmath@setstyle 1$}
195 \longmath@align{\hsize\longmath@width
196 \baselineskip \dimexpr \baselineskip * \ht1 / \ht0 \relax \lineskip\z@ \lineskiplimit\z@
197 \openup \longmathlinesep
198 \leftskip 0pt plus 0.6\longmath@width
199 \rightskip 0pt plus 0.6\longmath@width
200 \parfillskip-\rightskip \noindent\hskip-\leftskip \unhbox\longmath@box}
201 \longmath@extra = \z@
202 \fi \endgroup
203 }
204
205 \newenvironment{longmath}[2][c]
206 {\longmath@starfalse\longmath@begin{#1}{#2}}{\longmath@end}
207 \newenvironment{longmath*}[2][c]
208 {\longmath@startrue\longmath@begin{#1}{#2}}{\longmath@end}
209
210 % Read the dimensions of tagged groups from the aux file.
211 \def\longmath@group#1#2{\directlua{longmath.read_group({#1},{#2})}}
212 \def\longmath@skip#1#2{}
213
214 % Save the dimensions of the tagged groups at the end of the document.
215 \def\longmath@save{\let\longmath@group\longmath@skip\directlua{longmath.save_groups(\the\@auxout)}}
216 \AtEndDocument{\longmath@save}
217
218
219
220
221
222