Debian Codegraph @

texmf-dist/tex/optex/base/more-macros.opm

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \eoldef {OpTeX useful macros <2026-03-23>} % preloaded in format
4
5 \_doc -----------------------------
6 We define \`\opinput` `{<file name>}` macro which
7 does `\input {<file name>}` but the catcodes are set to normal catcodes
8 (like \OpTeX/ initializes them) and the catcodes setting is returned back to
9 the current values when the file is read. You can use `\opinput`
10 in any situation inside the document and you will be sure that the file
11 is read correctly with correct catcode settings.
12
13 To achieve this, we declare \`\optexcatcodes` catcode table
14 and \`\plaintexcatcodes`. They save the commonly used catcode tables.
15 Note that `\catcodetable` is a part of \LuaTeX/ extension.
16 The catcodetable stack is implemented by \OpTeX/ macros.
17 The \`\setctable` `<catcode table>` pushes
18 current catcode table to the stack and activates catcodes from
19 the `<catcode table>`. The \`\restorectable` returns to the saved catcodes
20 from the catcode table stack.
21
22 The `\opinput` works inside the catcode table stack. It reads `\optexcatcodes`
23 table and stores it to \`\_tmpcatcodes` table.
24 This table is actually used during `\input` (maybe catcodes are
25 changed here). Finally, `\_restoretable` pops the stacks and returns
26 to the catcodes used before `\opinput` is run.
27 \_cod -----------------------------
28
29 \_def\_opinput #1{\_setctable\_optexcatcodes
30 \_savecatcodetable\_tmpcatcodes \_catcodetable\_tmpcatcodes
31 \_input {#1}\_relax\_restorectable}
32
33 \_newcatcodetable \_optexcatcodes
34 \_newcatcodetable \_plaintexcatcodes
35 \_newcatcodetable \_tmpcatcodes
36
37 \_public \optexcatcodes \plaintexcatcodes \opinput ;
38
39 \_savecatcodetable\_optexcatcodes
40 {\_catcode`_=8 \savecatcodetable\plaintexcatcodes}
41
42 \_doc -----------------------------
43 The implementation of the catcodetable stack follows.
44
45 The current catcodes are
46 managed in the `\catcodetable0`. If the `\setctable` is used first (or at
47 the outer level of the stack), then the `\catcodetable0` is pushed to the
48 stack and the current table is re-set to the given `<catcode table>`.
49 The numbers of these tables are stacked to the \`\_ctablelist` macro.
50 The `\restorectable` reads the last saved catcode table number from the
51 `\_ctablelist` and uses it.
52 \_cod -----------------------------
53
54 \_catcodetable0
55
56 \_def\_setctable#1{\_edef\_ctablelist{{\_the\_catcodetable}\_ctablelist}%
57 \_catcodetable#1\_relax
58 }
59 \_def\_restorectable{\_ea\_restorectableA\_ctablelist\_relax}
60 \_def\_restorectableA#1#2\_relax{%
61 \_ifx^#2^\_opwarning
62 {You can't use \_noindent\restorectable without previous \_string\setctable}%
63 \_else \_def\_ctablelist{#2}\_catcodetable#1\_relax \_fi
64 }
65 \_def\_ctablelist{.}
66
67 \_public \setctable \restorectable ;
68
69 \_doc -----------------------------
70 When a special macro is defined with different catcodes then
71 \`\normalcatcodes` can be used at the end of such definition.
72 The normal catcodes are restored.
73 The macro reads
74 catcodes from `\optecatodes` table and sets it to the main catcode table 0.
75 \_cod -----------------------------
76
77 \_def\_normalcatcodes {\_catcodetable\_optexcatcodes \_savecatcodetable0 \_catcodetable0 }
78 \_public \normalcatcodes ;
79
80 \_doc -----------------------------
81 The \`\load` `[<filename-list>]` loads files specified in
82 comma separated `<filename-list>`. The first space (after comma)
83 is ignored using the trick `#1#2,`: first parameter is unseparated.
84 The `\load` macro saves information about loaded files by setting
85 `\_load:<filename>` as a defined macro.
86
87 If the \`\_afterload` macro is defined then it is run after `\_opinput`.
88 The catcode setting should be here. Note that catcode setting done in the
89 loaded file is forgotten after the `\opinput`.
90 \_cod -----------------------------
91
92 \_def \_load [#1]{\_savemathsb \_loadA #1,,,\_end \_restoremathsb}
93 \_def \_loadA #1#2,{\_ifx,#1\_ea \_loadE \_else \_loadB{#1#2}\_ea\_loadA\_fi}
94 \_def \_loadB #1{%
95 \_ifcsname _load:#1\_endcsname \_else
96 \_isfile {#1.opm}\_iftrue \_opinput {#1.opm}\_else \_opinput {#1}\_fi
97 \_sxdef{_load:#1}{}%
98 \_trycs{_afterload}{}\_let\_afterload=\_undefined
99 \_fi
100 }
101 \_def \_loadE #1\_end{}
102 \_public \load ;
103
104 \_doc -----------------------------
105 The declarator \`\optdef``\macro [<opt default>] <params>{<replacement text>}`
106 defines the `\macro` with the optional parameter followed by normal parameters
107 declared in `<params>`. The optional parameter must be used as the first
108 parameter in brackets `[...]`. If it isn't used then <opt default>
109 is taken into account. The `<replacement text>` can use `\the\opt`
110 because optional parameter is saved to the \`\opt` tokens register.
111 Note the difference from \LaTeX/ concept where the optional parameter is
112 in `#1`. \OpTeX/ uses `#1` as the first normal parameter (if declared).
113
114 The \`\nospaceafter` <token> or `\nospaceafter{<text>}`
115 ignores the following optional space at expand
116 processor level using the negative `\romannumeral` trick.
117 Then returns to process <token> or <text>.
118
119 The \`\nospacefuturelet` bahaves like `\futurelet` primitive, but it
120 ignores the following optional space and works at expand processor level.
121 \_cod -----------------------------
122
123 \_newtoks\_opt
124 \_def\_optdef#1[#2]{%
125 \_def#1{\_isnextchar[{\_cs{_oA:\_csstring#1}}{\_cs{_oA:\_csstring#1}[#2]}}%
126 \_sdef{_oA:\_csstring#1}[##1]{%
127 \_immediateassignment\_opt={##1}\_cs{_oB:\_csstring#1\_nospaceafter}}%
128 \_sdef{_oB:\_csstring#1\_nospaceafter}%
129 }
130 \_def\_nospaceafter#1{\_expanded{\_unexpanded{#1}\_ea}\_romannumeral-`\.\_noexpand}
131 \_def\_nospacefuturelet#1#2{\_ea\_immediateassignment
132 \_ea\_futurelet\_ea#1\_ea#2\_romannumeral-`\.\_noexpand}
133
134 \_public \opt \optdef \nospaceafter \nospacefuturelet ;
135
136 \_doc -----------------------------
137 \`\_noprefix` `<cs>` works like `\csstring` `<cs>`, but ignores not only
138 the first backlash but the second~\"`_`" ignores too (if it follows the
139 backslash).
140 \_cod -----------------------------
141
142 \_def\_noprefix#1{\_ea\_noprefixA \_csstring#1\_empty\_fin}
143 \_def\_noprefixA #1#2\_fin{\_if _#1\_else #1\_fi #2}
144
145 \_doc -----------------------------
146 The declarator \`\eoldef``\macro #1{<replacement text>}` defines a `\macro`
147 which scans its parameter to the end of the current line.
148 This is the parameter `#1` which can be used in the `<replacement text>`.
149 The catcode of the `\endlinechar` is reset temporarily when the parameter is scanned.
150
151 The macro defined by \^`\eoldef` cannot be used with its parameter inside
152 other macros because the catcode dancing is not possible here. But the
153 \`\bracedparam``\macro{<parameter>}` can be used here. The `\bracedparam`
154 is a prefix that re-sets temporarily the `\macro` to a `\macro` with
155 normal one parameter.
156
157 The \`\skiptoeol` macro reads the text to the end of the current line
158 and ignores it.
159 \_cod -----------------------------
160
161 \_def\_eoldef #1{\_def #1{\_begingroup \_catcode`\^^M=12 \_eoldefA #1}%
162 \_ea\_def\_csname _eol:\_noprefix #1\_endcsname}
163 \_catcode`\^^M=12 %
164 \_def\_eoldefA #1#2^^M{\_endgroup\_csname _eol:\_noprefix #1\_endcsname{#2}}%
165 \_normalcatcodes %
166
167 \_eoldef\_skiptoeol#1{}
168
169 \_def\_bracedparam#1{%
170 \_trycs{_eol:\_noprefix#1}%
171 {\_errmessage{\_string\bracedparam: \_string#1 isn't defined by \_string\eoldef}}%
172 }
173 \_public \eoldef \skiptoeol \bracedparam ;
174
175 \_doc -----------------------------
176 \`\scantoeol``\macro <text to end of line>` scans the
177 `<text to end of line>` in verbatim mode and runs the
178 `\macro{<text to end of line>}`. The `\macro`
179 can be defined `\def\macro#1{...\scantextokens{#1}...}`.
180 The new tokenization of the parameter is processed when the parameter is used,
181 no when the parameter is scanned. This principle is used in definition
182 of \^`\chap`, \^`\sec`, \^`\secc` and \^`\_Xtoc` macros.
183 It means that user can write \code{\\sec text `&` text} for example.
184 Inline verbatim works in title sections.
185
186 The verbatim scanner of `\scantoeol` keeps category 7 for `^` in
187 order to be able to use `^^J` as comment character which means that
188 the next line continues.
189 \_cod -----------------------------
190
191 \_def\_scantoeol#1{\_begingroup \_setscancatcodes \_scantoeolA#1}
192 \_def\_setscancatcodes{\_setverb \_catcode`\^^M=12\_catcode`\^=7\_catcode`\ =10\_catcode`\^^J=14 }
193 \_catcode`\^^M=12 %
194 \_def\_scantoeolA#1#2^^M{\_endgroup #1{#2}}%
195 \_normalcatcodes %
196
197 \_public \scantoeol ;
198
199 \_doc -----------------------------
200 The \`\replstring``\macro{<textA>}{<textB>}`
201 replaces all occurrences of `<textA>` by `<textB>` in the `\macro` body.
202 The `\macro` must be defined without parameters. The occurrences of
203 `<textA>` are not replaced if they are \"hidden" in braces, for example
204 `...{...<textA>...}...`. The category codes in the `<textA>` must exactly
205 match.
206
207 How it works: `\replstring\foo{<textA>}{<textB>}` prepares
208 `\_replacestringsA#1<textA>{...}` and runs
209 `\_replacestringsA<foo-body>?<textA>!<textA>`.
210 So, `#1` includes the first part of <foo-body> before first <textA>.
211 It is saved to \`\_repltoks` and `\_replacestringsB` is run with `\_replacestingsC` in a loop.
212 It finishes processing (if final `!` is scanned) or appends the next part to `\_repltoks` separated by
213 <textB> and continues loop. The final part of the \^`\replstring` macro removes the last `?`
214 from resulting `\_repltoks` and defines a new version of the `\foo`.
215 \_cod -----------------------------
216
217 \_newtoks\_repltoks
218 \_catcode`!=3 \_catcode`?=3
219 \_long\_def\_replstring #1#2#3{% \replstring #1{stringA}{stringB}
220 \_long\_def\_replacestringsA##1#2{\_repltoks\_ea{##1}\_replacestringsB\_empty}%
221 \_long\_def\_replacestringsB##1#2{\_ea\_replacestringsC\_ea{##1}}%
222 \_long\_def\_replacestringsC##1{\_ifx!##1\_empty\_else
223 \_toksapp\_repltoks{#3##1}\_ea\_replacestringsB\_ea\_empty\_fi}%
224 \_ea\_replacestringsA \_ea\_empty#1?#2!#2%
225 \_ea\_replacestringsD \_ea\_empty\_the\_repltoks#1}
226 \_long\_def\_replacestringsD #1?#2{\_repltoks\_ea{#1}\_edef#2{\_the\_repltoks}}%
227 \_normalcatcodes
228
229 \_public \replstring ;
230
231 \_doc -----------------------------
232 You can find three special tricks in the \^`\replstring` code:
233 \begitems
234 * Adding and removing `?` character: without this the
235 `\def\b{xxxA}\replstring\b{AA}{BB}` will cause an error.
236 * Using `\_toksapp` instead `\addto`: without this you cannot have $`#`_6$
237 in the string. Moreover, it significantly
238 reduces the calculation time. Unfortunately, it does \^`\replstring` unexpandable.
239 * Using `\_empty` before scanning separated parameters and removing it
240 after scanning by \^`\ea`: without this \TeX/
241 removes braces from `<textA>{xxx}<textA>`.
242 \enditems
243 The \^`\replstring` macro isn't expandable. But you can see
244 \ulink[http://petr.olsak.net/optex/optex-tricks.html\#xreplstring]{\OpTeX/ trick 0136}
245 and \ulink[http://petr.olsak.net/optex/optex-tricks.html\#replmacro]{\OpTeX/ trick 0137}.
246 These triks implement expandable alternatives and enable more general modifications
247 of macros by regular expressions.
248 \_cod -----------------------------
249
250 \_doc -----------------------------
251 The \`\catcode` primitive is redefined here. Why?
252 There is very common cases like \code{\\catcode`}`<something>`
253 or `\catcode"<number>` but these characters
254 \code{\`} or \code{"} can be set as active (typically by `\verbchar` macro).
255 Nothing problematic happens if re-defined `\catcode` is used in this case.
256
257 If you really need primitive `\catcode` then you can use `\_catcode`.
258 \_cod -----------------------------
259
260 \_def\catcode#1{\_catcode \_if`\_noexpand#1\_ea`\_else\_if"\_noexpand#1"\_else
261 \_if'\_noexpand#1'\_else \_ea\_ea\_ea\_ea\_ea\_ea\_ea#1\_fi\_fi\_fi}
262
263 \_doc -----------------------------
264 The \`\removespaces` `<text with spaces >{}` expands to <textwithoutspaces>.
265 \nl
266 The `\_ea`\`\ignorept``\the<dimen>` expands to a decimal
267 number `\the<dimen>` but without `pt` unit.
268 \_cod -----------------------------
269
270 \_def\_removespaces #1 {\_isempty{#1}\_iffalse #1\_ea\_removespaces\_fi}
271 \_ea\_def \_ea\_ignorept \_ea#\_ea1\_detokenize{pt}{#1}
272
273 \_public \removespaces \ignorept ;
274
275 \_doc -----------------------------
276 If you do `\let\foo=a` then it is not simple to return from `\foo` to the original
277 character code of `a`. You can write \code{`a} but you cannot write \code{`\\foo}.
278 The macro \`\cstochar``<sequence>` solves this problem. If the sequence
279 is equal to a character then it expands to this character (always with
280 catcode 12). If it isn't equal to a character then it expands to nothing.
281 You can say \code{\\expanded{`\\cstochar\\foo}} if you want to extract
282 the character code.
283 \_cod -----------------------------
284
285 \_def\_cstochar#1{\_ea\_cstocharA\_meaning#1 {} {} \_fin}
286 \_def\_cstocharA#1 #2 #3 #4\_fin{\_isinlist{#1#2}-\_iffalse #3\_fi}
287
288 \_public \cstochar ;
289
290 \_doc -----------------------------
291 You can use expandable \`\bp``{<dimen>}` converter from
292 \TeX/ `<dimen>` (or from an expression accepted by
293 `\dimexpr` primitive) to a decimal value in big points
294 (used as natural unit in the PDF format). So, you can write, for example:
295 \begtt
296 \pdfliteral{q \_bp{.3\hsize-2mm} \_bp{2mm} m 0 \_bp{-4mm} l S Q}
297 \endtt
298 You can use expandable \`\expr``{<expression>}` for analogical purposes.
299 It expands to the value of the `<expression>` at expand processor level.
300 The `<expression>` can include `+-*/^()` and decimal numbers in common syntax.
301 Moreover, `a//b` means integer division and `a\%b` is remainder.
302 The math functions (and pi constant) have to be prefixed by `math.`,
303 because it is processed by Lua interpreter. For
304 example `\expr{math.pi*math.sqrt(2)}`.
305 The list of available functions is in
306 \ulink[https://www.lua.org/manual/5.3/manual.html\#6.7]{Lua manual}.
307
308 You can set the number of decimal digits after decimal point of the
309 results of `\bp` and `\expr`
310 by optional syntax `\bp[<digits>]{<dimen>}` and
311 `\expr[<digits>]{<expression>}`. Default is \`\_decdigits`.
312
313 The usage of prefixed versions `\_expr` or `\_bp` is more recommended
314 for macro programmers because a user can re-define the control sequences
315 `\expr` or `\bp`.
316 \_cod -----------------------------
317
318 \_def\_decdigits{3} % digits after decimal point in \_bp and \_expr outputs.
319 \_def\_pttopb#1{%
320 \_directlua{tex.print(string.format('\_pcent.#1f',
321 token.scan_dimen()/65781.76))}% pt to bp conversion
322 }
323 \_def\_bp{\_isnextchar[{\_bpA}{\_bpA[\_decdigits]}}
324 \_def\_bpA[#1]#2{\_pttopb{#1}\_dimexpr#2\_relax}
325 \_def\_expr{\_isnextchar[{\_exprA}{\_exprA[\_decdigits]}}
326 \_def\_exprA[#1]#2{\_directlua{tex.print(string.format('\_pcent.#1f',#2))}}
327
328 \_public \expr \bp ;
329
330 \_doc -----------------------------
331 The \^`\expr` and \^`\bp` macros return their results with given number of decimal
332 digits even if there are trailing zeros. There is the \^`\nnum` macro to
333 \"normalize" such decimal numbers. \`\nnum``{<number>}` expands its parameter
334 and removes trailing zeros after decimal point and removes the decimal point
335 if nothing follows. For example, use `\nnum{\expr[10]{<expression>}}`.
336 The `\nnum` macro is fully expandable.
337 \_cod -----------------------------
338
339 \_def\_nnum #1{\_ea\_nnumA\_expanded{#1}.\_fin}
340 \_def\_nnumA #1.#2\_fin{#1\_ifx~#2~\_else \_nnumB #20.\_fin \_fi}
341 \_def\_nnumB #10.#2\_fin{\_ifx~#2~\_nnumC#1\_else \_nnumB #1.0.\_fin \_fi}
342 \_def\_nnumC #1.{\_ifx~#1~\_else .#1\_fi}
343 \_public \nnum ;
344
345 \_doc ------------------
346 You can write \^`\setpos``[<label>]` somewhere and the position of such
347 \^`\setpos``[<label>]` can be referenced by \^`\posx``[<label>]`,
348 \^`\posy``[<label>]` and \^`\pospg``[<label>]`. The first two macros
349 expand to $x$ and $y$ position measured from left-bottom corner of the page
350 (dimen values) and \^`\pospg[<label>]` expands to the <gpageno>, i.e.\ to
351 the page number counted from one at beginning of the document.
352 These values are available in the second (and more) \TeX/ run, because the
353 information is saved to `.ref` file and restored from it at the beginning of
354 the \TeX/ job. If these values are not known then mentioned macros expand to
355 0sp, 0sp and 0. The following example implements `\linefrom[<label>]` and
356 `\lineto[<label>]` macros. The line connecting these two points is drawn
357 (after second \TeX/ run):
358 \begtt
359 \def\linefrom[#1]{\setpos[#1:f]\drawlinefromto[#1]}
360 \def\lineto [#1]{\setpos[#1:t]}
361 \def\drawlinefromto[#1]{\ifnum\pospg[#1:f]>0 \ifnum\pospg[#1:f]=\pospg[#1:t]
362 \pdfliteral{q 0 0 m 1 0 0 RG % << red color
363 \expr{\bp{\posx[#1:t]}-\bp{\posx[#1:f]}}
364 \expr{\bp{\posy[#1:t]}-\bp{\posy[#1:f]}} l S Q}\fi\fi
365 }
366 This is a text.\linefrom[A]\par
367 This is second paragraph with a text.\lineto[A]
368 Try to reverse from-to and watch the changes.
369 \endtt
370 The coordinates are saved to the `.ref` file in the format
371 \`\_Xpos``{<label>}{<x-pos>}{<y-pos>}`. The \^`\_Xpos` macro defines
372 `\_pos:<label>` as `{<x-pos>}{<y-pos>}{<total-pg>}{<rel-pg>}`.
373 We need to read only given parameter by `\_posi`, `\_posii` or `\_posiii`
374 auxiliary macros. The implementation of
375 \`\setpos`, \`\posx` and \`\posy` macros are based on `\padsavepos`
376 `\pdflastxpos` and `\pdflastypos` pdf\TeX/ primitives. The \`\pospg`
377 simply reads the data from the \~`\_currpage` macro.
378 \_cod ------------------
379
380 \_def\_Xpos#1#2#3{\_sxdef{_pos:#1}{{#2}{#3}\_currpage}}
381 \_def\_setpos[#1]{\_openref\_pdfsavepos
382 \_ewref\_Xpos{{#1}\_unexpanded{{\_the\_pdflastxpos}{\_the\_pdflastypos}}}}
383
384 \_def\_posx [#1]{\_ea \_posi \_expanded {\_trycs{_pos:#1}{{0}{}{}{}}sp}}
385 \_def\_posy [#1]{\_ea \_posii \_expanded {\_trycs{_pos:#1}{{}{0}{}{}}sp}}
386 \_def\_pospg[#1]{\_ea \_posiii \_expanded {\_trycs{_pos:#1}{{}{}{0}{}}}}
387
388 \_def\_posi #1#2#3#4{#1} \_def\_posii #1#2#3#4{#2} \_def\_posiii #1#2#3#4{#3}
389
390 \_public \setpos \posx \posy \pospg ;
391
392 \_doc ------------------
393 The pair {\`\_doc` ... \`\_cod`} is used for documenting macros and to
394 printing the technical documentation of the \OpTeX/. The syntax is:
395 {\begtt \catcode`\<=13
396 \_doc <ignored text>
397 <documentation>
398 \_cod <ignored text>
399 \endtt
400 }
401 The `<documentation>` (and `<ignored text>` too) must be `<balanced text>`.
402 It means that you cannot document only the `{` but you must document the `}` too.
403 \_cod ------------------
404
405 \_long\_def\_doc #1\_cod {\_skiptoeol}
406
407 \_doc ------------------
408 \`\docgen` processes lines before `\_codedecl` because the version text
409 in the macro `\_<pkg>_version` can be defined here. The package
410 documentation can print it. \^`\docgen` prints banner to log because TeX doesn't do it
411 when command line doesn't begin with the main file name after parameters.
412 \_cod ------------------
413
414 \_def\_docgen #1 {\_ea \_docgenA \_input{#1.opm}}
415 \_long \_def\_docgenA #1\_codedecl#2\_endcode #3\_doc {#1\_wlog{\_banner}\_skiptoeol}
416
417 \_public \docgen ;
418
419 \_endcode % -------------------------------------
420
421 2026-03-23 \_loadA: unwanted space removed
422 2026-03-07 \nospaceafter reimplemented in order to allow \nospaceafter{text}
423 2024-10-31 \_tmptoks replaced by \_repltoks in \replstring
424 2024-09-09 \replstring: uses \toksapp (better speed) and \empty (keeps braces)
425 2024-02-10 \optdef reimplemented, it can create expandable macros now.
426 2024-01-18 \bracedparam reimplemented.
427 2023-12-04 \nnum introduced
428 2023-01-18 \bp, \expr have optional syntax for setting dec-digits
429 2022-11-24 \setpos, \posx, \posy moved from OpTeX trick here
430 2022-11-22 \docgen writes banner to log
431 2022-11-20 \docgen reads lines before \_codedecl in order to define pkg. version
432 2022-11-18 \cstochar introduced
433 2022-11-13 \docgen introduced, \bracedparam corrected, \scantoeol modified
434 2022-11-05 \replstring can be expandable using \immediateassigned: \toksapp replaced
435 2022-11-03 \nospacefuturelet added.
436 2022-10-27 \_savemathsb ...\_restoremathsb added to \load (eg. \load[xstring])
437 2022-05-24 \optdef: used \csstring instead \string, bug fixed.
438 2021-04-25 \normalcatcodes: typo fixed
439 2021-04-22 \the\_currctable replaced by \the\_catcodetable, bug fixed.
440 2020-05-22 robust \catcode newly defined in order \catcode\string.... be possible
441 2020-05-03 \load macro introduced
442 2020-03-15 released