• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 11712469350

06 Nov 2024 09:48PM UTC coverage: 59.894% (-9.5%) from 69.386%
11712469350

push

github

alerque
chore(nix): Restyle Nix again syncing toward upstream

10884 of 18172 relevant lines covered (59.89%)

3387.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.65
/packages/math/texlike.lua
1
local syms = require("packages.math.unicode-symbols")
9✔
2
local bits = require("core.parserbits")
9✔
3

4
local epnf = require("epnf")
9✔
5
local lpeg = require("lpeg")
9✔
6

7
local atomType = syms.atomType
9✔
8
local symbolDefaults = syms.symbolDefaults
9✔
9
local symbols = syms.symbols
9✔
10

11
-- Grammar to parse TeX-like math
12
-- luacheck: push ignore
13
-- stylua: ignore start
14
---@diagnostic disable: undefined-global, unused-local, lowercase-global
15
local mathGrammar = function (_ENV)
16
   local _ = WS^0
9✔
17
   local eol = S"\r\n"
9✔
18
   local digit = R("09")
9✔
19
   local natural = digit^1 / tostring
9✔
20
   local pos_natural = R("19") * digit^0 / tonumber
9✔
21
   local ctrl_word = R("AZ", "az")^1
9✔
22
   local ctrl_symbol = P(1) - S"{}\\"
9✔
23
   local ctrl_sequence_name = C(ctrl_word + ctrl_symbol) / 1
9✔
24
   local comment = (
25
         P"%" *
9✔
26
         P(1-eol)^0 *
9✔
27
         eol^-1
9✔
28
      )
29
   local utf8cont = R("\128\191")
9✔
30
   local utf8code = lpeg.R("\0\127")
9✔
31
      + lpeg.R("\194\223") * utf8cont
9✔
32
      + lpeg.R("\224\239") * utf8cont * utf8cont
9✔
33
      + lpeg.R("\240\244") * utf8cont * utf8cont * utf8cont
9✔
34
   -- Identifiers inside \mo and \mi tags
35
   local sileID = C(bits.identifier + P(1)) / 1
9✔
36
   local mathMLID = (utf8code - S"\\{}%")^1 / function (...)
9✔
37
         local ret = ""
×
38
         local t = {...}
×
39
         for _,b in ipairs(t) do
×
40
         ret = ret .. b
×
41
         end
42
         return ret
×
43
      end
44
   local group = P"{" * V"mathlist" * (P"}" + E("`}` expected"))
18✔
45
   -- Simple amsmath-like \text command (no embedded math)
46
   local textgroup = P"{" * C((1-P"}")^1) * (P"}" + E("`}` expected"))
18✔
47
   local element_no_infix =
48
      V"def" +
9✔
49
      V"text" + -- Important: before command
9✔
50
      V"command" +
9✔
51
      group +
9✔
52
      V"argument" +
9✔
53
      V"atom"
9✔
54
   local element =
55
      V"supsub" +
9✔
56
      V"subsup" +
9✔
57
      V"sup" +
9✔
58
      V"sub" +
9✔
59
      element_no_infix
9✔
60
   local sep = S",;" * _
9✔
61
   local quotedString = (P'"' * C((1-P'"')^1) * P'"')
9✔
62
   local value = ( quotedString + (1-S",;]")^1 )
9✔
63
   local pair = Cg(sileID * _ * "=" * _ * C(value)) * sep^-1 / function (...)
