mathml/luamml-amsmath.lua

177 lines
6.5 KiB
Lua
Raw Normal View History

2021-04-24 16:53:50 +02:00
local write_xml = require'luamml-xmlwriter'
local make_root = require'luamml-convert'.make_root
2021-04-25 18:09:13 +02:00
local save_result = require'luamml-tex'.save_result
local store_column = require'luamml-table'.store_column
local store_tag = require'luamml-table'.store_tag
2025-02-08 11:40:49 +01:00
local store_notag = require'luamml-table'.store_notag
local get_table = require'luamml-table'.get_table
local set_row_attribute = require'luamml-table'.set_row_attribute
2021-04-30 19:52:32 +02:00
local to_text = require'luamml-lr'
2021-04-24 16:53:50 +02:00
local properties = node.get_properties_table()
2021-06-18 23:03:39 +02:00
local math_t = node.id'math'
local funcid = luatexbase.new_luafunction'__luamml_amsmath_add_last_to_row:'
token.set_lua('__luamml_amsmath_add_last_to_row:', funcid, 'protected')
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
-- local box = token.scan_int()
local nest = tex.nest.top
local head, startmath = nest.head, nest.tail
repeat
startmath = startmath.prev
until startmath == head or (startmath.id == math_t and startmath.subtype == 0)
if startmath == head then return end
assert(startmath.id == node.id"math")
2021-06-22 17:44:18 +02:00
store_column(startmath)
2021-06-18 23:03:39 +02:00
end
2021-04-27 17:33:22 +02:00
local funcid = luatexbase.new_luafunction'__luamml_amsmath_add_box_to_row:'
token.set_lua('__luamml_amsmath_add_box_to_row:', funcid, 'protected')
2021-04-24 16:53:50 +02:00
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
-- local box = token.scan_int()
local boxnum = 0
local startmath = tex.box[boxnum].list
2021-06-18 23:03:39 +02:00
assert(startmath.id == math_t)
2021-06-22 17:44:18 +02:00
store_column(startmath)
2021-04-24 16:53:50 +02:00
end
local funcid = luatexbase.new_luafunction'__luamml_amsmath_set_row_columnalign:n'
token.set_lua('__luamml_amsmath_set_row_columnalign:n', funcid, 'protected')
lua.get_functions_table()[funcid] = function()
set_row_attribute('columnalign', token.scan_argument())
end
2025-02-26 17:02:11 +01:00
-- This function is used to add a intent :continued-row to
2025-02-23 18:16:53 +01:00
-- rows of a split environment.
-- we assume that the table is a mtable with mrow with mtd.
-- we check row 2..n. If the first cell has only one element and
-- for this element 'tex:ignore' has been set, we assume a continued row and
-- set the intent on the mrow.
local function add_intent_continued_row (table)
for index,rowtable in ipairs(table) do
if table[index][1] and table[index][1][1] then -- just for safety ...
if index > 1 and #table[index][1]==1 and table[index][1][1]['tex:ignore'] then
table[index]['intent']=':continued-row'
end
end
end
end
2025-02-26 17:02:11 +01:00
-- debug function for tables
-- activate with \directlua{debugmtable=2} or \directlua{debugmtable='split'}
local function debug_mtable (mtable,kind)
if debugmtable and (debugmtable==2) or (debugmtable==kind) then
texio.write_nl('==============')
texio.write_nl(kind)
texio.write_nl(table.serialize(mtable))
texio.write_nl('==============')
end
end
2021-06-18 23:03:39 +02:00
do
local saved
2021-06-23 04:39:58 +02:00
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_inner_table:n'
token.set_lua('__luamml_amsmath_save_inner_table:n', funcid)
2021-06-18 23:03:39 +02:00
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
2021-06-23 04:39:58 +02:00
local kind = token.scan_argument()
2025-02-23 19:58:39 +01:00
kind = kind:gsub("*","")
2021-06-18 23:03:39 +02:00
local mml_table = get_table()
if not mml_table then return end
mml_table.displaystyle = true
2025-02-23 19:58:39 +01:00
mml_table.class=kind
2025-02-23 18:16:53 +01:00
if kind=="split" then
add_intent_continued_row (mml_table)
end
2021-06-18 23:03:39 +02:00
local columns = node.count(node.id'align_record', tex.lists.align_head)//2
2021-06-23 04:39:58 +02:00
mml_table.columnalign = kind == 'gathered' and 'center' or string.rep('right left', columns, ' ')
2021-06-18 23:03:39 +02:00
local spacing = {}
for n in node.traverse_id(node.id'glue', tex.lists.align_head) do
2021-06-25 04:21:21 +02:00
spacing[#spacing+1] = n.width == 0 and '0' or string.format('%.3fpt', n.width/65781.76)
2021-06-18 23:03:39 +02:00
end
2021-06-23 04:39:58 +02:00
mml_table.columnspacing = #spacing > 3 and table.concat(spacing, ' ', 2, #spacing-2) or nil
2025-02-26 17:02:11 +01:00
debug_mtable(mml_table,kind)
2021-06-18 23:03:39 +02:00
saved = mml_table
end
2021-06-25 04:21:48 +02:00
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_smallmatrix:'
token.set_lua('__luamml_amsmath_save_smallmatrix:', funcid)
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
local mml_table = get_table()
mml_table.align = 'axis'
2025-02-22 16:55:05 +01:00
mml_table.class='smallmatrix'
2021-06-25 04:21:48 +02:00
mml_table.columnalign = 'center'
mml_table.columnspacing = '0.278em'
mml_table.rowspacing = string.format('%.3fpt', tex.lineskip.width/65781.76)
saved = {[0] = 'mpadded', width = '+0.333em', lspace = '0.167em', mml_table}
2025-02-26 17:02:11 +01:00
debug_mtable(mml_table,kind)
2021-06-25 04:21:48 +02:00
saved = mml_table
end
2021-06-18 23:03:39 +02:00
funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_inner_table:'
token.set_lua('__luamml_amsmath_finalize_inner_table:', funcid)
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
local vcenter = tex.nest.top.tail.nucleus
local props = properties[vcenter]
if not props then
props = {}
properties[vcenter] = props
end
props.mathml_table = assert(saved)
saved = nil
end
end
2021-06-23 04:39:58 +02:00
funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_table:n'
token.set_lua('__luamml_amsmath_finalize_table:n', funcid)
2021-04-24 16:53:50 +02:00
lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc
2021-06-23 04:39:58 +02:00
local kind = token.scan_argument()
2025-02-23 19:58:39 +01:00
kind = kind:gsub("*","")
local mml_table = get_table()
2021-04-24 16:53:50 +02:00
if not mml_table then return end
mml_table.displaystyle = true
2025-02-23 19:58:39 +01:00
mml_table.class=kind
-- this should perhaps be configurable and extendable
if kind == 'align' or 'alignat' or 'flalign' or 'xalignat' or 'xxalignat' then
2025-02-23 20:41:26 +00:00
mml_table.intent=":system-of-equations"
2025-02-23 19:58:39 +01:00
end
2021-04-25 18:09:13 +02:00
local columns = node.count(node.id'align_record', tex.lists.align_head)//2
2025-02-22 16:55:05 +01:00
mml_table.columnalign = kind == 'align' and 'left '..string.rep('right left', columns, ' ') or nil
mml_table.width = kind == 'multline' and '100%' or nil
2021-06-23 14:35:43 +02:00
-- mml_table.side = kind == 'multline' and 'rightoverlap' or nil
2021-04-27 03:30:08 +02:00
local spacing = {}
for n in node.traverse_id(node.id'glue', tex.lists.align_head) do
spacing[#spacing+1] = n.width == 0 and '0' or '.8em'
end
2021-06-23 04:39:58 +02:00
mml_table.columnspacing = #spacing > 3 and table.concat(spacing, ' ', 2, #spacing-2) or nil
2025-02-26 17:02:11 +01:00
debug_mtable(mml_table,kind)
2021-05-02 02:58:20 +02:00
save_result(mml_table, true)
2021-04-24 16:53:50 +02:00
end
2021-04-27 17:33:22 +02:00
local last_tag
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_tag:'
token.set_lua('__luamml_amsmath_save_tag:', funcid, 'protected')
2021-04-24 16:53:50 +02:00
lua.get_functions_table()[funcid] = function()
2021-04-27 17:33:22 +02:00
local nest = tex.nest.top
local chars = {}
2021-05-01 08:03:21 +02:00
last_tag = to_text(nest.head)
2021-04-27 17:33:22 +02:00
end
funcid = luatexbase.new_luafunction'__luamml_amsmath_set_tag:'
token.set_lua('__luamml_amsmath_set_tag:', funcid, 'protected')
lua.get_functions_table()[funcid] = function()
if not last_tag then
2025-02-08 11:40:49 +01:00
store_notag({[0] = 'mtd',''})
else
store_tag({[0] = 'mtd', last_tag})
last_tag = nil
2021-04-27 17:33:22 +02:00
end
2021-04-24 16:53:50 +02:00
end