| 1 |
#!/bin/python3 |
| 2 |
""" |
| 3 |
This file is not used while TeX is running. It's for generating unicode-math-input-table.tex file only. |
| 4 |
This requires pythonimmediate (not sure which version is compatible but |
| 5 |
commit 020068db8a966c138b5b0b93695c0fefdef03d0a on Python 3.11.3 is) |
| 6 |
|
| 7 |
To generate: run:: |
| 8 |
python3 unicode-math-input-script.py > unicode-math-input-table.tex |
| 9 |
|
| 10 |
How does it work? |
| 11 |
|
| 12 |
The mapping is determined from multiple sources: |
| 13 |
|
| 14 |
* The unicode-math package itself, which defines a "command → Unicode character" mapping. |
| 15 |
This does not always work because different TeX packages may name the command differently. |
| 16 |
|
| 17 |
* Synonym table, obtained by looking at STIX's command definition |
| 18 |
|
| 19 |
* TeX's glyph → unicode mapping (used to facilitate copy-paste in PDF), |
| 20 |
e.g. /usr/share/texmf-dist/tex/generic/pdftex/glyphtounicode.tex |
| 21 |
This should be good, but is currently not used. Furthermore, not all TeX commands are implemented by |
| 22 |
getting a single character from a font... |
| 23 |
|
| 24 |
How does the Unicode mapping work? |
| 25 |
|
| 26 |
First there's the `pdftex.map` file, then there's umsa.cmap for msam10.tfm/afm/pfm/pfb/mf (metafont source file) |
| 27 |
|
| 28 |
/usr/share/texmf-dist/fonts/source/public/amsfonts/symbols/msam10.mf |
| 29 |
/usr/share/texmf-dist/fonts/source/public/amsfonts/symbols/asymbols.mf |
| 30 |
|
| 31 |
/usr/share/texmf-dist/fonts/afm/public/amsfonts/symbols/msam10.afm |
| 32 |
→ plaintext-looking file may work |
| 33 |
|
| 34 |
/usr/share/texmf-dist/fonts/tfm/public/amsfonts/symbols/msam10.tfm |
| 35 |
/usr/share/texmf-dist/fonts/type1/public/amsfonts/symbols/msam10.pfm |
| 36 |
/usr/share/texmf-dist/fonts/type1/public/amsfonts/symbols/msam10.pfb |
| 37 |
|
| 38 |
The glyphtounicode.tex may be a bit problematic... |
| 39 |
https://tex.stackexchange.com/questions/66300/how-to-fix-missing-or-incorrect-mappings-from-glyphtounicode-tex |
| 40 |
|
| 41 |
See also: section 3.2 How to find a table of correspondences? in https://tex.stackexchange.com/a/628285/250119 |
| 42 |
|
| 43 |
""" |
| 44 |
|
| 45 |
from __future__ import annotations |
| 46 |
|
| 47 |
from pythonimmediate.engine import ChildProcessEngine |
| 48 |
from pythonimmediate.engine import default_engine |
| 49 |
from pythonimmediate import* |
| 50 |
import pythonimmediate |
| 51 |
from collections import defaultdict, Counter |
| 52 |
from itertools import groupby |
| 53 |
import os |
| 54 |
import json |
| 55 |
import subprocess |
| 56 |
import re |
| 57 |
import sys |
| 58 |
import unicodedata |
| 59 |
import functools |
| 60 |
from dataclasses import dataclass |
| 61 |
|
| 62 |
# ======== |
| 63 |
|
| 64 |
print(r'% This file is automatically generated from unicode-math-input-script.py.') |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
# ======== start a luatex engine |
| 69 |
# https://tex.stackexchange.com/questions/574607/tex-hashtokens-incomplete |
| 70 |
default_engine.set_engine(ChildProcessEngine("luatex", env={**os.environ, "hash_extra": "0"})) |
| 71 |
|
| 72 |
|
| 73 |
""" |
| 74 |
from the TeXbook: |
| 75 |
|
| 76 |
(INITEX starts out with |
| 77 |
\mathcode x = x for all characters x that are neither letters nor digits. The ten digits |
| 78 |
have \mathcode x = x+"7000; the 52 letters have \mathcode x = x+"7100.) |
| 79 |
""" |
| 80 |
# ======== reset all mathcode to 0 |
| 81 |
TokenList([r"\directlua", TokenList.fstr( |
| 82 |
r""" |
| 83 |
for i=0, 0x10ffff do |
| 84 |
tex.setmathcode(i, {0, 0, 0}) |
| 85 |
end |
| 86 |
""" |
| 87 |
)]).execute() |
| 88 |
|
| 89 |
# ======== load unicode-math |
| 90 |
execute(r''' |
| 91 |
\documentclass{article} |
| 92 |
\usepackage{unicode-math} |
| 93 |
\begin{document} |
| 94 |
''') |
| 95 |
|
| 96 |
# ======== print changed mathcodes (we aim to support all of these) |
| 97 |
changed_mathcodes = TokenList([r"\directlua", TokenList.fstr( |
| 98 |
r""" |
| 99 |
for i=0, 0x10ffff do |
| 100 |
local cls, family, pos=table.unpack(tex.getmathcode(i)) |
| 101 |
if not ( |
| 102 |
--(cls==0 and family==0 and pos==i) or (cls==7 and family==1 and pos==i) |
| 103 |
(cls==0 and family==0 and pos==0) |
| 104 |
) then |
| 105 |
tex.print(-2, i .. ":"..utf8.char(i)..":" .. cls..' '..family..' '..pos .. "\n") |
| 106 |
end |
| 107 |
end |
| 108 |
""" |
| 109 |
)]).expand_x().str() |
| 110 |
|
| 111 |
changed_chars: set[str] = set() |
| 112 |
for line in changed_mathcodes.splitlines(): |
| 113 |
match = re.fullmatch(r'(\d+):(.):(\d+) (\d+) (\d+)', line) |
| 114 |
assert match |
| 115 |
unicode_char = match[2] |
| 116 |
assert match[2]==chr(int(match[1])), match |
| 117 |
if match[3]=="8" and match[4]=="0" and match[5]=="0": |
| 118 |
code = Umathcode.active |
| 119 |
else: |
| 120 |
code = Umathcode(int(match[4]), MathClass.lookup(int(match[3])), int(match[5])) |
| 121 |
changed_chars.add(unicode_char) |
| 122 |
|
| 123 |
# ======== parse the unicode math table |
| 124 |
|
| 125 |
path = subprocess.run(["kpsewhich", "unicode-math-table.tex"], stdout=subprocess.PIPE).stdout |
| 126 |
lines = Path(path.decode('u8').strip('\n')).read_text().splitlines() |
| 127 |
lines = [line for line in lines if line and not line.startswith("%")] |
| 128 |
unicode_math_table_=defaultdict(list) |
| 129 |
for line in lines: |
| 130 |
match = re.fullmatch(r'\\UnicodeMathSymbol{"(.*)}{\\(.*?) *}{\\math(.*)}{(.*)}%', line) |
| 131 |
assert match |
| 132 |
unicode_char=chr(int(match[1], 16)) |
| 133 |
csname=match[2] |
| 134 |
#unicode_math_table_.append(Item(unicode_char=unicode_char, csname)) |
| 135 |
unicode_math_table_[unicode_char].append(csname) |
| 136 |
unicode_math_table={unicode_char: tuple(csnames) for unicode_char, csnames in unicode_math_table_.items()} |
| 137 |
|
| 138 |
# ======== extract unicode-math synonyms |
| 139 |
|
| 140 |
def control_sequences()->list[str]: |
| 141 |
return (lua_try_eval(r""" |
| 142 |
do |
| 143 |
local s={} |
| 144 |
for k, v in pairs(tex.hashtokens()) do |
| 145 |
if v:find("^[A-Za-z]+$") then |
| 146 |
s[v]=0 |
| 147 |
end |
| 148 |
end |
| 149 |
local t={} |
| 150 |
for v, _ in pairs(s) do table.insert(t, v) end |
| 151 |
return table.concat(t, "\x00") |
| 152 |
end |
| 153 |
""") or "").split("\x00") |
| 154 |
|
| 155 |
extra_synonyms_list: list[list[str]] = [ |
| 156 |
["adots", "iddots"], |
| 157 |
["unicodecdots", "cdots"], # https://github.com/wspr/unicode-math/issues/571 |
| 158 |
["unicodeellipsis", "ldots"], |
| 159 |
#["llbracket", "lBrack"], |
| 160 |
#["rrbracket", "rBrack"], |
| 161 |
] |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
c=control_sequences() |
| 167 |
m={x: T[x].meaning_str() for x in c} |
| 168 |
|
| 169 |
pattern=re.compile(r'\\protected macro:->\\([A-Za-z]+) ?') |
| 170 |
|
| 171 |
extra_synonyms_list += [[c, match[1]] for c, m in m.items() |
| 172 |
if (match:=pattern.fullmatch(m)) |
| 173 |
] |
| 174 |
|
| 175 |
def same_meaning_control_sequences(meaning: dict[str, str])->list[list[str]]: |
| 176 |
return [ |
| 177 |
l |
| 178 |
for m, l0 in groupby(sorted(c, key=lambda x: meaning[x]), lambda x: meaning[x]) |
| 179 |
if m!="undefined" |
| 180 |
for l in [[*l0]] |
| 181 |
if len(l)>=2 |
| 182 |
] |
| 183 |
|
| 184 |
extra_synonyms_list += same_meaning_control_sequences(m) |
| 185 |
|
| 186 |
# ======== extract amsmath&stix synonyms |
| 187 |
|
| 188 |
|
| 189 |
m_values=[] |
| 190 |
for preamble in [ |
| 191 |
r""" |
| 192 |
\documentclass{article} |
| 193 |
\usepackage{amsmath} |
| 194 |
\usepackage{amssymb} |
| 195 |
\usepackage{amsfonts} |
| 196 |
\begin{document} |
| 197 |
""", |
| 198 |
r""" |
| 199 |
\documentclass{article} |
| 200 |
\usepackage{stix} |
| 201 |
\begin{document} |
| 202 |
""" |
| 203 |
]: |
| 204 |
with ChildProcessEngine("luatex", env={**os.environ, "hash_extra": "0"}) as e, default_engine.set_engine(e): |
| 205 |
execute(preamble) |
| 206 |
c=control_sequences() |
| 207 |
m={x: T[x].meaning_str() for x in c} |
| 208 |
extra_synonyms_list += same_meaning_control_sequences(m) |
| 209 |
m_values.append(m) |
| 210 |
[amsmath_meaning, stix_meaning]=m_values |
| 211 |
# ======== build extra_synonyms table |
| 212 |
|
| 213 |
while True: |
| 214 |
tmp=Counter([x for l in extra_synonyms_list for x in l]) |
| 215 |
[(item, frequency)]=tmp.most_common(1) |
| 216 |
if frequency==1: break |
| 217 |
assert frequency>1 |
| 218 |
extra_synonyms_list=[ |
| 219 |
# the group that contain item |
| 220 |
[*{x for l in extra_synonyms_list if item in l for x in l}] |
| 221 |
] + [ |
| 222 |
# remaining groups |
| 223 |
l for l in extra_synonyms_list if item not in l] |
| 224 |
|
| 225 |
|
| 226 |
extra_synonyms_list=sorted([sorted(l) for l in {frozenset( |
| 227 |
item for item in l |
| 228 |
if item not in ("dotsc", "dotsm", "dotsb", "dots") # some simple filtering -- we will just use \cdots and \ldots |
| 229 |
) for l in extra_synonyms_list} if len(l)>1]) # deduplicate |
| 230 |
|
| 231 |
tmp=Counter(sum(extra_synonyms_list, [])) |
| 232 |
assert tmp.most_common()[0][1]==1, tmp |
| 233 |
|
| 234 |
extra_synonyms = {v: u for u in extra_synonyms_list for v in u} |
| 235 |
|
| 236 |
# ======== check how much of the table is valid on unicode-math/luatex |
| 237 |
|
| 238 |
def getdelcode(x: str)->tuple[int, int, int, int]: |
| 239 |
return tuple(map(int, TokenList([r"\directlua", TokenList.fstr( # type: ignore |
| 240 |
r""" |
| 241 |
for _, v in ipairs(tex.getdelcode(""" + str(ord(x)) + r""")) do tex.sprint(v..",") end |
| 242 |
""" |
| 243 |
)]).expand_x().str().rstrip(",").split(","))) |
| 244 |
|
| 245 |
@functools.lru_cache(maxsize=None) |
| 246 |
def meaning(csname: str)->str: |
| 247 |
return T[csname].meaning_str() |
| 248 |
|
| 249 |
@functools.lru_cache(maxsize=None) |
| 250 |
def good_delimiter(meaning: str, ch: str)->bool: |
| 251 |
math = umathcode[ch] |
| 252 |
o = ord(ch) |
| 253 |
if math.family!=0 or math.position!=o: return False |
| 254 |
a, b, c, d = getdelcode(ch) |
| 255 |
if a!=0 or b!=o or c!=0 or d!=0: return False |
| 256 |
other = f'\\protected macro:->\\Udelimiter {math.cls.value}\\symoperators "{o:05X}\\scan_stop: ' |
| 257 |
return meaning==other |
| 258 |
|
| 259 |
specially_handled = { |
| 260 |
match[1] for match in |
| 261 |
re.finditer(r'\\__umi_special_handle{(.)}', Path("unicode-math-input.sty").read_text()) |
| 262 |
} |
| 263 |
|
| 264 |
not_handled = {*"⎴⎵⏜⏝⏞⏟⟌\u03a2\U0001d455"} |
| 265 |
|
| 266 |
math_alphabet_translate = { |
| 267 |
"mup" : None, |
| 268 |
"mbf" : "umiMathbf", |
| 269 |
"mit" : "umiMathit", |
| 270 |
"mbfit" : "umiMathbfit", # https://tex.stackexchange.com/questions/14395/bold-italic-vectors |
| 271 |
"mscr" : "umiMathscr", |
| 272 |
"mbfscr" : "umiMathbfscr", # https://tex.stackexchange.com/questions/23455/latex-calligraphic-script-bold |
| 273 |
"mfrak" : "umiMathfrak", |
| 274 |
"Bbb" : "umiMathbb", |
| 275 |
"mitBbb" : "umiMathbbit", # https://tex.stackexchange.com/questions/16645/blackboard-italic-font |
| 276 |
"mbffrak" : "umiMathbffrak", # https://tex.stackexchange.com/questions/610696/may-i-have-bold-mathfraktur |
| 277 |
"msans" : "umiMathsf", |
| 278 |
"mbfsans" : "umiMathsfbf", # https://tex.stackexchange.com/questions/340097/bold-sans-serif-math-font |
| 279 |
"mitsans" : "umiMathsfit", |
| 280 |
"mbfitsans": "umiMathsfbfit", |
| 281 |
"mtt" : "umiMathtt", |
| 282 |
} |
| 283 |
math_alphabet_translate = dict(sorted(math_alphabet_translate.items(), key=lambda x: -len(x[0]))) # match against longest prefix first |
| 284 |
math_alphabet_csname_translation = { |
| 285 |
"alpha": r"\alpha", |
| 286 |
"Alpha": r"\Alpha", |
| 287 |
"beta": r"\beta", |
| 288 |
"Beta": r"\Beta", |
| 289 |
"chi": r"\chi", |
| 290 |
"Chi": r"\Chi", |
| 291 |
"delta": r"\delta", |
| 292 |
"Delta": r"\Delta", |
| 293 |
"digamma": r"\digamma", |
| 294 |
"Digamma": r"\Digamma", |
| 295 |
"epsilon": r"\epsilon", |
| 296 |
"Epsilon": r"\Epsilon", |
| 297 |
"eta": r"\eta", |
| 298 |
"Eta": r"\Eta", |
| 299 |
"gamma": r"\gamma", |
| 300 |
"Gamma": r"\Gamma", |
| 301 |
"iota": r"\iota", |
| 302 |
"Iota": r"\Iota", |
| 303 |
"kappa": r"\kappa", |
| 304 |
"Kappa": r"\Kappa", |
| 305 |
"lambda": r"\lambda", |
| 306 |
"Lambda": r"\Lambda", |
| 307 |
"mu": r"\mu", |
| 308 |
"Mu": r"\Mu", |
| 309 |
"nabla": r"\nabla", |
| 310 |
"nu": r"\nu", |
| 311 |
"Nu": r"\Nu", |
| 312 |
"omega": r"\omega", |
| 313 |
"Omega": r"\Omega", |
| 314 |
"omicron": r"\omicron", |
| 315 |
"Omicron": r"\Omicron", |
| 316 |
"partial": r"\partial", |
| 317 |
"phi": r"\phi", |
| 318 |
"Phi": r"\Phi", |
| 319 |
"pi": r"\pi", |
| 320 |
"Pi": r"\Pi", |
| 321 |
"psi": r"\psi", |
| 322 |
"Psi": r"\Psi", |
| 323 |
"rho": r"\rho", |
| 324 |
"Rho": r"\Rho", |
| 325 |
"sigma": r"\sigma", |
| 326 |
"Sigma": r"\Sigma", |
| 327 |
"sum": r"\sum", |
| 328 |
"tau": r"\tau", |
| 329 |
"Tau": r"\Tau", |
| 330 |
"theta": r"\theta", |
| 331 |
"Theta": r"\Theta", |
| 332 |
"upsilon": r"\upsilon", |
| 333 |
"Upsilon": r"\Upsilon", |
| 334 |
"varepsilon": r"\varepsilon", |
| 335 |
"varkappa": r"\varkappa", |
| 336 |
"varphi": r"\varphi", |
| 337 |
"varpi": r"\varpi", |
| 338 |
"varrho": r"\varrho", |
| 339 |
"varsigma": r"\varsigma", |
| 340 |
"vartheta": r"\vartheta", |
| 341 |
"varTheta": r"\varTheta", |
| 342 |
"xi": r"\xi", |
| 343 |
"Xi": r"\Xi", |
| 344 |
"zeta": r"\zeta", |
| 345 |
"Zeta": r"\Zeta", |
| 346 |
"a": "a", |
| 347 |
"A": "A", |
| 348 |
"b": "b", |
| 349 |
"B": "B", |
| 350 |
"c": "c", |
| 351 |
"C": "C", |
| 352 |
"d": "d", |
| 353 |
"D": "D", |
| 354 |
"e": "e", |
| 355 |
"E": "E", |
| 356 |
"f": "f", |
| 357 |
"F": "F", |
| 358 |
"g": "g", |
| 359 |
"G": "G", |
| 360 |
"h": "h", |
| 361 |
"H": "H", |
| 362 |
"i": "i", |
| 363 |
"I": "I", |
| 364 |
"j": "j", |
| 365 |
"J": "J", |
| 366 |
"k": "k", |
| 367 |
"K": "K", |
| 368 |
"l": "l", |
| 369 |
"L": "L", |
| 370 |
"m": "m", |
| 371 |
"M": "M", |
| 372 |
"n": "n", |
| 373 |
"N": "N", |
| 374 |
"o": "o", |
| 375 |
"O": "O", |
| 376 |
"p": "p", |
| 377 |
"P": "P", |
| 378 |
"q": "q", |
| 379 |
"Q": "Q", |
| 380 |
"r": "r", |
| 381 |
"R": "R", |
| 382 |
"s": "s", |
| 383 |
"S": "S", |
| 384 |
"t": "t", |
| 385 |
"T": "T", |
| 386 |
"u": "u", |
| 387 |
"U": "U", |
| 388 |
"v": "v", |
| 389 |
"V": "V", |
| 390 |
"w": "w", |
| 391 |
"W": "W", |
| 392 |
"x": "x", |
| 393 |
"X": "X", |
| 394 |
"y": "y", |
| 395 |
"Y": "Y", |
| 396 |
"z": "z", |
| 397 |
"Z": "Z", |
| 398 |
"zero" : "0", |
| 399 |
"one" : "1", |
| 400 |
"two" : "2", |
| 401 |
"three": "3", |
| 402 |
"four" : "4", |
| 403 |
"five" : "5", |
| 404 |
"six" : "6", |
| 405 |
"seven": "7", |
| 406 |
"eight": "8", |
| 407 |
"nine" : "9", |
| 408 |
} |
| 409 |
|
| 410 |
math_alphabet_redundant_greek = { |
| 411 |
r"\Alpha" : "A", |
| 412 |
r"\Beta" : "B", |
| 413 |
r"\Chi" : "X", |
| 414 |
r"\Digamma": "F", |
| 415 |
r"\Epsilon": "E", |
| 416 |
r"\Eta" : "H", |
| 417 |
r"\Iota" : "I", |
| 418 |
r"\Kappa" : "K", |
| 419 |
r"\Mu" : "M", |
| 420 |
r"\Nu" : "N", |
| 421 |
r"\omicron": "o", |
| 422 |
r"\Omicron": "O", |
| 423 |
r"\Rho" : "P", |
| 424 |
r"\Tau" : "T", |
| 425 |
r"\Zeta" : "Z", |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
ASCII_symbol_synonym = { |
| 430 |
"minus": "-", |
| 431 |
"mid": "|", |
| 432 |
} |
| 433 |
|
| 434 |
## |
| 435 |
|
| 436 |
remaining_chars = changed_chars - {*unicode_math_table} - specially_handled - not_handled |
| 437 |
remaining_chars = {x for x in remaining_chars if ord(x) >= 0x80} |
| 438 |
|
| 439 |
for i in range(ord("!"), ord("~")+1): |
| 440 |
fullch=chr(0xff00+i-0x20) |
| 441 |
assert unicodedata.name(fullch) == "FULLWIDTH " + unicodedata.name(chr(i)) |
| 442 |
if fullch in remaining_chars: remaining_chars.remove(fullch) |
| 443 |
print(r'\__umi_define_char{' + fullch + r'}{\char'+str(i)+' }') |
| 444 |
|
| 445 |
defined_csnames = {x for l in unicode_math_table.values() for x in l} | {*stix_meaning} | {*amsmath_meaning} |
| 446 |
|
| 447 |
|
| 448 |
pdf_engine=ChildProcessEngine("pdftex") |
| 449 |
with default_engine.set_engine(pdf_engine): execute(r""" |
| 450 |
\documentclass{article} |
| 451 |
\usepackage{amsmath} |
| 452 |
\usepackage{amssymb} |
| 453 |
\usepackage{amsfonts} |
| 454 |
\usepackage{mathrsfs} |
| 455 |
\begin{document} |
| 456 |
""") |
| 457 |
|
| 458 |
def remove_not(a: str)->Optional[str]: |
| 459 |
global defined_csnames |
| 460 |
if a in (r"\ni", r"\nu"): return None |
| 461 |
if a.startswith(r"\not") and a.removeprefix(r"\not") in defined_csnames: |
| 462 |
return '\\' + a.removeprefix(r"\not") |
| 463 |
elif a.startswith(r"\n") and a.removeprefix(r"\n") in defined_csnames: |
| 464 |
return '\\' + a.removeprefix(r"\n") |
| 465 |
else: return None |
| 466 |
|
| 467 |
for unicode_char, csnames_ in unicode_math_table.items(): |
| 468 |
csnames = [*csnames_] |
| 469 |
if unicodedata.combining(unicode_char) != 0: |
| 470 |
if 0: |
| 471 |
print( |
| 472 |
repr(unicode_char), |
| 473 |
f"U+{ord(unicode_char):04X}", |
| 474 |
unicodedata.name(unicode_char), |
| 475 |
"mathcode: ", umathcode[unicode_char], |
| 476 |
{csname: meaning(csname) for csname in csnames}, |
| 477 |
f" -- good: {good}" if good else "" |
| 478 |
) |
| 479 |
for csname in csnames: |
| 480 |
assert "Umathaccent" in meaning(csname), (unicode_char, unicodedata.name(unicode_char), csname, meaning(csname)) |
| 481 |
continue # don't support combining characters |
| 482 |
|
| 483 |
is_combining2="COMBINING" in unicodedata.name(unicode_char).split() |
| 484 |
if is_combining2: |
| 485 |
for csname in csnames: |
| 486 |
assert csname in "enclosecircle enclosesquare enclosediamond enclosetriangle".split(), (unicode_char, csname) |
| 487 |
assert len(csnames)==1 |
| 488 |
|
| 489 |
optional_space=" " if is_combining2 else "" |
| 490 |
|
| 491 |
if ord(unicode_char) <= 0x7f: continue |
| 492 |
if unicode_char in specially_handled or unicode_char in not_handled: continue |
| 493 |
# |
| 494 |
bad_or_delimiter = [csname for csname in csnames if meaning(csname) != "the character " + unicode_char] |
| 495 |
delimiter = [csname for csname in bad_or_delimiter if good_delimiter(meaning(csname), unicode_char)] |
| 496 |
bad = [*{*bad_or_delimiter} - {*delimiter}] |
| 497 |
is_delimiter = delimiter or getdelcode(unicode_char)!=(-1, 0, 0, 0) |
| 498 |
# in unicode-math: |
| 499 |
# the situation with ⟨/langle and ↑/uparrow is different |
| 500 |
# in both cases the character gets assigned mathcode and delcode so \left⟨ and \left↑ both work |
| 501 |
# in langle case the macro is defined to be \protected macro:->\Udelimiter 4\symoperators "027E8\scan_stop: |
| 502 |
# this is because of @@_set_math_open logic which sets the macro like that |
| 503 |
# (I don't know why it doesn't just do the thing below) |
| 504 |
# in uparrow case the macro is defined to be "the character ↑" |
| 505 |
# then delcode is assigned in @@_assign_delcode manually |
| 506 |
if bad: |
| 507 |
good = [*set(csnames) - set(bad)] |
| 508 |
print( |
| 509 |
repr(unicode_char), |
| 510 |
f"U+{ord(unicode_char):04X}", |
| 511 |
unicodedata.name(unicode_char), |
| 512 |
"mathcode: ", umathcode[unicode_char], |
| 513 |
"bad: ", {csname: meaning(csname) for csname in bad}, |
| 514 |
f" -- good: {good}" if good else "" |
| 515 |
) |
| 516 |
assert False, "please specially handle this" |
| 517 |
else: |
| 518 |
csnames = [*csnames_] |
| 519 |
for csname in [*csnames]: |
| 520 |
if csname in extra_synonyms: |
| 521 |
csnames+=extra_synonyms[csname] |
| 522 |
csnames=[*{csname: None for csname in csnames}] |
| 523 |
|
| 524 |
items1=[] |
| 525 |
for csname in csnames: |
| 526 |
if not is_delimiter: |
| 527 |
with default_engine.set_engine(pdf_engine): |
| 528 |
assert "delimiter" not in T[csname].meaning_str(), (unicode_char, csname) |
| 529 |
# that is the symbol is not a delimiter in pdf_engine either (check is not particularly reliable but okay) |
| 530 |
|
| 531 |
for prefix, replacement in math_alphabet_translate.items(): |
| 532 |
if csname.startswith(prefix): |
| 533 |
assert csname not in ASCII_symbol_synonym |
| 534 |
cs = math_alphabet_csname_translation[csname.removeprefix(prefix)] |
| 535 |
def wrap_in_alphabet_selector(cs: str)->str: |
| 536 |
if replacement is None: return cs |
| 537 |
return "\\" + replacement + "{" + cs + "}" |
| 538 |
|
| 539 |
if cs in math_alphabet_redundant_greek: |
| 540 |
items1.append(wrap_in_alphabet_selector( |
| 541 |
"\\__umi_alternatives_iisafe" + cs + ("" if math_alphabet_redundant_greek[cs].startswith("\\") else " ") + math_alphabet_redundant_greek[cs] |
| 542 |
)) |
| 543 |
else: |
| 544 |
items1.append(wrap_in_alphabet_selector(cs)) |
| 545 |
break |
| 546 |
else: |
| 547 |
items1.append("\\" + csname) |
| 548 |
if csname in ASCII_symbol_synonym: items1+=ASCII_symbol_synonym[csname] |
| 549 |
|
| 550 |
assert items1 |
| 551 |
if is_delimiter and len(items1)>1: |
| 552 |
print("Warning: Synonym for delimiter not supported?", unicode_char, delimiter, items1, file=sys.stderr) |
| 553 |
del items1[1:] |
| 554 |
|
| 555 |
if len(items1)==1: |
| 556 |
a = items1[0] |
| 557 |
b = remove_not(a) |
| 558 |
if b is not None: |
| 559 |
assert not is_delimiter |
| 560 |
print(f"\\__umi_define_char{{{optional_space}{unicode_char}}}{{\__umi_alternatives_not{a}{b}}}") |
| 561 |
else: |
| 562 |
if is_delimiter: |
| 563 |
print(f"\\__umi_define_char_maybe_delimiter{{{optional_space}{unicode_char}}}{{{a}}}") |
| 564 |
else: |
| 565 |
print(f"\\__umi_define_char{{{optional_space}{unicode_char}}}{{{a}}}") |
| 566 |
elif len(items1)==2: |
| 567 |
assert re.fullmatch(r'\\[a-zA-Z]+', items1[0]), items1 |
| 568 |
assert re.fullmatch(r'\\[a-zA-Z]+|[^a-zA-Z]', items1[1]), items1 |
| 569 |
b=remove_not(items1[0]) |
| 570 |
if b is not None: |
| 571 |
d=remove_not(items1[1]) |
| 572 |
assert d is not None, items1 |
| 573 |
print(f"\\__umi_define_char{{{optional_space}{unicode_char}}}{{\\__umi_alternatives_not_two{items1[0]}{items1[1]}{b}{d}}}") |
| 574 |
else: |
| 575 |
print(f"\\__umi_define_char{{{optional_space}{unicode_char}}}{{\\__umi_alternatives{items1[0]}{items1[1]}}}") |
| 576 |
else: |
| 577 |
assert len(items1)>=3, items1 |
| 578 |
assert all(remove_not(x) is None for x in items1), items1 |
| 579 |
assert all(re.fullmatch(r'\\[a-zA-Z]+', c) for c in items1), items1 |
| 580 |
print(f"\\__umi_define_char{{{optional_space}{unicode_char}}}{{\\__umi_alternatives_m{{{''.join(items1)}}}}}") |
| 581 |
|
| 582 |
## |
| 583 |
|
| 584 |
# ======== |
| 585 |
|
| 586 |
sys.exit() |
| 587 |
|
| 588 |
# ======== part below are draft. |
| 589 |
|
| 590 |
default_engine.set_engine(ChildProcessEngine("luatex", env={**os.environ, "hash_extra": "0"}, autorestart=True)) |
| 591 |
execute(r'\documentclass{article}\usepackage{unicode-math}\begin{document}') |
| 592 |
|
| 593 |
execute(r'\documentclass{article}\usepackage{amsmath,amssymb,amsfonts}\begin{document}') |
| 594 |
|
| 595 |
execute(r'\documentclass{article}\usepackage{amsmath}\usepackage{amssymb}\usepackage{amsfonts}\usepackage{tikz}') |
| 596 |
|
| 597 |
|
| 598 |
@functools.lru_cache(maxsize=None) |
| 599 |
def is_defined(csname: str)->bool: |
| 600 |
return T[csname].meaning_str()!="undefined" |
| 601 |
|
| 602 |
|
| 603 |
# show distinct items with math alphabet |
| 604 |
a_=defaultdict(list) |
| 605 |
for l in unicode_math_table.values(): |
| 606 |
for csname in l: |
| 607 |
if csname.startswith(tuple(math_alphabet_translate)): |
| 608 |
t = csname |
| 609 |
for prefix in sorted(math_alphabet_translate, key=len, reverse=True): |
| 610 |
if t.startswith(prefix): |
| 611 |
t=t.removeprefix(prefix) |
| 612 |
break |
| 613 |
a_[t].append(csname) |
| 614 |
a_ |
| 615 |
|
| 616 |
a_.keys() |
| 617 |
|
| 618 |
for v in math_alphabet_csname_translation.values(): |
| 619 |
if v.startswith("\\") and not is_defined(v[1:]): |
| 620 |
print(v) |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
def is_okay(csname: str)->bool: |
| 628 |
if is_defined(csname): return True |
| 629 |
if csname.startswith(tuple(math_alphabet_translate)): return True |
| 630 |
return False |
| 631 |
# |
| 632 |
# print bad ones |
| 633 |
for unicode_char, csnames_ in unicode_math_table.items(): |
| 634 |
if ord(unicode_char) >= 0x80 and all( not is_okay(csname) for csname in csnames_ ): |
| 635 |
print(unicode_char, csnames_) |
| 636 |
|
| 637 |
|
| 638 |
# print okay ones |
| 639 |
for unicode_char, csnames_ in unicode_math_table.items(): |
| 640 |
valid_csnames = [ csname for csname in csnames_ if T[csname].meaning_str()!="undefined" ] |
| 641 |
if ord(unicode_char) >= 0x80 and valid_csnames: |
| 642 |
print(unicode_char, valid_csnames) |
| 643 |
|
| 644 |
T.lsime.meaning_str() |
| 645 |
|
| 646 |
"ℝ".encode('u8') |
| 647 |
|
| 648 |
BalancedTokenList([T.meaning, Catcode.active("\xe2")]).expand_x(engine=pdf_engine) |
| 649 |
|
| 650 |
T["UTFviii@three@octets"].meaning_str(engine=pdf_engine) |
| 651 |
|
| 652 |
T["UTFviii@three@octets@combine"].meaning_str(engine=pdf_engine) |
| 653 |
|
| 654 |
T["UTF@three@octets@noexpand"].meaning_str(engine=pdf_engine) |
| 655 |
|
| 656 |
BalancedTokenList([T.meaning, Catcode.active("\xe2")]).expand_x() |
| 657 |
|
| 658 |
test_engine=ChildProcessEngine("pdftex") |
| 659 |
BalancedTokenList(r"\def\aa{bb}").execute(engine=test_engine) |
| 660 |
BalancedTokenList(r"\csname\noexpand\aa\endcsname").expand_o(engine=test_engine) # give error |
| 661 |
BalancedTokenList(r"\csname\string\aa\endcsname").expand_o(engine=test_engine) # \[\aa] as expected |
| 662 |
|