3

My Input LaTeX FileName is Article05.tex. I would like to find the text <stepn number>(.-)</stepn number> and store the text is individual file like name is Article05-step.tex. How to achieve this in LuaLaTeX?

My MWE is:

\documentclass{article}
\usepackage{luacode}

\begin{luacode*}
xml = require('luaxml-mod-xml')
handler = require('luaxml-mod-handler')
\end{luacode*}

\begin{document}

\begin{luacode*}
sample = [[
<?xml version="1.0" encoding="utf-8"?>
<art>
<title>Scattering of flexural waves an electric current</title>
<para>Smart testing structures are components <step1>Reduce acoustic noise.</step1> used in engineering applications that are capable of sensing or reacting to their environment <step3>Change their mechanical properties.</step3> in a predictable and desired manner. In addition to <step2>Carrying mechanical loads</step2>, <!--may smart structures--> alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para>
</art>]]
treehandler = handler.simpleTreeHandler()
x = xml.xmlParser(treehandler)
x:parse(sample)
tex.sprint(treehandler.root["art"]["title"])

for _, element in ipairs(treehandler.root["art"]["para"]) do
  --if element._attr["attr"] == "A2" then
  --tex.print("\\\\")
  --tex.sprint(element)
  --end
end
\end{luacode*}
\end{document}

In the Article05-step.tex should be:

STEP1: Reduce acoustic noise.
STEP2: Change their mechanical properties.

1 Answer 1

3

Try this:

\documentclass{article}
\usepackage{luacode}


\begin{document}

\begin{luacode*}
local domobject = require "luaxml-domobject"
sample = [[
<?xml version="1.0" encoding="utf-8"?>
<art>
<title>Scattering of flexural waves an electric current</title>
<para>Smart testing structures are components <step1>Reduce acoustic noise.</step1> used in engineering applications that are capable of sensing or reacting to their environment <step2>Change their mechanical properties.</step2> in a predictable and desired manner. In addition to carrying mechanical loads, <!--may smart structures--> alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para>
</art>
]]

local f = io.open(tex.jobname .. "-step.tex", "w")
local dom = domobject.parse(sample)
local steps = {}
dom:traverse_elements(function(el)
-- detect if element is named stepN
local number = el:get_element_name():match("^step([0-9]+)")
if number then
  steps[#steps+1] = {number = tonumber(number), text = el:get_text()}
  -- f:write("STEP" .. number ..": " ..  .. "\n")
end
end)
table.sort(steps, function(a, b) return a.number < b.number end)
for k,v in ipairs(steps) do
  f:write("STEP" .. v.number ..": " .. v.text .. "\n")
end
  
f:close()
\end{luacode*}
\end{document}

It uses LuaXML's DOM object. The dom:traverse_elements function loops over all elements, detects if the element name starts with step and save it's text content to the \jobname-step.tex file.

This is the result:

STEP1: Reduce acoustic noise.
STEP2: Change their mechanical properties.
4
  • 1
    Brilliant Michal. It's working fine what i have expected. Thanks for your immediate help.
    – Balaji
    Commented Jul 28, 2021 at 15:57
  • How to sort the step number in ascending order. For Example: STEP1: Reduce acoustic noise. STEP3: Change their mechanical properties. STEP2: Carrying mechanical loads shoule be STEP1: Reduce acoustic noise. STEP2: Carrying mechanical loads STEP3: Change their mechanical properties.
    – Balaji
    Commented Aug 19, 2021 at 13:09
  • @Balaji you would need to save steps into array and sort it. see the updated answer
    – michal.h21
    Commented Aug 19, 2021 at 13:38
  • Great Michal. It's working fine now. Thanks for your update.
    – Balaji
    Commented Aug 19, 2021 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .