Add labels to saved formulas

This commit is contained in:
Marcel Fabian Krüger 2021-06-27 00:43:30 +02:00
parent e6077a2702
commit e21116b501
7 changed files with 57 additions and 39 deletions

View File

@ -42,7 +42,7 @@
\m@th
\displaystyle
{##}
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
$
\__luamml_amsmath_add_last_to_row:
\tabskip \z@skip
@ -54,7 +54,7 @@
{}
##
}
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
$
\__luamml_amsmath_add_last_to_row:
\hfil
@ -85,7 +85,7 @@
\m@th
\displaystyle
##
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
$
\__luamml_amsmath_add_last_to_row:
\hfil
@ -122,7 +122,7 @@
\m@th
\displaystyle
{##}
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
$
}
\__luamml_amsmath_add_box_to_row:
@ -180,7 +180,7 @@
\ifmeasuring@
\luamml_flag_ignore:
\else
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
\fi
$
}
@ -204,7 +204,7 @@
\ifmeasuring@
\luamml_flag_ignore:
\else
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
\fi
$
}
@ -298,14 +298,14 @@
% Less luckily, \endmultline@math sometimes get overwritten for the last line.
% But that isn't a problem since we want special behavior there anyway.
\cs_set:Npn \endmultline@math {
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
$
\__luamml_amsmath_add_last_to_row:
}
\cs_set:Npn \rendmultline@ {
\iftag@
\luamml_flag_save:Nn \displaystyle {mtd}
\luamml_flag_save:nNn {} \displaystyle {mtd}
$
\__luamml_amsmath_add_last_to_row:
\let \endmultline@math \relax
@ -370,7 +370,7 @@
\m@th
\scriptstyle
##
\luamml_flag_save:n {mtd} % No \scriptsize here since we want to add the mstyle nodes
\luamml_flag_save:nn {} {mtd} % No \scriptsize here since we want to add the mstyle nodes
$
\__luamml_amsmath_add_last_to_row:
\hfil
@ -381,7 +381,7 @@
\m@th
\scriptstyle
##
\luamml_flag_save:n {mtd} % No \scriptsize here since we want to add the mstyle nodes
\luamml_flag_save:nn {} {mtd} % No \scriptsize here since we want to add the mstyle nodes
$
\__luamml_amsmath_add_last_to_row:
\hfil

View File

@ -18,7 +18,7 @@
} {
\__luamml_array_init_col:
\insert@column
\luamml_flag_save:n {mtd}
\luamml_flag_save:nn {} {mtd}
\d@llarend
\__luamml_array_finalize_col:w 0~
}
@ -33,7 +33,7 @@
} {
\__luamml_array_init_col:
\insert@column
\luamml_flag_save:n {mtd}
\luamml_flag_save:nn {} {mtd}
\d@llarend
\__luamml_array_finalize_col:w 1~
}
@ -49,7 +49,7 @@
} {
\__luamml_array_init_col:
\insert@column
\luamml_flag_save:n {mtd}
\luamml_flag_save:nn {} {mtd}
\d@llarend
\__luamml_array_finalize_col:w 2~
}

View File

@ -9,7 +9,7 @@
\m@th
#1
{#2}
\luamml_flag_save:Nn #1 {mphantom}
\luamml_flag_save:nNn {} #1 {mphantom}
$
}
\finph@nt

View File

