array improvements

This commit is contained in:
Marcel Fabian Krüger 2021-04-30 19:03:36 +02:00
parent b33340721f
commit c026f67867
2 changed files with 90 additions and 68 deletions

View File

@ -7,51 +7,45 @@ local get_table = require'luamml-table'.get_table
local properties = node.get_properties_table() local properties = node.get_properties_table()
local funcid = luatexbase.new_luafunction'__luamml_array_add_list_to_row:' local funcid = luatexbase.new_luafunction'__luamml_array_init_col:'
token.set_lua('__luamml_array_add_list_to_row:', funcid, 'protected') token.set_lua('__luamml_array_init_col:', funcid, 'protected')
lua.get_functions_table()[funcid] = function() lua.get_functions_table()[funcid] = function()
-- TODO: Error handling etc -- TODO: Error handling etc
-- local box = token.scan_int() local nest = tex.nest[tex.nest.ptr-1]
local startmath -- The special will be deleted again, it just marks the right math list since the start math node is not there yet
local preskip local special = node.new('whatsit', 'special')
local postskip node.insert_after(nest.tail, nest.tail, special)
local prestretch = 0 -- Not one since overflowing content protrudes right nest.tail = special
local stretch = {0, 0, 0, 0, 0} local temp = nest.head
local n = tex.nest.top.head.next local props = properties[temp]
local func, ctx, n = node.traverse(tex.nest.top.head.next) if not props then
while true do props = {}
local id, sub n, id, sub = func(ctx, n) properties[temp] = props
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 end
startmath = n props.luamml_array_startmath = special
for i=2, 5 do
if stretch[i] ~= 0 then
prestretch = i
end end
end
n = node.end_of_math(n) local funcid = luatexbase.new_luafunction'__luamml_array_finalize_col:w'
end token.set_lua('__luamml_array_finalize_col:w', funcid, 'protected')
elseif node.id'glue' == id then lua.get_functions_table()[funcid] = function()
stretch[n.stretch_order+1] = stretch[n.stretch_order+1] + n.stretch local alignment = token.scan_int() -- Do it first to consume number even if we end early
elseif node.id'rule' == id then -- TODO: Error handling etc
local temp = tex.nest.top.head
local props = properties[temp]
local special = props and props.luamml_array_startmath
if not special then return end
node.remove(tex.nest.top.head, special)
local startmath = node.free(special)
props.luamml_array_startmath = nil
alignment = alignment == 1 and 'left' or alignment == 2 and 'right' or nil
if node.end_of_math(startmath) == tex.nest.top.tail then
if startmath.nest == tex.nest.top.tail then return end
store_column(startmath).columnalign = alignment
else else
texio.write_nl'Foreign nodes detected in array field' -- Oh no, we got text. Let't complain to the user, it's probably their fault
end print'We are mathematicians, don\'t bother us with text'
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
end end

View File

@ -3,20 +3,63 @@
\lua_now:n { require'luamml-array' } \lua_now:n { require'luamml-array' }
\cs_new:Npn \__luamml_array_dollarend_array: { \cs_set:Npn \@classz {
\@classx
\@tempcnta \count@
\prepnext@tok
\@addtopreamble {
\ifcase \@chnum
\hfil
\hskip 1sp
\d@llarbegin
\__luamml_array_init_col:
\insert@column
\luamml_flag_save: \luamml_flag_save:
$ \d@llarend
\__luamml_array_add_list_to_row: \__luamml_array_finalize_col:w 0~
\do@row@strut
\hfil
\or
\hskip 1sp
\d@llarbegin
\__luamml_array_init_col:
\insert@column
\luamml_flag_save:
\d@llarend
\__luamml_array_finalize_col:w 1~
\do@row@strut
\hfil
\or
\hfil
\hskip 1sp
\d@llarbegin
\__luamml_array_init_col:
\insert@column
\luamml_flag_save:
\d@llarend
\__luamml_array_finalize_col:w 2~
\do@row@strut
\or
\setbox \ar@mcellbox \vbox \@startpbox { \@nextchar }
\insert@column
\@endpbox
\ar@align@mcell
\do@row@strut
\or
\vtop \@startpbox { \@nextchar }
\insert@column
\@endpbox
\do@row@strut
\or
\vbox \@startpbox { \@nextchar }
\insert@column
\@endpbox
\do@row@strut
\fi
}
\prepnext@tok
} }
\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 { \cs_set:Npn \endarray {
\crcr \crcr
\__luamml_array_save_array: \__luamml_array_save_array:
@ -26,18 +69,3 @@
\@arrayright \@arrayright
\gdef \@preamble {} \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
}