diff --git a/luamml-amsmath.lua b/luamml-amsmath.lua index bf516b8..340ede4 100644 --- a/luamml-amsmath.lua +++ b/luamml-amsmath.lua @@ -52,7 +52,6 @@ lua.get_functions_table()[funcid] = function() chars[#chars+1] = 0x20 elseif id == node.id'kern' then else - print(n) texio.write_nl'WARNING: Unsupported node in tag dropped' end end diff --git a/luamml-array.lua b/luamml-array.lua new file mode 100644 index 0000000..98b96bc --- /dev/null +++ b/luamml-array.lua @@ -0,0 +1,87 @@ +local write_xml = require'luamml-xmlwriter' +local make_root = require'luamml-convert'.make_root +local save_result = require'luamml-tex'.save_result +local store_column = require'luamml-table'.store_column +local store_tag = require'luamml-table'.store_tag +local get_table = require'luamml-table'.get_table + +local properties = node.get_properties_table() + +local funcid = luatexbase.new_luafunction'__luamml_array_add_list_to_row:' +token.set_lua('__luamml_array_add_list_to_row:', funcid, 'protected') +lua.get_functions_table()[funcid] = function() + -- TODO: Error handling etc + -- local box = token.scan_int() + local startmath + local preskip + local postskip + local prestretch = 0 -- Not one since overflowing content protrudes right + local stretch = {0, 0, 0, 0, 0} + local n = tex.nest.top.head.next + local func, ctx, n = node.traverse(tex.nest.top.head.next) + while true do + local id, sub n, id, sub = func(ctx, n) + if not n then break end + if node.id'math' == id then + if sub == 0 then + if startmath then + texio.write_nl'Multiple formulas detected in array field' + end + startmath = n + for i=2, 5 do + if stretch[i] ~= 0 then + prestretch = i + end + end + n = node.end_of_math(n) + end + elseif node.id'glue' == id then + stretch[n.stretch_order+1] = stretch[n.stretch_order+1] + n.stretch + elseif node.id'rule' == id then + else + texio.write_nl'Foreign nodes detected in array field' + end + end + if startmath then + local poststretch + for i=1, 5 do + if stretch[i] ~= 0 then + poststretch = i + end + end + store_column(startmath).columnalign = prestretch < poststretch and 'left' or prestretch > poststretch and 'right' or nil -- or 'center' -- center is already default + else + texio.write_nl'Formula missing in array field' + end +end + +local saved_array + +funcid = luatexbase.new_luafunction'__luamml_array_finalize_array:' +token.set_lua('__luamml_array_save_array:', funcid) +lua.get_functions_table()[funcid] = function() + -- TODO: Error handling etc. + local colsep = tex.dimen['col@sep'] + saved_array = get_table() + if colsep ~= 0 then + saved_array = {[0] = 'mpadded', + width = string.format('%+.3fpt', 2*colsep/65781.76), + lspace = string.format('%+.3fpt', colsep/65781.76), + saved_array + } + end +end + +funcid = luatexbase.new_luafunction'__luamml_array_finalize_array:' +token.set_lua('__luamml_array_finalize_array:', funcid) +lua.get_functions_table()[funcid] = function() + -- TODO: Error handling etc. + local nucl = tex.nest.top.tail.nucleus + local props = properties[nucl] + if not props then + props = {} + properties[nucl] = props + end + props.mathml_table = saved_array + saved_array = nil +end diff --git a/luamml-convert.lua b/luamml-convert.lua index aa17967..cd225e1 100644 --- a/luamml-convert.lua +++ b/luamml-convert.lua @@ -45,7 +45,7 @@ local function delim_to_table(delim) if props then return props end local char = delim.small_char if char == 0 then - return {[0] = 'mspace', width = string.format("%.2fpt", tex.nulldelimiterspace/65781.76)}, space_like + return {[0] = 'mspace', width = string.format("%.3fpt", tex.nulldelimiterspace/65781.76)}, space_like else local fam = delim.small_fam char = remap_lookup[fam << 21 | char] @@ -277,10 +277,10 @@ local function space_to_table(amount, sub, cur_style) if amount == 0 then return end if sub == 99 then -- TODO magic number -- 18*2^16=1179648 - return {[0] = 'mspace', width = string.format("%.2fem", amount/1179648)}, space_like + return {[0] = 'mspace', width = string.format("%.3fem", amount/1179648)}, space_like else -- 65781.76=tex.sp'100bp'/100 - return {[0] = 'mspace', width = string.format("%.2fpt", amount/65781.76)}, space_like + return {[0] = 'mspace', width = string.format("%.3fpt", amount/65781.76)}, space_like end end diff --git a/luamml-patches-array.sty b/luamml-patches-array.sty new file mode 100644 index 0000000..3b7f9fa --- /dev/null +++ b/luamml-patches-array.sty @@ -0,0 +1,43 @@ +\ProvidesExplPackage {luamml-patches-array} {2021-04-23} {0.0.1-alpha} + {Feel free to add a description here} + +\lua_now:n { require'luamml-array' } + +\cs_new:Npn \__luamml_array_dollarend_array: { + \luamml_flag_save: + $ + \__luamml_array_add_list_to_row: +} + +\cs_new_eq:NN \luamml@d@llarend@array \__luamml_array_dollarend_array: +\cs_set:Npn \array { + \col@sep \arraycolsep + \def \d@llarbegin { $ } + \let \d@llarend \luamml@d@llarend@array + \def \@halignto {} + \@tabarray +} +\cs_set:Npn \endarray { + \crcr + \__luamml_array_save_array: + \egroup + \egroup + \__luamml_array_finalize_array: + \@arrayright + \gdef \@preamble {} +} +\cs_set:Npn \endalign { + \math@cr + \black@ \totwidth@ + \__luamml_amsmath_finalize_table: + \egroup + \ifingather@ + \restorealignstate@ + \egroup + \nonumber + \ifnum0=‘{\fi\iffalse}\fi + \else + $$ + \fi + \ignorespacesafterend +} diff --git a/luamml-table.lua b/luamml-table.lua index f4321c4..e151a91 100644 --- a/luamml-table.lua +++ b/luamml-table.lua @@ -34,6 +34,7 @@ local function store_column(startmath, display) mml = {[0] = 'mtd', mml} end table.insert(store_get_row(), mml) + return mml end local function store_tag(xml) diff --git a/luamml.sty b/luamml.sty index d9ef869..f563a75 100644 --- a/luamml.sty +++ b/luamml.sty @@ -18,4 +18,15 @@ \int_set:Nn \l__luamml_flag_int { 6 } } -\RequirePackage { luamml-patches-amsmath } +\cs_new:Npn \__luamml_patch_package:nn #1 #2 { + \@ifpackageloaded {#1} {#2} { + \hook_gput_code:nnn {package/after/#1} {luamml} {#2} + } +} +\cs_new:Npn \__luamml_patch_package:n #1 { + \__luamml_patch_package:nn {#1} { + \RequirePackage { luamml-patches-#1 } + } +} +\__luamml_patch_package:n {amsmath} +\__luamml_patch_package:n {array}