Debian Codegraph @

texmf-dist/source/latex/unicode-math/um-code-primes.dtx

raw
1 %%^^A%% um-code-primes.dtx -- part of UNICODE-MATH <wspr.io/unicode-math>
2 %%^^A%% The definitions needed for the input of primes.
3
4 % \section{Primes}
5 %
6 % \begin{macrocode}
7 %<*package>
8 % \end{macrocode}
9 %
10 % We need a new `prime' algorithm. Unicode math has four pre-drawn prime glyphs.
11 % \begin{quote}\obeylines
12 % \unichar{2032} {prime} (\cs{prime}): $x\prime$
13 % \unichar{2033} {double prime} (\cs{dprime}): $x\dprime$
14 % \unichar{2034} {triple prime} (\cs{trprime}): $x\trprime$
15 % \unichar{2057} {quadruple prime} (\cs{qprime}): $x\qprime$
16 % \end{quote}
17 % As you can see, they're all drawn at the correct height without being superscripted.
18 % However, in a correctly behaving OpenType font,
19 % we also see different behaviour after the \texttt{ssty} feature is applied:
20 % \begin{quote}
21 % \font\1="[XITSMath-Regular.otf]:script=math,+ssty=0"\1
22 % \char"1D465\char"2032\quad
23 % \char"1D465\char"2033\quad
24 % \char"1D465\char"2034\quad
25 % \char"1D465\char"2057
26 % \end{quote}
27 % The glyphs are now `full size' so that when placed inside a superscript,
28 % their shape will match the originally sized ones. Many thanks to Ross Mills
29 % of Tiro Typeworks for originally pointing out this behaviour.
30 %
31 % In regular \LaTeX, primes can be entered with the straight quote character
32 % |'|, and multiple straight quotes chain together to produce multiple
33 % primes. Better results can be achieved in \pkg{unicode-math} by chaining
34 % multiple single primes into a pre-drawn multi-prime glyph; consider
35 % $x\prime{}\prime{}\prime$ vs.\ $x\trprime$.
36 %
37 % For Unicode maths, we wish to conserve this behaviour and augment it with
38 % the possibility of adding any combination of Unicode prime or any of the
39 % $n$-prime characters. E.g., the user might copy-paste a double prime from
40 % another source and then later type another single prime after it; the output
41 % should be the triple prime.
42 %
43 % Our algorithm is:
44 % \begin{itemize}[nolistsep]
45 % \item Prime encountered; pcount=1.
46 % \item Scan ahead; if prime: pcount:=pcount+1; repeat.
47 % \item If not prime, stop scanning.
48 % \item If pcount=1, \cs{prime}, end.
49 % \item If pcount=2, check \cs{dprime}; if it exists, use it, end; if not, goto last step.
50 % \item Ditto pcount=3 \& \cs{trprime}.
51 % \item Ditto pcount=4 \& \cs{qprime}.
52 % \item If pcount>4 or the glyph doesn't exist, insert pcount \cs{prime}s with \cs{primekern} between each.
53 % \end{itemize}
54 %
55 % This is a wrapper to insert a superscript; if there is a subsequent
56 % trailing superscript, then it is included within the insertion.
57 % \begin{macrocode}
58 \cs_new:Nn \@@_arg_i_before_egroup:n {#1\egroup}
59 \cs_new:Nn \@@_superscript:n
60 {
61 ^\bgroup #1
62 \peek_meaning_remove:NTF ^ \@@_arg_i_before_egroup:n \egroup
63 }
64 % \end{macrocode}
65 %
66 % \begin{macrocode}
67 \cs_new:Nn \@@_nprimes:Nn
68 {
69 \@@_superscript:n
70 {
71 #1
72 \prg_replicate:nn {#2-1} { \mskip \g_@@_primekern_muskip #1 }
73 }
74 }
75 % \end{macrocode}
76 %
77 % \begin{macrocode}
78 \cs_new:Nn \@@_nprimes_select:nn
79 {
80 \int_case:nnF {#2}
81 {
82 {1} { \@@_superscript:n {#1} }
83 {2} {
84 \@@_glyph_if_exist:NnTF \g_@@_prime_font_cmd_tl {"2033}
85 { \@@_superscript:n {\@@_prime_double_mchar} }
86 { \@@_nprimes:Nn #1 {#2} }
87 }
88 {3} {
89 \@@_glyph_if_exist:NnTF \g_@@_prime_font_cmd_tl {"2034}
90 { \@@_superscript:n {\@@_prime_triple_mchar} }
91 { \@@_nprimes:Nn #1 {#2} }
92 }
93 {4} {
94 \@@_glyph_if_exist:NnTF \g_@@_prime_font_cmd_tl {"2057}
95 { \@@_superscript:n {\@@_prime_quad_mchar} }
96 { \@@_nprimes:Nn #1 {#2} }
97 }
98 }
99 {
100 \@@_nprimes:Nn #1 {#2}
101 }
102 }
103 % \end{macrocode}
104 %
105 % \begin{macrocode}
106 \cs_new:Nn \@@_nbackprimes_select:nn
107 {
108 \int_case:nnF {#2}
109 {
110 {1} { \@@_superscript:n {#1} }
111 {2} {
112 \@@_glyph_if_exist:NnTF \g_@@_prime_font_cmd_tl {"2036}
113 { \@@_superscript:n {\@@_backprime_double_mchar} }
114 { \@@_nprimes:Nn #1 {#2} }
115 }
116 {3} {
117 \@@_glyph_if_exist:NnTF \g_@@_prime_font_cmd_tl {"2037}
118 { \@@_superscript:n {\@@_backprime_triple_mchar} }
119 { \@@_nprimes:Nn #1 {#2} }
120 }
121 }
122 {
123 \@@_nprimes:Nn #1 {#2}
124 }
125 }
126 % \end{macrocode}
127 %
128 % Scanning is annoying because I'm too lazy to do it for the general case.
129 %
130 % \begin{macrocode}
131 \cs_new:Npn \@@_scan_prime:
132 {
133 \cs_set_eq:NN \@@_superscript:n \use:n
134 \int_zero:N \l_@@_primecount_int
135 \@@_scanprime_collect:N \@@_prime_single_mchar
136 }
137 \cs_new:Npn \@@_scan_dprime:
138 {
139 \cs_set_eq:NN \@@_superscript:n \use:n
140 \int_set:Nn \l_@@_primecount_int {1}
141 \@@_scanprime_collect:N \@@_prime_single_mchar
142 }
143 \cs_new:Npn \@@_scan_trprime:
144 {
145 \cs_set_eq:NN \@@_superscript:n \use:n
146 \int_set:Nn \l_@@_primecount_int {2}
147 \@@_scanprime_collect:N \@@_prime_single_mchar
148 }
149 \cs_new:Npn \@@_scan_qprime:
150 {
151 \cs_set_eq:NN \@@_superscript:n \use:n
152 \int_set:Nn \l_@@_primecount_int {3}
153 \@@_scanprime_collect:N \@@_prime_single_mchar
154 }
155 \cs_new:Npn \@@_scan_sup_prime:
156 {
157 \int_zero:N \l_@@_primecount_int
158 \@@_scanprime_collect:N \@@_prime_single_mchar
159 }
160 \cs_new:Npn \@@_scan_sup_dprime:
161 {
162 \int_set:Nn \l_@@_primecount_int {1}
163 \@@_scanprime_collect:N \@@_prime_single_mchar
164 }
165 \cs_new:Npn \@@_scan_sup_trprime:
166 {
167 \int_set:Nn \l_@@_primecount_int {2}
168 \@@_scanprime_collect:N \@@_prime_single_mchar
169 }
170 \cs_new:Npn \@@_scan_sup_qprime:
171 {
172 \int_set:Nn \l_@@_primecount_int {3}
173 \@@_scanprime_collect:N \@@_prime_single_mchar
174 }
175 \cs_new:Nn \@@_scanprime_collect:N
176 {
177 \int_incr:N \l_@@_primecount_int
178 \peek_meaning_remove:NTF '
179 { \@@_scanprime_collect:N #1 }
180 {
181 \peek_meaning_remove:NTF \@@_scan_prime:
182 { \@@_scanprime_collect:N #1 }
183 {
184 \peek_meaning_remove:NTF ^^^^2032
185 { \@@_scanprime_collect:N #1 }
186 {
187 \peek_meaning_remove:NTF \@@_scan_dprime:
188 {
189 \int_incr:N \l_@@_primecount_int
190 \@@_scanprime_collect:N #1
191 }
192 {
193 \peek_meaning_remove:NTF ^^^^2033
194 {
195 \int_incr:N \l_@@_primecount_int
196 \@@_scanprime_collect:N #1
197 }
198 {
199 \peek_meaning_remove:NTF \@@_scan_trprime:
200 {
201 \int_add:Nn \l_@@_primecount_int {2}
202 \@@_scanprime_collect:N #1
203 }
204 {
205 \peek_meaning_remove:NTF ^^^^2034
206 {
207 \int_add:Nn \l_@@_primecount_int {2}
208 \@@_scanprime_collect:N #1
209 }
210 {
211 \peek_meaning_remove:NTF \@@_scan_qprime:
212 {
213 \int_add:Nn \l_@@_primecount_int {3}
214 \@@_scanprime_collect:N #1
215 }
216 {
217 \peek_meaning_remove:NTF ^^^^2057
218 {
219 \int_add:Nn \l_@@_primecount_int {3}
220 \@@_scanprime_collect:N #1
221 }
222 {
223 \@@_nprimes_select:nn {#1} {\l_@@_primecount_int}
224 }
225 }
226 }
227 }
228 }
229 }
230 }
231 }
232 }
233 }
234 % \end{macrocode}
235 %
236 % \begin{macrocode}
237 \cs_new:Npn \@@_scan_backprime:
238 {
239 \cs_set_eq:NN \@@_superscript:n \use:n
240 \int_zero:N \l_@@_primecount_int
241 \@@_scanbackprime_collect:N \@@_backprime_single_mchar
242 }
243 \cs_new:Npn \@@_scan_backdprime:
244 {
245 \cs_set_eq:NN \@@_superscript:n \use:n
246 \int_set:Nn \l_@@_primecount_int {1}
247 \@@_scanbackprime_collect:N \@@_backprime_single_mchar
248 }
249 \cs_new:Npn \@@_scan_backtrprime:
250 {
251 \cs_set_eq:NN \@@_superscript:n \use:n
252 \int_set:Nn \l_@@_primecount_int {2}
253 \@@_scanbackprime_collect:N \@@_backprime_single_mchar
254 }
255 \cs_new:Npn \@@_scan_sup_backprime:
256 {
257 \int_zero:N \l_@@_primecount_int
258 \@@_scanbackprime_collect:N \@@_backprime_single_mchar
259 }
260 \cs_new:Npn \@@_scan_sup_backdprime:
261 {
262 \int_set:Nn \l_@@_primecount_int {1}
263 \@@_scanbackprime_collect:N \@@_backprime_single_mchar
264 }
265 \cs_new:Npn \@@_scan_sup_backtrprime:
266 {
267 \int_set:Nn \l_@@_primecount_int {2}
268 \@@_scanbackprime_collect:N \@@_backprime_single_mchar
269 }
270 \cs_new:Nn \@@_scanbackprime_collect:N
271 {
272 \int_incr:N \l_@@_primecount_int
273 \peek_meaning_remove:NTF `
274 {
275 \@@_scanbackprime_collect:N #1
276 }
277 {
278 \peek_meaning_remove:NTF \@@_scan_backprime:
279 {
280 \@@_scanbackprime_collect:N #1
281 }
282 {
283 \peek_meaning_remove:NTF ^^^^2035
284 {
285 \@@_scanbackprime_collect:N #1
286 }
287 {
288 \peek_meaning_remove:NTF \@@_scan_backdprime:
289 {
290 \int_incr:N \l_@@_primecount_int
291 \@@_scanbackprime_collect:N #1
292 }
293 {
294 \peek_meaning_remove:NTF ^^^^2036
295 {
296 \int_incr:N \l_@@_primecount_int
297 \@@_scanbackprime_collect:N #1
298 }
299 {
300 \peek_meaning_remove:NTF \@@_scan_backtrprime:
301 {
302 \int_add:Nn \l_@@_primecount_int {2}
303 \@@_scanbackprime_collect:N #1
304 }
305 {
306 \peek_meaning_remove:NTF ^^^^2037
307 {
308 \int_add:Nn \l_@@_primecount_int {2}
309 \@@_scanbackprime_collect:N #1
310 }
311 {
312 \@@_nbackprimes_select:nn {#1} {\l_@@_primecount_int}
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320 }
321 % \end{macrocode}
322 %
323 % \begin{macrocode}
324 \AtBeginDocument { \@@_define_prime_commands: \@@_define_prime_chars: }
325 \cs_new:Nn \@@_define_prime_commands:
326 {
327 \cs_set_eq:NN \prime \@@_prime_single_mchar
328 \cs_set_eq:NN \dprime \@@_prime_double_mchar
329 \cs_set_eq:NN \trprime \@@_prime_triple_mchar
330 \cs_set_eq:NN \qprime \@@_prime_quad_mchar
331 \cs_set_eq:NN \backprime \@@_backprime_single_mchar
332 \cs_set_eq:NN \backdprime \@@_backprime_double_mchar
333 \cs_set_eq:NN \backtrprime \@@_backprime_triple_mchar
334 }
335 % \end{macrocode}
336 %
337 % \begin{macrocode}
338 \group_begin:
339 \char_set_catcode_active:N \'
340 \char_set_catcode_active:N \`
341 \char_set_catcode_active:n {"2032}
342 \char_set_catcode_active:n {"2033}
343 \char_set_catcode_active:n {"2034}
344 \char_set_catcode_active:n {"2057}
345 \char_set_catcode_active:n {"2035}
346 \char_set_catcode_active:n {"2036}
347 \char_set_catcode_active:n {"2037}
348 \cs_gset:Nn \@@_define_prime_chars:
349 {
350 \cs_set_eq:NN ' \@@_scan_sup_prime:
351 \cs_set_eq:NN ^^^^2032 \@@_scan_sup_prime:
352 \cs_set_eq:NN ^^^^2033 \@@_scan_sup_dprime:
353 \cs_set_eq:NN ^^^^2034 \@@_scan_sup_trprime:
354 \cs_set_eq:NN ^^^^2057 \@@_scan_sup_qprime:
355 \cs_set_eq:NN ` \@@_scan_sup_backprime:
356 \cs_set_eq:NN ^^^^2035 \@@_scan_sup_backprime:
357 \cs_set_eq:NN ^^^^2036 \@@_scan_sup_backdprime:
358 \cs_set_eq:NN ^^^^2037 \@@_scan_sup_backtrprime:
359 }
360 \group_end:
361
362 \cs_set_eq:NN \active@math@prime \@@_scan_sup_prime:
363 % \end{macrocode}
364 %
365 %
366 % \begin{macrocode}
367 %</package>
368 % \end{macrocode}
369
370 \endinput
371
372 % /©
373 %
374 % ------------------------------------------------
375 % The UNICODE-MATH package <wspr.io/unicode-math>
376 % ------------------------------------------------
377 % This package is free software and may be redistributed and/or modified under
378 % the conditions of the LaTeX Project Public License, version 1.3c or higher
379 % (your choice): <http://www.latex-project.org/lppl/>.
380 % ------------------------------------------------
381 % Copyright 2006-2019 Will Robertson, LPPL "maintainer"
382 % Copyright 2010-2017 Philipp Stephani
383 % Copyright 2011-2017 Joseph Wright
384 % Copyright 2012-2015 Khaled Hosny
385 % ------------------------------------------------
386 %
387 % ©/