| 1 |
--[[ |
| 2 |
This file is loaded by luamml-patches-amsmath.sty |
| 3 |
|
| 4 |
It defines the luafunctions |
| 5 |
* \__luamml_amsmath_add_last_to_row: |
| 6 |
* \__luamml_amsmath_add_box_to_row: |
| 7 |
* \__luamml_amsmath_set_row_columnalign:n |
| 8 |
* \__luamml_amsmath_save_inner_table:n |
| 9 |
* \__luamml_amsmath_save_smallmatrix: |
| 10 |
* \__luamml_amsmath_finalize_inner_table: |
| 11 |
* \__luamml_amsmath_save_inner_table:n |
| 12 |
* \__luamml_amsmath_save_smallmatrix: |
| 13 |
* \__luamml_amsmath_finalize_inner_table: |
| 14 |
* \__luamml_amsmath_save_tag_with_struct_elem:N |
| 15 |
* \__luamml_amsmath_save_tag: |
| 16 |
* \__luamml_amsmath_set_tag: |
| 17 |
|
| 18 |
These are all rather special commands used in amsmath environments to |
| 19 |
get the correct math tagging. |
| 20 |
|
| 21 |
TODO: error handling and more documentation |
| 22 |
--]] |
| 23 |
|
| 24 |
-- TODO these two are unused? |
| 25 |
local write_xml = require'luamml-xmlwriter' |
| 26 |
local make_root = require'luamml-convert'.make_root |
| 27 |
|
| 28 |
local save_result = require'luamml-tex'.save_result |
| 29 |
local store_row = require'luamml-table'.store_row |
| 30 |
local store_column = require'luamml-table'.store_column |
| 31 |
local store_tag = require'luamml-table'.store_tag |
| 32 |
local store_notag = require'luamml-table'.store_notag |
| 33 |
local get_table = require'luamml-table'.get_table |
| 34 |
local set_row_attribute = require'luamml-table'.set_row_attribute |
| 35 |
local to_text = require'luamml-lr' |
| 36 |
|
| 37 |
local properties = node.get_properties_table() |
| 38 |
|
| 39 |
local math_t = node.id'math' |
| 40 |
|
| 41 |
local funcid = luatexbase.new_luafunction'__luamml_amsmath_add_last_to_row:' |
| 42 |
token.set_lua('__luamml_amsmath_add_last_to_row:', funcid, 'protected') |
| 43 |
lua.get_functions_table()[funcid] = function() |
| 44 |
-- TODO: Error handling etc |
| 45 |
-- local box = token.scan_int() |
| 46 |
local nest = tex.nest.top |
| 47 |
local head, startmath = nest.head, nest.tail |
| 48 |
repeat |
| 49 |
startmath = startmath.prev |
| 50 |
until startmath == head or (startmath.id == math_t and startmath.subtype == 0) |
| 51 |
if startmath == head then return end |
| 52 |
assert(startmath.id == node.id"math") |
| 53 |
store_column(startmath) |
| 54 |
end |
| 55 |
|
| 56 |
local funcid = luatexbase.new_luafunction'__luamml_amsmath_add_box_to_row:' |
| 57 |
token.set_lua('__luamml_amsmath_add_box_to_row:', funcid, 'protected') |
| 58 |
lua.get_functions_table()[funcid] = function() |
| 59 |
-- TODO: Error handling etc |
| 60 |
-- local box = token.scan_int() |
| 61 |
local boxnum = 0 |
| 62 |
local startmath = tex.box[boxnum].list |
| 63 |
assert(startmath.id == math_t) |
| 64 |
store_column(startmath) |
| 65 |
end |
| 66 |
|
| 67 |
local funcid = luatexbase.new_luafunction'__luamml_amsmath_set_row_columnalign:n' |
| 68 |
token.set_lua('__luamml_amsmath_set_row_columnalign:n', funcid, 'protected') |
| 69 |
lua.get_functions_table()[funcid] = function() |
| 70 |
set_row_attribute('columnalign', token.scan_argument()) |
| 71 |
end |
| 72 |
|
| 73 |
--[[ |
| 74 |
This function is used to add a intent :continued-row to |
| 75 |
rows of a split environment. |
| 76 |
we assume that the table is a mtable with mrow with mtd. |
| 77 |
we check row 2..n. If the first cell is empty, we assume a continued row and |
| 78 |
set the intent on the mrow. |
| 79 |
|
| 80 |
The function is used below in \__luamml_amsmath_save_inner_table:n |
| 81 |
--]] |
| 82 |
local function add_intent_continued_row(table) |
| 83 |
for index, row in ipairs(table) do |
| 84 |
if index > 1 and row[1] and #row[1] == 0 then |
| 85 |
row.intent = ':continued-row' |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
|
| 90 |
--[[ |
| 91 |
This function adds an intent =":pause-medium" on every second mtd in a table |
| 92 |
currently it is also on the first (after the label) but this could be changed |
| 93 |
used in \__luamml_amsmath_finalize_table:n for |
| 94 |
'align' or 'alignat' or 'flalign' or 'xalignat' or 'xxalignat' |
| 95 |
--]] |
| 96 |
|
| 97 |
local function add_intent_pause(mmltable) |
| 98 |
for _, row in ipairs(mmltable) do |
| 99 |
for colindex, col in ipairs(row) do |
| 100 |
if colindex % 2 == 0 then |
| 101 |
col.intent = ':pause-medium' |
| 102 |
end |
| 103 |
end |
| 104 |
end |
| 105 |
end |
| 106 |
|
| 107 |
--[[ |
| 108 |
debug function for tables |
| 109 |
activate with \directlua{debugmtable=2} or \directlua{debugmtable='split'} |
| 110 |
change 2025-05-26: fixed logic if kind doesn't exist. |
| 111 |
--]] |
| 112 |
|
| 113 |
local function debug_mtable(mtable, kind) |
| 114 |
if debugmtable == 2 or debugmtable == kind then |
| 115 |
texio.write_nl"==============" |
| 116 |
texio.write_nl(kind or "?kind?") |
| 117 |
texio.write_nl(table.serialize(mtable)) |
| 118 |
texio.write_nl"==============" |
| 119 |
end |
| 120 |
end |
| 121 |
|
| 122 |
do |
| 123 |
local saved |
| 124 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_inner_table:n' |
| 125 |
token.set_lua('__luamml_amsmath_save_inner_table:n', funcid) |
| 126 |
lua.get_functions_table()[funcid] = function() |
| 127 |
-- TODO: Error handling etc |
| 128 |
local kind = token.scan_argument() |
| 129 |
kind = kind:gsub('*', '') |
| 130 |
local mml_table = get_table() |
| 131 |
if not mml_table then return end |
| 132 |
mml_table.displaystyle = true |
| 133 |
mml_table.class = kind |
| 134 |
if kind == 'split' then |
| 135 |
add_intent_continued_row(mml_table) |
| 136 |
end |
| 137 |
local columns = node.count(node.id'align_record', tex.lists.align_head) // 2 |
| 138 |
mml_table.columnalign = kind == 'gathered' and 'center' or string.rep('right left', columns, ' ') |
| 139 |
local spacing = {} |
| 140 |
for n in node.traverse_id(node.id 'glue', tex.lists.align_head) do |
| 141 |
spacing[#spacing + 1] = n.width == 0 and '0' or string.format('%.3fpt', n.width / 65781.76) |
| 142 |
end |
| 143 |
mml_table.columnspacing = #spacing > 3 and table.concat(spacing, ' ', 2, #spacing - 2) or nil |
| 144 |
debug_mtable(mml_table, kind) |
| 145 |
saved = mml_table |
| 146 |
end |
| 147 |
|
| 148 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_smallmatrix:' |
| 149 |
token.set_lua('__luamml_amsmath_save_smallmatrix:', funcid) |
| 150 |
lua.get_functions_table()[funcid] = function() |
| 151 |
-- TODO: Error handling etc |
| 152 |
local mml_table = get_table() |
| 153 |
mml_table.align = 'axis' |
| 154 |
mml_table.class = 'smallmatrix' |
| 155 |
mml_table.columnalign = 'center' |
| 156 |
mml_table.columnspacing = '0.278em' |
| 157 |
mml_table.rowspacing = string.format('%.3fpt', tex.lineskip.width / 65781.76) |
| 158 |
saved = {[0] = 'mrow', |
| 159 |
{[0] = 'mspace', width = '0.167em'}, |
| 160 |
mml_table, |
| 161 |
{[0] = 'mspace', width = '0.167em'}, |
| 162 |
} |
| 163 |
debug_mtable(mml_table, kind) |
| 164 |
saved = mml_table |
| 165 |
end |
| 166 |
|
| 167 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_inner_table:' |
| 168 |
token.set_lua('__luamml_amsmath_finalize_inner_table:', funcid) |
| 169 |
lua.get_functions_table()[funcid] = function() |
| 170 |
-- TODO: Error handling etc |
| 171 |
local vcenter = tex.nest.top.tail.nucleus |
| 172 |
local props = properties[vcenter] |
| 173 |
if not props then |
| 174 |
props = {} |
| 175 |
properties[vcenter] = props |
| 176 |
end |
| 177 |
props.mathml_table = assert(saved) |
| 178 |
saved = nil |
| 179 |
end |
| 180 |
end |
| 181 |
|
| 182 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_finalize_table:n' |
| 183 |
token.set_lua('__luamml_amsmath_finalize_table:n', funcid) |
| 184 |
lua.get_functions_table()[funcid] = function() |
| 185 |
-- TODO: Error handling etc |
| 186 |
local kind = token.scan_argument() |
| 187 |
kind = kind:gsub('*', '') |
| 188 |
local mml_table = get_table() |
| 189 |
if not mml_table then return end |
| 190 |
mml_table.displaystyle = true |
| 191 |
mml_table.class = kind |
| 192 |
-- this should perhaps be configurable and extendable |
| 193 |
if kind == 'align' or 'alignat' or 'flalign' or 'xalignat' or 'xxalignat' then |
| 194 |
mml_table.intent = ':system-of-equations' |
| 195 |
add_intent_pause(mml_table) |
| 196 |
end |
| 197 |
local columns = node.count(node.id'align_record', tex.lists.align_head) // 2 |
| 198 |
mml_table.columnalign = kind == 'align' and 'left ' .. string.rep('right left', columns, ' ') or nil |
| 199 |
mml_table.width = kind == 'multline' and '100%' or nil |
| 200 |
-- mml_table.side = kind == 'multline' and 'rightoverlap' or nil |
| 201 |
local spacing = {} |
| 202 |
for n in node.traverse_id(node.id'glue', tex.lists.align_head) do |
| 203 |
spacing[#spacing + 1] = n.width == 0 and '0' or '.8em' |
| 204 |
end |
| 205 |
mml_table.columnspacing = #spacing > 3 and '0 ' .. table.concat(spacing, ' ', 2, #spacing - 2) or nil |
| 206 |
debug_mtable(mml_table, kind) |
| 207 |
save_result(mml_table, true) |
| 208 |
end |
| 209 |
|
| 210 |
local last_tag |
| 211 |
|
| 212 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_tag:' |
| 213 |
token.set_lua('__luamml_amsmath_save_tag:', funcid, 'protected') |
| 214 |
lua.get_functions_table()[funcid] = function() |
| 215 |
local nest = tex.nest.top |
| 216 |
last_tag = to_text(nest.head) |
| 217 |
end |
| 218 |
|
| 219 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_tag_with_struct_elem:N' |
| 220 |
token.set_lua('__luamml_amsmath_save_tag_with_struct_elem:N', funcid, 'protected') |
| 221 |
lua.get_functions_table()[funcid] = function() |
| 222 |
local struct_num = token.scan_int() |
| 223 |
local nest = tex.nest.top |
| 224 |
last_tag = to_text(nest.head) |
| 225 |
last_tag[':structnum'] = struct_num |
| 226 |
end |
| 227 |
|
| 228 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_set_tag:' |
| 229 |
token.set_lua('__luamml_amsmath_set_tag:', funcid, 'protected') |
| 230 |
lua.get_functions_table()[funcid] = function() |
| 231 |
if not last_tag then |
| 232 |
store_notag{ [0] = 'mtd', '' } |
| 233 |
else |
| 234 |
store_tag{ [0] = 'mtd', last_tag } |
| 235 |
last_tag = nil |
| 236 |
end |
| 237 |
end |
| 238 |
|
| 239 |
funcid = luatexbase.new_luafunction'__luamml_amsmath_save_intertext_with_struct_elem:N' |
| 240 |
token.set_lua('__luamml_amsmath_save_intertext_with_struct_elem:N', funcid, 'protected') |
| 241 |
lua.get_functions_table()[funcid] = function() |
| 242 |
local struct_num = token.scan_int() |
| 243 |
local content = to_text(tex.nest.top.head) |
| 244 |
content[':structnum'] = struct_num |
| 245 |
store_row { [0] = 'mtr', { [0] = 'mtd', colspan = tex.count['maxfields@'] + 1, content } } |
| 246 |
end |
| 247 |
|
| 248 |
require'luamml-tex'.set_extract_eqno(function() |
| 249 |
if not last_tag then return end |
| 250 |
local tag = last_tag |
| 251 |
last_tag = nil |
| 252 |
return function(mml, _core) |
| 253 |
if mml and mml[0] == 'mrow' then |
| 254 |
mml[0], mml.intent = 'mtd', mml.intent or ':pause-medium' |
| 255 |
else |
| 256 |
mml = { [0] = 'mtd', intent = ':pause-medium', mml } |
| 257 |
end |
| 258 |
mml = { [0] = 'mtable', |
| 259 |
displaystyle = 'true', |
| 260 |
intent = ':system-of-equations', |
| 261 |
{ [0] = 'mtr', |
| 262 |
{ [0] = 'mtd', intent = ':equation-label', tag }, |
| 263 |
mml, |
| 264 |
}, |
| 265 |
} |
| 266 |
return mml |
| 267 |
end |
| 268 |
end) |