@ -9,6 +9,7 @@ local write_xml = require'luamml-xmlwriter'
local write_struct = require'luamml-structelemwriter'
local filename_token = token.create'l__luamml_filename_tl'
local label_token = token.create'l__luamml_label_tl'
local properties = node.get_properties_table()
local mmode, hmode, vmode do
@ -70,6 +71,8 @@ local mlist_result
local undefined_cmd = token.command_id'undefined_cs'
local call_cmd = token.command_id'call'
local labelled_mathml = {}
local function save_result(xml, display, structelem)
mlist_result = make_root(xml, display and 0 or 2)
token.put_next(filename_token)
@ -121,6 +124,18 @@ luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style)
properties[startmath] = props
end
props.saved_mathml_table, props.saved_mathml_core = xml, core
token.put_next(label_token)
local label = token.scan_argument()
if label ~= '' then
if labelled_mathml[label] then
tex.error('MathML Label already in use', {
'A MathML expression has a label which is already used by another \z
formula. If you do not want to label this formula with a unique \z
label, set a empty label instead.'})
else
labelled_mathml[label] = xml
end
end
if flag & 10 == 8 then
write_struct(xml, true) -- This modifies xml in-place to reference the struture element
end
@ -148,4 +163,5 @@ require'luamml-tex-annotate'
return {
save_result = save_result,
labelled = labelled_mathml,
}

View File

@ -88,6 +88,7 @@
%<luatex>\tl_new:N \l__luamml_filename_tl
\tl_new:N \l__luamml_root_tl
\tl_set:Nn \l__luamml_root_tl { mrow }
\tl_new:N \l__luamml_label_tl
%<pdftex>\int_new:N \g__luamml_formula_id_int
%<luatex>\int_new:N \tracingmathml
% \end{macrocode}
@ -110,6 +111,7 @@
% \cs{luamml_flag_save:}.
% \begin{macrocode}
\cs_new_protected:Npn \luamml_flag_process: {
\tl_set:Nn \l__luamml_label_tl {}
\int_set:Nn \l__luamml_flag_int { 3 }
}
% \end{macrocode}
@ -128,33 +130,37 @@
}
% \end{macro}
%
% \begin{macro}{\luamml_flag_save:,
% \luamml_flag_save:N,
% \luamml_flag_save:n,
% \luamml_flag_save:Nn}
% \begin{macro}{\luamml_flag_save:n,
% \luamml_flag_save:nN,
% \luamml_flag_save:nn,
% \luamml_flag_save:nNn}
% Convert the current formula but only save it's representation in the math
% node without emitting it as a complete formula. This is useful when the
% expression forms part of a bigger formula and will be intergrated into it's
% MathML tables later by special code.
% It optinally accepts two parameters: One math style command
% It optinally accepts three parameters: A label, one math style command
% (\cs{displaystyle}, \cs{textstyle}, etc.) which is the implicit math style
% (so the style which the surrounding code expects this style to have) and a
% name for the root element (defaults to \texttt{mrow}).
% If the root element name is \texttt{mrow}, it will get suppressed in some
% cases.
% \begin{macrocode}
\cs_new_protected:Npn \luamml_flag_save: {
\cs_new_protected:Npn \luamml_flag_save:n #1 {
\tl_set:Nn \l__luamml_label_tl {#1}
\int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 1 }
}
\cs_new_protected:Npn \luamml_flag_save:N #1 {
\int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 17 + 32 * #1 }
\cs_new_protected:Npn \luamml_flag_save:nN #1#2 {
\tl_set:Nn \l__luamml_label_tl {#1}
\int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 17 + 32 * #2 }
}
\cs_new_protected:Npn \luamml_flag_save:n {
\cs_new_protected:Npn \luamml_flag_save:nn #1 {
\tl_set:Nn \l__luamml_label_tl {#1}
\int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 5 }
\tl_set:Nn \l__luamml_root_tl
}
\cs_new_protected:Npn \luamml_flag_save:Nn #1 {
\int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 21 + 32 * #1 }
\cs_new_protected:Npn \luamml_flag_save:nNn #1#2 {
\tl_set:Nn \l__luamml_label_tl {#1}
\int_set:Nn \l__luamml_flag_int { \__luamml_maybe_structelem: 21 + 32 * #2 }
\tl_set:Nn \l__luamml_root_tl
}
% \end{macrocode}
@ -176,6 +182,7 @@
% \begin{macrocode}
%<*luatex>
\cs_new_protected:Npn \luamml_flag_structelem: {
\tl_set:Nn \l__luamml_label_tl {}
\int_set:Nn \l__luamml_flag_int { 11 }
}
%</luatex>
@ -350,6 +357,8 @@
\int_use:N \l__luamml_flag_int
:
\l__luamml_root_tl
:
\l__luamml_label_tl
}
\__luamml_pdf_showlists:
\iow_now:Nx \l__luamml_pdf_stream {
@ -359,16 +368,6 @@
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\luamml_pdf_last_formula:}
% A unique identifier for the last formula written with \cs{luamml_pdf_write:}.
% Some form of this will probably be added to the Lua\TeX insterface at some point.
% \begin{macrocode}
\cs_new:Npn \luamml_pdf_last_formula: {
\int_use:N \g__luamml_formula_id_int
}
% \end{macrocode}
% \end{macro}
%
% \begin{macrocode}
%</pdftex>
% \end{macrocode}

View File

@ -8,7 +8,7 @@ local math_lists_block = l.Ct('### ' * l.Cg(l.C'display' * ' ', 'display')^-1 *
* non_final_list_block)^1
local generic_list_block = '### ' * (line - 'current page:') * non_final_list_block
local luamml_block = l.Cg('LUAMML_FORMULA_BEGIN:' * id * ':' * l.Ct(
l.Cg(id, 'flag') * ':' * l.Cg((1-l.P'\n')^0, 'tag') * l.P'\n'^1
l.Cg(id, 'flag') * ':' * l.Cg((1-l.S':\n')^0, 'tag') * ':' * l.Cg((1-l.P'\n')^1, 'label')^-1 * l.P'\n'^1
* (math_lists_block + generic_list_block/0)^0
* (line - 'LUAMML_FORMULA_END\n')^0

View File

@ -67,7 +67,7 @@ end
-- 1: Only save
-- 3: Generate normally
for i, block in ipairs(parsed.groups) do
local flag, tag = block.flag, block.tag
local flag, tag, label = block.flag, block.tag, block.label
block = block[1]
if flag & 3 ~= 0 then
local style = block.display and 0 or 2
@ -84,8 +84,11 @@ for i, block in ipairs(parsed.groups) do
xml = {[0] = tag, xml}
end
end
if style == 2 and flag & 1 == 1 then
parsed.mathml[i] = xml
if style == 2 and flag & 1 == 1 and label ~= '' then
if parsed.mathml[label] then
error'Invalid label reuse'
end
parsed.mathml[label] = xml
end
end
end