9✔
64
      local t = {...}; return t[1], t[#t]
422✔
65
   end
66
   local list = Cf(Ct"" * pair^0, rawset)
9✔
67
   local parameters = (
68
         P"[" *
9✔
69
         list *
9✔
70
         P"]"
9✔
71
      )^-1 / function (a)
9✔
72
            return type(a)=="table" and a or {}
728✔
73
         end
74
   local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
9✔
75
      function (t)
76
         t.id = "mathlist"
×
77
         return t
×
78
      end
79
   local dim2_arg =
80
      Cg(P"{" *
18✔
81
         dim2_arg_inner *
9✔
82
         (P"\\\\" * dim2_arg_inner)^1 *
9✔
83
         (P"}" + E("`}` expected"))
18✔
84
         ) / function (...)
×
85
            local t = {...}
×
86
            -- Remove the last mathlist if empty. This way,
87
            -- `inner1 \\ inner2 \\` is the same as `inner1 \\ inner2`.
88
            if not t[#t][1] or not t[#t][1][1] then table.remove(t) end
×
89
            return pl.utils.unpack(t)
×
90
         end
91

92
   local dim2_arg_inner = Ct(V"mathlist" * (P"&" * V"mathlist")^0) /
9✔
93
      function (t)
94
         t.id = "mathlist"
16✔
95
         return t
16✔
96
      end
97
   local dim2_arg =
98
      Cg(P"{" *
18✔
99
         dim2_arg_inner *
9✔
100
         (P"\\\\" * dim2_arg_inner)^1 *
9✔
101
         (P"}" + E("`}` expected"))
18✔
102
         ) / function (...)
×
103
         local t = {...}
4✔
104
         -- Remove the last mathlist if empty. This way,
105
         -- `inner1 \\ inner2 \\` is the same as `inner1 \\ inner2`.
106
         if not t[#t][1] or not t[#t][1][1] then table.remove(t) end
4✔
107
         return pl.utils.unpack(t)
4✔
108
         end
109

110
   START "math"
9✔
111
   math = V"mathlist" * EOF"Unexpected character at end of math code"
18✔
112
   mathlist = (comment + (WS * _) + element)^0
9✔
113
   supsub = element_no_infix * _ * P"^" * _ * element_no_infix * _ *
9✔
114
      P"_" * _ * element_no_infix
9✔
115
   subsup = element_no_infix * _ * P"_" * _ * element_no_infix * _ *
9✔
116
      P"^" * _ * element_no_infix
9✔
117
   sup = element_no_infix * _ * P"^" * _ * element_no_infix
9✔
118
   sub = element_no_infix * _ * P"_" * _ * element_no_infix
9✔
119
   atom = natural + C(utf8code - S"\\{}%^_&") +
9✔
120
      (P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
9✔
121
   text = (
×
122
         P"\\text" *
9✔
123
         Cg(parameters, "options") *
9✔
124
         textgroup
9✔
125
      )
9✔
126
   command = (
×
127
         P"\\" *
9✔
128
         Cg(ctrl_sequence_name, "command") *
9✔
129
         Cg(parameters, "options") *
9✔
130
         (dim2_arg + group^0)
9✔
131
      )
9✔
132
   def = P"\\def" * _ * P"{" *
9✔
133
      Cg(ctrl_sequence_name, "command-name") * P"}" * _ *
9✔
134
      --P"[" * Cg(digit^1, "arity") * P"]" * _ *
135
      P"{" * V"mathlist" * P"}"
9✔
136
   argument = P"#" * Cg(pos_natural, "index")
9✔
137
end
138
-- luacheck: pop
139
-- stylua: ignore end
140
---@diagnostic enable: undefined-global, unused-local, lowercase-global
141

142
local mathParser = epnf.define(mathGrammar)
9✔
143

144
local commands = {}
9✔
145

146
-- A command type is a type for each argument it takes: either string or MathML
147
-- tree. If a command has no type, it is assumed to take only trees.
148
-- Tags like <mi>, <mo>, <mn> take a string, and this needs to be propagated in
149
-- commands that use them.
150

151
local objType = {
9✔
152
   tree = 1,
153
   str = 2,
154
}
155

156
local function inferArgTypes_aux (accumulator, typeRequired, body)
157
   if type(body) == "table" then
2,128✔
158
      if body.id == "argument" then
2,128✔
159
         local ret = accumulator
153✔
160
         table.insert(ret, body.index, typeRequired)
153✔
161
         return ret
153✔
162
      elseif body.id == "command" then
1,975✔
163
         if commands[body.command] then
507✔
164
            local cmdArgTypes = commands[body.command][1]
363✔
165
            if #cmdArgTypes ~= #body then
363✔
166
               SU.error(
×
167
                  "Wrong number of arguments ("
168
                     .. #body
×
169
                     .. ") for command "
×
170
                     .. body.command
×
171
                     .. " (should be "
×
172
                     .. #cmdArgTypes
×
173
                     .. ")"
×
174
               )
175
            else
176
               for i = 1, #cmdArgTypes do
672✔
177
                  accumulator = inferArgTypes_aux(accumulator, cmdArgTypes[i], body[i])
618✔
178
               end
179
            end
180
            return accumulator
363✔
181
         elseif body.command == "mi" or body.command == "mo" or body.command == "mn" then
144✔
182
            if #body ~= 1 then
×
183
               SU.error("Wrong number of arguments (" .. #body .. ") for command " .. body.command .. " (should be 1)")
×
184
            end
185
            accumulator = inferArgTypes_aux(accumulator, objType.str, body[1])
×
186
            return accumulator
×
187
         else
188
            -- Not a macro, recurse on children assuming tree type for all
189
            -- arguments
190
            for _, child in ipairs(body) do
216✔
191
               accumulator = inferArgTypes_aux(accumulator, objType.tree, child)
144✔
192
            end
193
            return accumulator
144✔
194
         end
195
      elseif body.id == "atom" then
1,468✔
196
         return accumulator
607✔
197
      else
198
         -- Simply recurse on children
199
         for _, child in ipairs(body) do
2,128✔
200
            accumulator = inferArgTypes_aux(accumulator, typeRequired, child)
2,534✔
201
         end
202
         return accumulator
861✔
203
      end
204
   else
205
      SU.error("invalid argument to inferArgTypes_aux")
×
206
   end
207
end
208

209
local inferArgTypes = function (body)
210
   return inferArgTypes_aux({}, objType.tree, body)
480✔
211
end
212

213
local function registerCommand (name, argTypes, func)
214
   commands[name] = { argTypes, func }
516✔
215
end
216

217
-- Computes func(func(... func(init, k1, v1), k2, v2)..., k_n, v_n), i.e. applies
218
-- func on every key-value pair in the table. Keys with numeric indices are
219
-- processed in order. This is an important property for MathML compilation below.
220
local function fold_pairs (func, table)
221
   local accumulator = {}
1,541✔
222
   for k, v in pl.utils.kpairs(table) do
12,933✔
223
      accumulator = func(v, k, accumulator)
8,310✔
224
   end
225
   for i, v in ipairs(table) do
3,834✔
226
      accumulator = func(v, i, accumulator)
4,586✔
227
   end
228
   return accumulator
1,541✔
229
end
230

231
local function forall (pred, list)
232
   for _, x in ipairs(list) do
46✔
233
      if not pred(x) then
74✔
234
         return false
37✔
235
      end
236
   end
237
   return true
9✔
238
end
239

240
local compileToStr = function (argEnv, mathlist)
241
   if #mathlist == 1 and mathlist.id == "atom" then
102✔
242
      -- List is a single atom
243
      return mathlist[1]
×
244
   elseif #mathlist == 1 and mathlist[1].id == "argument" then
102✔
245
      return argEnv[mathlist[1].index]
×
246
   elseif mathlist.id == "argument" then
102✔
247
      return argEnv[mathlist.index]
×
248
   else
249
      local ret = ""
102✔
250
      for _, elt in ipairs(mathlist) do
414✔
251
         if elt.id == "atom" then
312✔
252
            ret = ret .. elt[1]
312✔
253
         elseif elt.id == "command" and symbols[elt.command] then
×
254
            ret = ret .. symbols[elt.command]
×
255
         else
256
            SU.error("Encountered non-character token in command that takes a string")
×
257
         end
258
      end
259
      return ret
102✔
260
   end
261
end
262

263
local function isOperatorKind (tree, typeOfAtom, typeOfSymbol)
264
   if not tree then
997✔
265
      return false -- safeguard
×
266
   end
267
   if tree.command ~= "mo" then
997✔
268
      return false
736✔
269
   end
270
   -- Case \mo[atom=big]{ops}
271
   -- E.g. \mo[atom=big]{lim}
272
   if tree.options and tree.options.atom == typeOfAtom then
261✔
273
      return true
4✔
274
   end
275
   -- Case \mo{ops} where ops is registered with the resquested type
276
   -- E.g. \mo{∑) or \sum
277
   if tree[1] and symbolDefaults[tree[1]] and symbolDefaults[tree[1]].atom == typeOfSymbol then
257✔
278
      return true
36✔
279
   end
280
   return false
221✔
281
end
282

283
local function isBigOperator (tree)
284
   return isOperatorKind(tree, "big", atomType.bigOperator)
52✔
285
end
286
local function isCloseOperator (tree)
287
   return isOperatorKind(tree, "close", atomType.closeSymbol)
480✔
288
end
289
local function isOpeningOperator (tree)
290
   return isOperatorKind(tree, "open", atomType.openingSymbol)
465✔
291
end
292

293
local function isAccentSymbol (symbol)
294
   return symbolDefaults[symbol] and symbolDefaults[symbol].atom == atomType.accentSymbol
42✔
295
end
296

297
local function compileToMathML_aux (_, arg_env, tree)
298
   if type(tree) == "string" then
1,884✔
299
      return tree
343✔
300
   end
301
   local function compile_and_insert (child, key, accumulator)
302
      if type(key) ~= "number" then
6,448✔
303
         accumulator[key] = child
4,155✔
304
         return accumulator
4,155✔
305
      -- Compile all children, except if this node is a macro definition (no
306
      -- evaluation "under lambda") or the application of a registered macro
307
      -- (since evaluating the nodes depends on the macro's signature, it is more
308
      -- complex and done below)..
309
      elseif tree.id == "def" or (tree.id == "command" and commands[tree.command]) then
2,293✔
310
         -- Conserve unevaluated child
311
         table.insert(accumulator, child)
615✔
312
      else
313
         -- Compile next child
314
         local comp = compileToMathML_aux(nil, arg_env, child)
1,678✔
315
         if comp then
1,678✔
316
            if comp.id == "wrapper" then
1,198✔
317
               -- Insert all children of the wrapper node
318
               for _, inner_child in ipairs(comp) do
171✔
319
                  table.insert(accumulator, inner_child)
86✔
320
               end
321
            else
322
               table.insert(accumulator, comp)
1,113✔
323
            end
324
         end
325
      end
326
      return accumulator
2,293✔
327
   end
328
   tree = fold_pairs(compile_and_insert, tree)
3,082✔
329
   if tree.id == "math" then
1,541✔
330
      tree.command = "math"
46✔
331
      -- If the outermost `mrow` contains only other `mrow`s, remove it
332
      -- (allowing vertical stacking).
333
      if forall(function (c)
92✔
334
         return c.command == "mrow"
37✔
335
      end, tree[1]) then
92✔
336
         tree[1].command = "math"
9✔
337
         return tree[1]
9✔
338
      end
339
   elseif tree.id == "mathlist" then
1,495✔
340
      -- Turn mathlist into `mrow` except if it has exactly one `mtr` or `mtd`
341
      -- child.
342
      -- Note that `def`s have already been compiled away at this point.
343
      if #tree == 1 then
280✔
344
         if tree[1].command == "mtr" or tree[1].command == "mtd" then
183✔
345
            return tree[1]
×
346
         else
347
            tree.command = "mrow"
183✔
348
         end
349
      else
350
         -- Re-wrap content from opening to closing operator in an implicit mrow,
351
         -- so stretchy operators apply to the correct span of content.
352
         local children = {}
97✔
353
         local stack = {}
97✔
354
         for _, child in ipairs(tree) do
562✔
355
            if isOpeningOperator(child) then
930✔
356
               table.insert(stack, children)
17✔
357
               local mrow = {
17✔
358
                  command = "mrow",
359
                  options = {},
17✔
360
                  child,
17✔
361
               }
362
               table.insert(children, mrow)
17✔
363
               children = mrow
17✔
364
            elseif isCloseOperator(child) then
896✔
365
               table.insert(children, child)
15✔
366
               if #stack > 0 then
15✔
367
                  children = table.remove(stack)
30✔
368
               end
369
            elseif
×
370
               (child.command == "msubsup" or child.command == "msub" or child.command == "msup")
433✔
371
               and isCloseOperator(child[1]) -- child[1] is the base
64✔
372
            then
373
               if #stack > 0 then
×
374
                  -- Special case for closing operator with sub/superscript:
375
                  -- (....)^i must be interpreted as {(....)}^i, not as (...{)}^i
376
                  -- Push the closing operator into the mrow
377
                  table.insert(children, child[1])
×
378
                  -- Move the mrow into the msubsup, replacing the closing operator
379
                  child[1] = children
×
380
                  -- And insert the msubsup into the parent
381
                  children = table.remove(stack)
×
382
                  children[#children] = child
×
383
               else
384
                  table.insert(children, child)
×
385
               end
386
            else
387
               table.insert(children, child)
433✔
388
            end
389
         end
390
         tree = #stack > 0 and stack[1] or children
97✔
391
         tree.command = "mrow"
97✔
392
      end
393
   elseif tree.id == "atom" then
1,215✔
394
      local codepoints = {}
343✔
395
      for _, cp in luautf8.codes(tree[1]) do
698✔
396
         table.insert(codepoints, cp)
355✔
397
      end
398
      local cp = codepoints[1]
343✔
399
      if
400
         #codepoints == 1
343✔
401
         and ( -- If length of UTF-8 string is 1
×
402
            cp >= SU.codepoint("A") and cp <= SU.codepoint("Z")
805✔
403
            or cp >= SU.codepoint("a") and cp <= SU.codepoint("z")
787✔
404
            or cp >= SU.codepoint("Α") and cp <= SU.codepoint("Ω")
517✔
405
            or cp >= SU.codepoint("α") and cp <= SU.codepoint("ω")
517✔
406
         )
407
      then
408
         tree.command = "mi"
106✔
409
      elseif lpeg.match(lpeg.R("09") ^ 1, tree[1]) then
237✔
410
         tree.command = "mn"
100✔
411
      else
412
         tree.command = "mo"
137✔
413
      end
414
      tree.options = {}
343✔
415
   -- Translate TeX-like sub/superscripts to `munderover` or `msubsup`,
416
   -- depending on whether the base is a big operator
417
   elseif tree.id == "sup" and isBigOperator(tree[1]) then
882✔
418
      tree.command = "mover"
×
419
   elseif tree.id == "sub" and isBigOperator(tree[1]) then
900✔
420
      tree.command = "munder"
×
421
   elseif tree.id == "subsup" and isBigOperator(tree[1]) then
886✔
422
      tree.command = "munderover"
8✔
423
   elseif tree.id == "supsub" and isBigOperator(tree[1]) then
864✔
424
      tree.command = "munderover"
×
425
      local tmp = tree[2]
×
426
      tree[2] = tree[3]
×
427
      tree[3] = tmp
×
428
   elseif tree.id == "sup" then
864✔
429
      tree.command = "msup"
10✔
430
   elseif tree.id == "sub" then
854✔
431
      tree.command = "msub"
28✔
432
   elseif tree.id == "subsup" then
826✔
433
      tree.command = "msubsup"
6✔
434
   elseif tree.id == "supsub" then
820✔
435
      tree.command = "msubsup"
×
436
      local tmp = tree[2]
×
437
      tree[2] = tree[3]
×
438
      tree[3] = tmp
×
439
   elseif tree.id == "def" then
820✔
440
      local commandName = tree["command-name"]
480✔
441
      local argTypes = inferArgTypes(tree[1])
480✔
442
      registerCommand(commandName, argTypes, function (compiledArgs)
960✔
443
         return compileToMathML_aux(nil, compiledArgs, tree[1])
85✔
444
      end)
445
      return nil
480✔
446
   elseif tree.id == "text" then
340✔
447
      tree.command = "mtext"
×
448
   elseif tree.id == "command" and commands[tree.command] then
340✔
449
      local argTypes = commands[tree.command][1]
188✔
450
      local cmdFun = commands[tree.command][2]
188✔
451
      local applicationTree = tree
188✔
452
      local cmdName = tree.command
188✔
453
      if #applicationTree ~= #argTypes then
188✔
454
         SU.error(
×
455
            "Wrong number of arguments ("
456
               .. #applicationTree
×
457
               .. ") for command "
×
458
               .. cmdName
×
459
               .. " (should be "
×
460
               .. #argTypes
×
461
               .. ")"
×
462
         )
463
      end
464
      -- Compile every argument
465
      local compiledArgs = {}
188✔
466
      for i, arg in pairs(applicationTree) do
1,075✔
467
         if type(i) == "number" then
887✔
468
            if argTypes[i] == objType.tree then
135✔
469
               table.insert(compiledArgs, compileToMathML_aux(nil, arg_env, arg))
66✔
470
            else
471
               local x = compileToStr(arg_env, arg)
102✔
472
               table.insert(compiledArgs, x)
102✔
473
            end
474
         else
475
            -- Not an argument but an attribute. Add it to the compiled
476
            -- argument tree as-is
477
            compiledArgs[i] = applicationTree[i]
752✔
478
         end
479
      end
480
      local res = cmdFun(compiledArgs)
188✔
481
      if res.command == "mrow" then
188✔
482
         -- Mark the outer mrow to be unwrapped in the parent
483
         res.id = "wrapper"
85✔
484
      end
485
      return res
188✔
486
   elseif tree.id == "command" and symbols[tree.command] then
152✔
487
      local atom = { id = "atom", [1] = symbols[tree.command] }
42✔
488
      if isAccentSymbol(symbols[tree.command]) and #tree > 0 then
84✔
489
         -- LaTeX-style accents \vec{v} = <mover accent="true"><mi>v</mi><mo>→</mo></mover>
490
         local accent = {
×
491
            id = "command",
492
            command = "mover",
493
            options = {
×
494
               accent = "true",
495
            },
496
         }
497
         accent[1] = compileToMathML_aux(nil, arg_env, tree[1])
×
498
         accent[2] = compileToMathML_aux(nil, arg_env, atom)
×
499
         tree = accent
×
500
      elseif #tree > 0 then
42✔
501
         -- Play cool with LaTeX-style commands that don't take arguments:
502
         -- Edge case for non-accent symbols so we don't loose bracketed groups
503
         -- that might have been seen as command arguments.
504
         -- Ex. \langle{x}\rangle (without space after \langle)
505
         local sym = compileToMathML_aux(nil, arg_env, atom)
×
506
         -- Compile all children in-place
507
         for i, child in ipairs(tree) do
×
508
            tree[i] = compileToMathML_aux(nil, arg_env, child)
×
509
         end
510
         -- Insert symbol at the beginning,
511
         -- And add a wrapper mrow to be unwrapped in the parent.
512
         table.insert(tree, 1, sym)
×
513
         tree.command = "mrow"
×
514
         tree.id = "wrapper"
×
515
      else
516
         tree = compileToMathML_aux(nil, arg_env, atom)
84✔
517
      end
518
   elseif tree.id == "argument" then
110✔
519
      if arg_env[tree.index] then
33✔
520
         return arg_env[tree.index]
33✔
521
      else
522
         SU.error("Argument #" .. tree.index .. " has escaped its scope (probably not fully applied command).")
×
523
      end
524
   end
525
   tree.id = nil
831✔
526
   return tree
831✔
527
end
528

529
local function printMathML (tree)
530
   if type(tree) == "string" then
×
531
      return tree
×
532
   end
533
   local result = "\\" .. tree.command
×
534
   if tree.options then
×
535
      local options = {}
×
536
      for k, v in pairs(tree.options) do
×
537
         table.insert(options, k .. "=" .. tostring(v))
×
538
      end
539
      if #options > 0 then
×
540
         result = result .. "[" .. table.concat(options, ", ") .. "]"
×
541
      end
542
   end
543
   if #tree > 0 then
×
544
      result = result .. "{"
×
545
      for _, child in ipairs(tree) do
×
546
         result = result .. printMathML(child)
×
547
      end
548
      result = result .. "}"
×
549
   end
550
   return result
×
551
end
552

553
local function compileToMathML (_, arg_env, tree)
554
   local result = compileToMathML_aux(_, arg_env, tree)
46✔
555
   SU.debug("texmath", function ()
92✔
556
      return "Resulting MathML: " .. printMathML(result)
×
557
   end)
558
   return result
46✔
559
end
560

561
local function convertTexlike (_, content)
562
   local ret = epnf.parsestring(mathParser, content[1])
46✔
563
   SU.debug("texmath", function ()
92✔
564
      return "Parsed TeX math: " .. pl.pretty.write(ret)
×
565
   end)
566
   return ret
46✔
567
end
568

569
registerCommand("%", {}, function ()
18✔
570
   return { "%", command = "mo", options = {} }
1✔
571
end)
572
registerCommand("mi", { [1] = objType.str }, function (x)
18✔
573
   return x
87✔
574
end)
575
registerCommand("mo", { [1] = objType.str }, function (x)
18✔
576
   return x
15✔
577
end)
578
registerCommand("mn", { [1] = objType.str }, function (x)
18✔
579
   return x
×
580
end)
581

582
compileToMathML(
18✔
583
   nil,
9✔
584
   {},
585
   convertTexlike(nil, {
9✔
586
      [==[
×
587
  \def{frac}{\mfrac{#1}{#2}}
588
  \def{sqrt}{\msqrt{#1}}
589
  \def{bi}{\mi[mathvariant=bold-italic]{#1}}
590
  \def{dsi}{\mi[mathvariant=double-struck]{#1}}
591

592
  \def{lim}{\mo[atom=big]{lim}}
593

594
  % From amsmath:
595
  \def{to}{\mo[atom=bin]{→}}
596
  \def{gcd}{\mo[atom=big]{gcd}}
597
  \def{sup}{\mo[atom=big]{sup}}
598
  \def{inf}{\mo[atom=big]{inf}}
599
  \def{max}{\mo[atom=big]{max}}
600
  \def{min}{\mo[atom=big]{min}}
601
  % Those use U+202F NARROW NO-BREAK SPACE in their names
602
  \def{limsup}{\mo[atom=big]{lim sup}}
603
  \def{liminf}{\mo[atom=big]{lim inf}}
604
  \def{projlim}{\mo[atom=big]{proj lim}}
605
  \def{injlim}{\mo[atom=big]{inj lim}}
606

607
  % Standard spaces gleaned from plain TeX
608
  \def{thinspace}{\mspace[width=thin]}
609
  \def{negthinspace}{\mspace[width=-thin]}
610
  \def{,}{\thinspace}
611
  \def{!}{\negthinspace}
612
  \def{medspace}{\mspace[width=med]}
613
  \def{negmedspace}{\mspace[width=-med]}
614
  \def{>}{\medspace}
615
  \def{thickspace}{\mspace[width=thick]}
616
  \def{negthickspace}{\mspace[width=-thick]}
617
  \def{;}{\thickspace}
618
  \def{enspace}{\mspace[width=1en]}
619
  \def{enskip}{\enspace}
620
  \def{quad}{\mspace[width=1em]}
621
  \def{qquad}{\mspace[width=2em]}
622

623
  % MathML says a single-character identifier must be in italic by default.
624
  % TeX however has the following Greek capital macros rendered in upright shape.
625
  % It so common that you've probably never seen Γ(x) written with an italic gamma.
626
  \def{Gamma}{\mi[mathvariant=normal]{Γ}}
627
  \def{Delta}{\mi[mathvariant=normal]{Δ}}
628
  \def{Theta}{\mi[mathvariant=normal]{Θ}}
629
  \def{Lambda}{\mi[mathvariant=normal]{Λ}}
630
  \def{Xi}{\mi[mathvariant=normal]{Ξ}}
631
  \def{Pi}{\mi[mathvariant=normal]{Π}}
632
  \def{Sigma}{\mi[mathvariant=normal]{Σ}}
633
  \def{Upsilon}{\mi[mathvariant=normal]{Υ}}
634
  \def{Phi}{\mi[mathvariant=normal]{Φ}}
635
  \def{Psi}{\mi[mathvariant=normal]{Ψ}}
636
  \def{Omega}{\mi[mathvariant=normal]{Ω}}
637
  % Some calligraphic (script), fraktur, double-struck styles:
638
  % Convenience for compatibility with LaTeX.
639
  \def{mathcal}{\mi[mathvariant=script]{#1}}
640
  \def{mathfrak}{\mi[mathvariant=fraktur]{#1}}
641
  \def{mathbb}{\mi[mathvariant=double-struck]{#1}}
642
  % Some style-switching commands for compatibility with LaTeX math.
643
  % Caveat emptor: LaTeX would allow these to apply to a whole formula.
644
  % We can't do that in MathML, as mathvariant applies to token elements only.
645
  % Also note that LaTeX and related packages may have many more such commands.
646
  % We only provide a few common ('historical') ones here.
647
  \def{mathrm}{\mi[mathvariant=normal]{#1}}
648
  \def{mathbf}{\mi[mathvariant=bold]{#1}}
649
  \def{mathit}{\mi[mathvariant=italic]{#1}}
650
  \def{mathsf}{\mi[mathvariant=sans-serif]{#1}}
651
  \def{mathtt}{\mi[mathvariant=monospace]{#1}}
652

653
  % Modulus operator forms
654
  \def{bmod}{\mo{mod}}
655
  \def{pmod}{\quad(\mo{mod} #1)}
656

657
  % Phantom commands from TeX/LaTeX
658
  \def{phantom}{\mphantom{#1}}
659
  \def{hphantom}{\mpadded[height=0, depth=0]{\mphantom{#1}}}
660
  \def{vphantom}{\mpadded[width=0]{\mphantom{#1}}}
661
  %\mphantom[special=v]{#1}}}
662
]==],
663
   })
664
)
665

666
return { convertTexlike, compileToMathML }
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc