1

In the XML file cross-links are not expended like 2–5 and final latex should be \citep{B2,B3,B4,B5}.

Any Number should expend in all the cross-links like, if XML coded as 11–18 it should be convert to \citep{B11,B12,B13,B14,B15,B16,B17,B18} (beginning to end). How to get the expected result using LuaLaTeX?

My XML is:

\documentclass{article}
\usepackage{luacode}

\begin{document}

\begin{luacode} local domobject = require "luaxml-domobject" local transform = require "luaxml-transform" sample = [[ <?xml version="1.0" encoding="utf-8"?> <art> <title>Scattering of flexural waves an electric current</title> <para>Smart testing structures are $a+b$ components Reduce acoustic noise used in engineering applications that are capable of sensing or reacting to their environment change <?LuaLaTeX Sample XXX1?> their mechanical properties. in a predictable and desired manner. In addition to carrying mechanical loads, <?LuaLaTeX \hspace{12pt}Abc?> alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para> <para>The first <xref ref-type="section" rid="Sec1">Section 1</xref> description and correlation between gait impairment and hydrocephalus were made in 1965 by Hakim and Adam (<xref ref-type="bibr" rid="B1">1</xref>). The etiology of idiopathic normal pressure hydrocephalus (iNPH) has not yet been entirely understood (<xref ref-type="bibr" rid="B2">2</xref>&#x02013;<xref ref-type="bibr" rid="B5">5</xref>). In elderly patients, other conditions such as spinal canal stenosis, Parkinson&#x00027;s disease, and polyneuropathy may influence the gait negatively.</para> </art>]]

local dom = domobject.parse(sample) local function process_instructions(el) for _, child in ipairs(el:get_children()) do local ntype = child:get_node_type() if ntype == "PI" and child._name=="LuaLaTeX" then local text = child._attr[ "_text" ] local newel = el:create_element("lualatex-instruction", {text = text}) child:replace_node(newel) end if child:is_element() then process_instructions(child) end end end

process_instructions(dom:root_node())

local transformer = transform.new() transformer:add_action("title", "\section{@<.>}") transformer:add_action("para", "@<.>\par") -- handle the processing instruction transformer:add_custom_action("lualatex-instruction", function(el) return el:get_attribute("text") end) transform.print_tex(transformer:process_dom(dom)) \end{luacode*} \end{document}

I have expected output LaTeX file is:

Smart testing structures are $a+b$ components Reduce acoustic noise used in
engineering applications that are capable of sensing or reacting to their environment change Sample XXX1 their mechanical properties. in a predictable
and desired manner. In addition to carrying mechanical loads,
Abc alleviate
vibration, reduce acoustic noise, change their mechanical properties as required
or monitor their own condition.

The first Section 1 description and correlation between gait impairment and hydrocephalus were made in 1965 by Hakim and Adam (\citep{B1}). The etiology of idio- pathic normal pressure hydrocephalus (iNPH) has not yet been entirely under- stood (\citep{B2,B3,B4,B5}). In elderly patients, other conditions such as spinal canal stenosis, Parkinson’s disease, and polyneuropathy may influence the gait negatively.

Balaji
  • 2,282
  • you just assign a Lua string to sample and never use it, so this document has no output. – David Carlisle Jul 19 '23 at 16:26
  • @David: Yes and does not know how to get the expect output using lualatex. – Balaji Jul 19 '23 at 17:49
  • but there is no standard typeset form for xml. You would have to define how to handle each element and implement the transform to tex. – David Carlisle Jul 19 '23 at 17:52
  • @DavidCarlisle: I have updated the question. I know how to get the attribute value from xref. But not any idea expand the attribute values (like B2,B3,B4,B5).. How to get this? – Balaji Jul 20 '23 at 11:27

1 Answers1

1

So if I understand it correctly, you don't have the numbers that you want to expand explicitly stated as attributes of the xref element, but rather encoded as two separate xref elements:

<xref ref-type="bibr" rid="B2">2</xref>&#x02013;<xref ref-type="bibr" rid="B5">5</xref>

I don't think it is a good idea, but we can try anyway. In this case, you will need some pre-processing of the DOM to detect these cases and convert them to something that can be used in the tranformer library. This code should do that:

-- try to find <xref> followed by dash and another <xref>
-- we then expand rid attributes
for _, xref in ipairs(dom:query_selector("xref[ref-type='bibr']")) do
  local first = xref:get_sibling_node(1)
  local second = xref:get_sibling_node(2)
  if first and second and
     first:is_text() and first:get_text() == "–" and -- next node must be "–" text
     second:is_element() and second:get_element_name() == "xref" -- second element must be <xref>
  then
    local rid1 = xref:get_attribute("rid") or ""
    local rid2 = second:get_attribute("rid") or ""
    local prefix1, number1 = rid1:match("(%a+)(%d+)")
    local prefix2, number2 = rid2:match("(%a+)(%d+)")
    -- expand only if prefixes match each other
    if prefix1 and prefix2 and prefix1 == prefix2 then
      -- expand numbers
      local t = {}
      for i = number1, number2 do
        t[#t+1] = prefix1 .. math.floor(i)
      end
      -- save expanded numbers
      xref:set_attribute("rid", table.concat(t, ","))
      -- remove unnecessary elements
      first:remove_node()
      second:remove_node()
    end
  end

end

It detects <xref> elements followed by dash and another <xref>, sets the expanded ids as the rid attribute, and removes the unnecessary dash and second <xref>. You can then declare this transform rule to convert <xref> with ref-type of bibr to \citep:

transformer:add_action("xref[ref-type='bibr']", "\\citep{@{rid}}")

The @{rid} action will print value of the rid attribute.

This is the translated TeX result:

\section{Scattering of flexural waves an electric current}
Smart testing structures are \$a+b\$ components Reduce acoustic noise used in engineering applications that are capable of sensing or reacting to their environment change  Sample XXX1 their mechanical properties. 
in a predictable and desired manner. In addition to carrying mechanical loads,  \hspace*{12pt}Abc alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.\par
The first Section 1 description and correlation between gait impairment and hydrocephalus were made in 1965 by Hakim and Adam (\citep{B1}). 
The etiology of idiopathic normal pressure hydrocephalus (iNPH) has not yet been entirely understood (\citep{B2,B3,B4,B5}). 
In elderly patients, other conditions such as spinal canal stenosis, Parkinson's disease, and polyneuropathy may influence the gait negatively.\par

This is the full code:

\documentclass{article}
\usepackage{luacode}
\usepackage{natbib}

\begin{document}

\begin{luacode} local domobject = require "luaxml-domobject" local transform = require "luaxml-transform" sample = [[ <?xml version="1.0" encoding="utf-8"?> <art> <title>Scattering of flexural waves an electric current</title> <para>Smart testing structures are $a+b$ components Reduce acoustic noise used in engineering applications that are capable of sensing or reacting to their environment change <?LuaLaTeX Sample XXX1?> their mechanical properties. in a predictable and desired manner. In addition to carrying mechanical loads, <?LuaLaTeX \hspace{12pt}Abc?> alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para> <para>The first <xref ref-type="section" rid="Sec1">Section 1</xref> description and correlation between gait impairment and hydrocephalus were made in 1965 by Hakim and Adam (<xref ref-type="bibr" rid="B1">1</xref>). The etiology of idiopathic normal pressure hydrocephalus (iNPH) has not yet been entirely understood (<xref ref-type="bibr" rid="B2">2</xref>&#x02013;<xref ref-type="bibr" rid="B5">5</xref>). In elderly patients, other conditions such as spinal canal stenosis, Parkinson&#x00027;s disease, and polyneuropathy may influence the gait negatively.</para> </art>]]

local dom = domobject.parse(sample) local function process_instructions(el) for _, child in ipairs(el:get_children()) do local ntype = child:get_node_type() if ntype == "PI" and child._name=="LuaLaTeX" then local text = child._attr[ "_text" ] local newel = el:create_element("lualatex-instruction", {text = text}) child:replace_node(newel) end if child:is_element() then process_instructions(child) end end end

-- try to find <xref> followed by dash and another <xref> -- we then expand rid attributes for _, xref in ipairs(dom:query_selector("xref[ref-type='bibr']")) do local first = xref:get_sibling_node(1) local second = xref:get_sibling_node(2) if first and second and first:is_text() and first:get_text() == "–" and -- next node must be "–" text second:is_element() and second:get_element_name() == "xref" -- second element must be <xref> then local rid1 = xref:get_attribute("rid") or "" local rid2 = second:get_attribute("rid") or "" local prefix1, number1 = rid1:match("(%a+)(%d+)") local prefix2, number2 = rid2:match("(%a+)(%d+)") -- expand only if prefixes match each other if prefix1 and prefix2 and prefix1 == prefix2 then -- expand numbers local t = {} for i = number1, number2 do t[#t+1] = prefix1 .. math.floor(i) end -- save expanded numbers xref:set_attribute("rid", table.concat(t, ",")) -- remove unnecessary elements first:remove_node() second:remove_node() end end

end

process_instructions(dom:root_node())

local transformer = transform.new() transformer:add_action("title", "\section{@<.>}") transformer:add_action("para", "@<.>\par") transformer:add_action("xref[ref-type='bibr']", "\citep{@{rid}}") -- handle the processing instruction transformer:add_custom_action("lualatex-instruction", function(el) return el:get_attribute("text") end) local result = transformer:process_dom(dom) print(result) transform.print_tex(result) \end{luacode*} \end{document}

michal.h21
  • 50,697