[NTG-context] changing an XML node and reprocess it (or xmlflushing a string)

mf massifr at fastwebnet.it
Mon Oct 7 18:26:50 CEST 2019

i'm using XML and i find useful specifying a fraction made of text this way:

<span class="fraction">text for numerator/text for denominator</span>

With some lua, i can transform it into

\frac{\text{text for numerator}}{\text{text for denominator}}

which typesets something like this:

  text for numerator
text for denominator

Suppose you want to add some styling to the texts, like this:

<span class="fraction">text for <i>numerator</i>/text for 

You should split the span node into two elements and the xmlflush them.
This is my M(not)WE:

<p>A paragraph with a fraction made of text with styles:
<span class="fraction">a <i>fraction</i> made of text/with 
<red>styles</red> inside</span>.</p>

local sub = string.sub
local sfind = string.find
local xmltext = xml.text
local xmlconvert = xml.convert

local function numeratorDenominator( text )
   local before, after = sfind( text, "[^<]/[^>]" )
   local num, den
   if before and after then
     num = sub( text, 1, before )
     den = sub( text, after )
   return num, den

function xml.functions.textfraction( t )
   local text = xmltext( t, '' )
   local num, den = numeratorDenominator( text )
   if num and den then
     local fontstyle = tokens.getters.macro( "fontstyle" )
     local xml_num = xmlconvert( num )
     local xml_den = xmlconvert( den )
--    context( "$\\frac{\\text{\\" ..fontstyle .. " " .. num .. 
"}}{\\text{\\" ..fontstyle .. " " .. den .. "}}$" )
     context( "$\\frac{\\text{\\" ..fontstyle .. " " )
     context( num ) -- context.xmlprocessstring( xml_num )
     context( "}}{\\text{\\" ..fontstyle .. " " )
     context( den ) -- context.xmlprocessstring( xml_den )
     context( "}}$" )
     context.xmlflush( t )

\startxmlsetups xml:textsetups


\startxmlsetups xml:p

\startxmlsetups xml:i
   {\it \xmlflush{#1}}

\startxmlsetups xml:red
   {\red \xmlflush{#1}}

\startxmlsetups xml:fraction



More information about the ntg-context mailing list