Debian Codegraph @

texmf-dist/doc/latex/unicode-math-input/unicode-math-input.tex

raw
1 %! TEX program = lualatex
2 \ProvidesFile{unicode-math-input.tex} [2024/01/25 v0.1.1 ]
3 \PassOptionsToPackage{hyphens}{url}
4 \RequirePackage{fvextra}
5 \documentclass{l3doc}
6 \usepackage[svgnames]{xcolor}
7 \EnableCrossrefs
8 \CodelineIndex
9 \fvset{breaklines=true,gobble=0,tabsize=4,frame=single,numbers=left,numbersep=3pt}
10 \AtBeginDocument{\DeleteShortVerb\"}
11 \MakeOuterQuote{"}
12 \usepackage{cleveref}
13 \usepackage{fontspec}
14 \setmonofont{DejaVu Sans Mono}[Scale=0.8]
15 \usepackage[bold-style=ISO]{unicode-math}
16 \tracinglostchars=3
17 \newcommand\csref[1]{\texttt{\hyperref[doc/function//#1]{\textbackslash #1}}}
18 \newcommand\varref[1]{\texttt{\hyperref[doc/function//#1]{#1}}}
19 \usepackage{precattl}
20 \begin{document}
21 \precattlExec{
22 \NewDocumentEnvironment{option}{v}{\begin{variable}{#1\cO\}\iffalse}\fi Package option.\par}{\end{variable}}
23 }
24
25 \hfuzz=1pt
26 \GetFileInfo{\jobname.tex}
27
28 \title{\pkg{unicode-math-input} --- Allow entering Unicode symbols in math formulas\thanks{This file
29 describes version \fileversion, last revised \filedate.}
30 }
31 \author{user202729%
32 %\thanks{E-mail: (not set)}
33 }
34 \date{Released \filedate}
35
36 \maketitle
37
38 \begin{abstract}
39 Allow entering Unicode symbols in math formulas.
40 \end{abstract}
41
42 \section{Introduction}
43
44 This package allows entering Unicode symbols in math formulas.
45
46 \subsection{Existing packages}
47
48 There are several existing packages, but other than |unicode-math| (which also changes the output encoding)
49 they does not cover a lot of characters and/or does not handle several issues well.
50
51 We compare the situation with several existing packages:
52
53 \begin{itemize}
54 \item \pkg{commonunicode}:
55 \begin{itemize}
56 \item defines all characters to be active, which means it breaks usage of |α| in \pkg{fancyvrb}'s |Verbatim| environment for example.
57 \item changes the behavior of e.g. |½| in text mode in PDF\LaTeX.
58 \item does not always select best option, for example |∄| always get mapped to |\not\exists| even though the outcome is worse than |\nexists|.
59 \item fakes several symbols such as |≝| even when there's better option e.g. |\eqdef|,
60 \item uses |\ensuremath| extensively, which means no error message when it's used in text mode,
61 \item not as good symbol coverage.
62 \end{itemize}
63 \item \pkg{unixode}:
64 \begin{itemize}
65 \item defines |′| to be |\prime| which is big and not usable, it should be |^{\prime}|
66 similar to |'|'s definition.
67 \item defines |–| (en dash) to be nothing, which breaks the character even in text mode.
68 \item does not define |×| or |±| (they're already valid in text mode in \LaTeX, but will be
69 silently omitted in math mode)
70 \item does not handle consecutive superscript/subscript characters.
71 \item you need to manually patch the source code a bit in order to make it work with PDF\LaTeX. And even after that it will raise lots of warnings about redefining Unicode characters.
72 \end{itemize}
73 \item \pkg{utf8x}:
74 \begin{itemize}
75 \item incompatible with lots of packages.
76 \item does not define $\bigoplus$ (|\bigoplus|)
77 \item also does not handle consecutive superscript/subscript characters.
78 \end{itemize}
79 \end{itemize}
80
81 See also \url{https://tex.stackexchange.com/a/628285}.
82
83 \subsection{Features}
84
85 \LaTeX's implementation of input encoding and font encoding is \emph{very} complicated, necessitated by the fact that non-Unicode \TeX\ engines handles each UTF-8 character as multiple tokens and enc\TeX\ extension is not enabled in \LaTeX.%
86 \footnote{Refer to \url{https://tex.stackexchange.com/a/266282/250119} for a way to force-enable enc\TeX\ extension in
87 \LaTeX\ if you're interested.}
88
89 There's a few other issues that we don't really need to deal with, because they are in the next layer:
90
91 \begin{itemize}
92 \item \href{https://tex.stackexchange.com/q/31640/250119}{What is the use of the command \texttt{\textbackslash IeC}?}
93 \item \url{https://tex.stackexchange.com/a/239575/250119}
94
95 \end{itemize}
96
97 We don't need to deal with |\IeC| as since \TeX\ Live 2019, the mechanism is no longer used and the Unicode character itself
98 is written to auxiliary files.
99 %\immediate\openout 5=a.tex
100 %\makeatletter
101 %\let \protect \@unexpandable@protect
102 %\immediate\write 5{a á b}
103
104 We need to get the following things correct:
105
106 \begin{itemize}
107 \item |\left⟨|
108
109 In Lua\LaTeX\ in order to implement this we need to hard code the |\Udelcode| of the character,
110 so if |\langle| is redefined, the change will not follow.
111
112 An alternative is to overwrite the definition of |\left| built-in, but this is not used.
113
114 \item |\big⟨| (in \pkg{amsmath} package or outside)
115
116 In PDF\LaTeX\ there's an issue of argument-grabbing (|\big| etc. is a macro so they will
117 only grab the first octet of the |⟨| character), so the macro must be patched.
118
119 Furthermore, the patching is done |\AtBeginDocument| in case |amsmath| etc. is loaded after this package.
120
121 We handle |\big \Big \bigg \Bigg| and the |\bigl|, |\bigr| variants etc.
122
123 Pass the option \varref{ignore-patch-delimiter-commands} to disable the behavior in case of package clash.
124
125 \item in \pkg{unicode-math}, |a`| renders as |a^{\backprime}| i.e. $a^{\backprime}$.
126 We will not modify the default behavior i.e. $a\text{`}$ in this package.
127
128 \item |\section{$1 × 2$}| (for writing to auxiliary file in table of contents) -- as mentioned above,
129 since \TeX\ Live 2019 this is correct by default.
130 \item Some characters such as |×| or |½| in PDF\LaTeX\ are already
131 usable outside math mode, we try to not break the compatibility.
132 \item The symbol should work correctly when appear at the start of an alignment entry,
133 e.g., the start of an |align*| cell.
134 \item |$2³⁴$| (consecutive Unicode characters for superscript/subscript, refer to \url{https://tex.stackexchange.com/q/344160/250119}.) Also need to handle |'| similarly.
135
136 \item This packages does modify the default definition of |'| to allow |G'²| to work however.
137 Pass the option \varref{ignore-patch-prime} to disable the behavior in case of package clash.
138
139 \item The original implementation of |'| is somewhat interesting that it allows sequences such as |G'^\bgroup 123\egroup| to work,
140 we will not emulate it here.
141
142 \item Also need to handle Unicode prime symbols |′|, |″| etc.
143
144 \item To minimize errors, we make |≢| default to |\nequiv|, but fallback to |\not\equiv|
145 if the former is not available.
146
147 We should also take care of aliases -- for example, |≰| should check |\nle| and |\nleq| before fallback to |\not\le| or |\not\leq|.
148
149 Note that by default (or with \pkg{amsmath} or \pkg{amssymb}),
150 |\not| does not smartly check the following symbol,
151 however with some packages such as \pkg{unicode-math},
152 \pkg{txfonts} the |\not| does do that -- in particular, it checks for the presence of control sequences named
153 |\notXXX| and |\nXXX| where |XXX| stands for the original control sequence/character.
154
155 It would be beneficial for \pkg{amssymb}
156 to make |\not| smart, as for example |\not\exists| looks worse than |\nexists|,
157 however the package does not touch |\not|.
158
159 \item Similarly, |''| default to |^{\dprime}| if available, else fallback to |^{\prime\prime}|.
160
161 \item Whenever possible, we do not make the symbols have active catcode, only change the mathcode,
162 that way usage of the symbols in places such as |fancyvrb| environment is minimally affected. (see test files for an example)
163 \item We try to make minimum assumptions about the internal implementation details of \LaTeX\ packages; nevertheless this is not always possible.
164
165 \item Combining modifiers (such as |U+00305 COMBINING OVERLINE| in |a̅|, which corresponds to |\overbar|) are difficult to support
166 (although with whole-file scanning + \pkg{rescansync} or Lua\TeX's |process_input_buffer| callback
167 it's not impossible; an alternative is to use Lua\TeX\ callback to modify the math list
168 after it's constructed, see \url{https://github.com/wspr/unicode-math/issues/555#issuecomment-1045207378}
169 for an example),
170 plus |unicode-math| does not support them anyway,
171 so they will not be supported.
172
173 They're difficult to support because normally the modifier appear after the character that it modifies
174 but \TeX\ requires the command (e.g. |\overbar|) to appear \emph{before} the character that it modifies.
175
176 As a special case, the 4 commands |\enclosecircle| |\enclosesquare| |\enclosediamond| and |\enclosetriangle|
177 are supported (simply because the \TeX\ command can appear after the character it modifies)
178
179 \item The fraction slash |U+2044 FRACTION SLASH|, as in |1|⁄|2| rendering $\frac{1}{2}$, is also not implemented
180 because of similar difficulty as above.
181 \item Symbols such as |√| or |∛| will be equivalent to |\sqrt| command (taking an argument to draw a square root)
182 instead of |\surd| (the symbol itself), unlike \pkg{unicode-math}.
183
184 While sequences such as |⁵√{67}| may feasibly be supported without breaking too many things,
185 implementation is difficult and we don't see much use for it.
186 \item Similarly, one might expect that $⏟$ |U+23DF BOTTOM CURLY BRACKET| get mapped to |\underbrace|,
187 but the behavior of such command would be a bit unexpected (you need to
188 write $⏟$|{123}_{456}| to get $\smash{\underbrace{123}_{456}}$), so this will not be the default.
189
190 \item the Unicode character is mapped indirectly to the control sequence,
191 so that when the user/some package redefines a control sequence such as |\pi|, the
192 corresponding Unicode character (|π|) will also change. This will incur a small loss in efficiency however.
193
194 (modulo the issue with |\Udelcode| mentioned above)
195
196 \item The character |⋯| is mapped to |\cdots| and |…| is mapped to |\ldots|. Note that |\dots| behaves
197 the same as |\ldots| without \pkg{amsmath} package loaded, but with it it smartly detect which variant to use
198 depends on the following character, for example |$\dots +$| prints $\dots +$ but |$\dots ,$| prints $\dots ,$.
199
200 There's another discrepancy with the spacing around these 2 characters,
201 see \url{https://github.com/wspr/unicode-math/issues/571}.
202 \end{itemize}
203
204 There are some issues however:
205 \begin{itemize}
206 \item \textsf{\rlap{0}\kern 1.2pt\relax 0} |U+1D7D8 MATHEMATICAL DOUBLE-STRUCK DIGIT ZERO|
207 gets translated to |\mathbb{0}|, but this is incorrect by default
208 unless the blackboard bold font happens to have such a character.
209
210 (nevertheless, it's difficult to change math font in the middle of the document anyway.
211 Refer to \url{https://tex.stackexchange.com/q/30049/250119}.)
212
213 \item In the \pkg{unicode-math} source code there's this remark:
214
215 \begin{quote}
216 The catcode setting is to work around (strange?) behaviour in Lua\TeX\ in
217 which catcode 11 characters don't have italic correction for maths. We don't adjust
218 ascii chars, however, because certain punctuation should not have their catcodes
219 changed.
220 \end{quote}
221
222 This feature is currently unimplemented.
223
224 \item At the moment, following a Unicode superscript character, double superscript will not be defined --
225 that is, |G²^3^4| will just display as |G^{234}| --
226 while this is fixable, we don't see much point in detecting this error.
227 \end{itemize}
228
229 \section{Usage}
230
231 Simply include the package.
232
233 \begin{verbatim}
234 \usepackage{unicode-math-input}
235 \end{verbatim}
236
237 Because by default the \pkg{unicode-math} package will already allow entering Unicode symbols in math formulas, this package will raise an error if \pkg{unicode-math} is already loaded.
238
239 \section{Advanced commands and options}
240
241 \begin{function}{
242 \umiMathbf,\umiMathit,\umiMathbfit,\umiMathscr,\umiMathbfscr,\umiMathfrak,\umiMathbb,\umiMathbbit,\umiMathbffrak,\umiMathsf,\umiMathsfbf,\umiMathsfit,\umiMathsfbfit,\umiMathtt
243 }
244 \begin{syntax}
245 |\umiMathbf {...}|
246 |\umiMathit {...}|
247 \end{syntax}
248 These functions are not to be used directly. But you can redefine them to customized behavior of bold/italic/etc. Unicode characters.
249
250 For example you can |\renewcommand\umiMathbf[1]{\mathbf{#1}}| which is the default behavior.
251
252 Or you can execute, for example, |\renewcommand\umiMathscr[1]{\mathcal{#1}}| to use the calligraphic instead of the script alphabet for script characters.
253
254 More usefully, you may want to |\renewcommand\umiMathbf{\bm}| to make entered characters such as
255 $𝐚$ appear bold italic in the output, remember to load package \pkg{bm} if you want to do so (which is |unicode-math| behavior
256 with |[bold-style=ISO]| package option).
257 \end{function}
258
259 \begin{function}{\umiFrac}
260 \begin{syntax}
261 |\umiFrac {1} {2}|
262 \end{syntax}
263 Not to be used directly, but you can redefine it such as
264 |\let\umiFrac\tfrac| (or more clearly, |\renewcommand\umiFrac[2]{\tfrac{#1}{#2}}|)
265 to customize the appearance of Unicode characters like |½|.
266
267 If you want to customize the appearance of individual symbols, consider using \csref{umiDeclareMathChar}.
268 \end{function}
269
270 \begin{function}{\umiDeclareMathChar}
271 \begin{syntax}
272 |\umiDeclareMathChar {α} {\alpha}|
273 \end{syntax}
274 Does what it says. Will override existing definitions, if any.
275
276 Note that the Unicode character must be braced.
277
278 (You may choose to call \csref{umiPatchCmdUnicodeArg}| \umiDeclareMathChar|
279 beforehand so bracing is not necessary, but this is not really recommended)
280
281 This might or might not destroy the existing text-mode definition. For now,
282 one way to preserve it is |\umiDeclareMathChar {²} {\TextOrMath{\texttwosuperior}{^2}}|.
283 \end{function}
284
285 \begin{function}{\umiDeclareMathDelimiter}
286 \begin{syntax}
287 |\umiDeclareMathDelimiter {⟨} \langle|
288 \end{syntax}
289 You must use this in order to use the Unicode character with |\left|, |\big|, |\bigl| etc.
290 (because of the internal detail being that in Xe\LaTeX\ and Lua\LaTeX,
291 as this package does not change the character catcode to be active,
292 it's necessary to set the |delcode| as mentioned before)
293
294 In that case the second argument must be a single token.
295
296 Unfortunately, the command does not always work -- it must detect the second argument to be a delimiter, but
297 if the detection fails it may not work.
298 \end{function}
299
300 \emph{Note}: There's no need to provide |\umiDeclareMathAlphabet|, |\umiDeclareMathAccent| or |\umiDeclareMathRadical|, for |\umiDeclareMathChar| suffices.
301 It's not supported to define \emph{control sequences}, for that the typical |\RenewDocumentCommand|
302 or |\RenewCommandCopy| suffices.
303
304 \begin{function}{\umiRefreshDelimiterList}
305 \begin{syntax}
306 |\umiRefreshDelimiterList|
307 \end{syntax}
308 You should normally not need this command.
309
310 As mentioned before, in Lua\LaTeX\ once a command is redefined, the Unicode character does not automatically update.
311
312 This command will check all the normal delimiter Unicode characters. In PDF\LaTeX\ this command does nothing.
313
314 Another way is to use \csref{umiDeclareMathDelimiter} to manually refresh individual Unicode characters,
315 this is also useful if you define an Unicode character that is not "normally" a delimiter.
316 \end{function}
317
318 \begin{option}{ignore-refresh-delimiter-list}
319 \csref{umiRefreshDelimiterList} will be run |\AtBeginDocument|. Pass this to disable it running.
320
321 Only needed if there's some package clash or if there's spurious warning on "not determined to be a delimiter" etc.
322 \end{option}
323
324 \begin{function}{\umiPatchCmdUnicodeArg,\umiUnpatchCmdUnicodeArg}
325 \begin{syntax}
326 |\umiPatchCmdUnicodeArg \sqrt|
327 |\umiUnpatchCmdUnicodeArg \sqrt|
328 \end{syntax}
329 After executing this command, the command specified in the argument (|\sqrt| in this example)
330 can be called with one argument being an Unicode character without needing a brace.
331
332 (i.e. you can write |\sqrt α| instead of |\sqrt{α}|.)
333
334 Because of implementation detail,
335 \begin{itemize}
336 \item |\sqrtα| (without the space between |\sqrt| and |α|) works in PDF\LaTeX\ but not Lua\LaTeX. (so this form is not recommended.)
337 \item |\sqrt α| works in Lua\LaTeX\ without needing the patch. In other words, the patch does nothing in Unicode engines.
338 \end{itemize}
339
340 The command being patched must take at least one mandatory argument as the first argument, and it only affect that first argument.
341 In other words, |\sqrt[3]α| cannot be patched this way unless you do e.g. |\newcommand\cbrt[1]{\sqrt[3]{#1}}| then |\umiPatchCmdUnicodeArg\cbrt|,
342 then |\cbrt α| works (but |\sqrt[3]α| still doesn't).
343 \end{function}
344
345 \begin{function}{\umiPatchCmdUnicodeTwoArgs}
346 \begin{syntax}
347 |\umiPatchCmdUnicodeTwoArgs \frac|
348 |\umiUnpatchCmdUnicodeArg \frac|
349 \end{syntax}
350 Similar to above, but for commands with (at least) two mandatory arguments.
351
352 Only affects these 2 mandatory arguments.
353 \end{function}
354
355 \begin{function}{\umiPatchCmdUnicodeArgExtraGroup}
356 \begin{syntax}
357 |\umiPatchCmdUnicodeArgExtraGroup \Big|
358 \end{syntax}
359 Don't use this command unless you know exactly what you're doing.
360
361 Similar to |\umiPatchCmdUnicodeArg|, but open an implicit group before executing anything and close the group after.
362
363 The command being patched must take exactly one argument.
364
365 This is useful because some \TeX\ primitives such as |^| or |\mathopen|
366 requires either a single "character" or a group braced with |{...}| / |\bgroup...\egroup| --
367 in particular, |\Big|'s original definition is such that |\Bigl| being defined
368 as |\mathopen \Big| can work, and we must ensure it still work after the patch.
369 \end{function}
370
371 \begin{option}{ignore-patch-delimiter-commands}
372 Pass this to avoid patching |\Big| etc. with the command above (only needed if there's some package clash).
373 \end{option}
374
375 \begin{function}{\umiBraceNext}
376 \begin{syntax}
377 |\umiBraceNext {abc...} αxyz...|
378 \end{syntax}
379 In the example above, after some steps of execution of \TeX, the state will be
380 |abc... {α}xyz...|.
381
382 Formally: if the character following the first argument to |\umiBraceNext| is not representable in a single byte
383 and the engine is not Unicode, the character will be braced, otherwise nothing happens. Then the argument
384 is put back in the input stream.
385
386 This is an internal command mainly useful for defining the command above, for example after
387 \begin{verbatim}
388 \let\oldbig\big
389 \def\big{\umiBraceNext{\oldbig}}
390 \end{verbatim}
391 then |\big⟨| will eventually execute |\oldbig{⟨}| which is the desired behavior (that |\oldbig| expects one braced argument).
392 \end{function}
393
394 \begin{option}{ignore-patch-prime}
395 Do not patch the default definition of |'| in math mode.
396
397 By default it's patched to allow |G'²| and |G²'| to work. Only use this when there's some package clash.
398 \end{option}
399
400 \begin{function}{\umiPatchPrime,\umiUnpatchPrime}
401 \begin{syntax}
402 |\umiPatchPrime|
403 |\umiUnpatchPrime|
404 \end{syntax}
405 As mentioned above, by default |\umiPatchPrime| is run |\AtBeginDocument|. But it can be patched
406 and unpatched manually.
407
408 Note that it's undefined behavior if some package modifies the definition of active |'|
409 while it's patched. To resolve conflict, unpatch |'|, load the package, then patch again.
410 \end{function}
411
412 \section{Compatibility}
413
414 This package should have tested with various \TeX\ distribution versions on Overleaf.
415
416 \section{Advanced remarks}
417
418 As mentioned before, by design this package defines the Unicode character in math mode to do whatever the corresponding
419 \LaTeX\ command does \emph{at the time of use}, so if you redefine the meaning of |\alpha|, then the Unicode character |α|
420 will change as well.
421
422 The other "standard" way to define commands in \LaTeX\ is to assign the mathcode to the character/control sequence directly,
423 using |\DeclareMathSymbol| etc. which is used to define almost all the standard control sequences.
424 For efficiency reasons or other reasons, you may want to \emph{copy} the definition of an existing control sequence
425 (this way the definition of the Unicode character is not changed when the control sequence changes),
426 you can do that by:
427
428 \begin{function}{\umiDeclareMathCharCopy}
429 \begin{syntax}
430 |\umiDeclareMathCharCopy {±} \pm|
431 \end{syntax}
432 Does what it says.
433
434 The second argument must be a single control sequence.
435 \end{function}
436
437 \begin{function}{\umiDeclareMathDelimiterCopy}
438 \begin{syntax}
439 |\umiDeclareMathDelimiterCopy {‖} \Vert|
440 \end{syntax}
441 Does what it says. Refer to \csref{umiDeclareMathDelimiter} for difference between this command and \csref{umiDeclareMathCharCopy}.
442 \end{function}
443
444 In case you want to explicitly specify a font/slot pair for an Unicode character, you can use |\DeclareMathSymbol| etc.
445 directly, then use one of the commands above to copy it to the Unicode character.
446
447 Useful resources:
448
449 \begin{sloppypar}
450 \hbadness=10000
451 \begin{itemize}
452 \item \url{https://tex.stackexchange.com/questions/98781/create-a-font-table-for-all-available-characters-for-a-particular-font}
453 \item \url{https://tex.stackexchange.com/questions/380775/font-table-for-opentype-truetype-fonts}
454 \item \url{https://ctan.org/pkg/fonttable} (need double quotes if font name has spaces: \url{https://tex.stackexchange.com/a/506246/250119})
455 \item Although there's always |texdoc encguide| for the default (non-Unicode) encodings.
456 \end{itemize}
457 \end{sloppypar}
458
459
460 \PrintChanges
461 \PrintIndex
462 \Finale
463
464 \end{document}