I have successfully loaded XML in LuaLaTeX with help of Michal.h21. Now I would like to store all the customized defined Lua Script in a separate LaTeX Style file, like bibr.sty, so that I will call just \usepackage{bibr} in the LaTeX file.
In the bibr.sty file all the customized defined lua script need to be defined.
My BIBR Cross-Links XML file is below:
\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>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>–<xref ref-type="bibr" rid="B5">5</xref>). In elderly patients, other conditions such as spinal canal stenosis, Parkinson's disease, and polyneuropathy may influence the gait negatively.</para>
</art>]]
local dom = domobject.parse(sample)
local function process_instructions(el)
-- 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
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 Output Result in Command-Prompt/Terminal
print(result)
-- Print Output Result in PDF uncomment below
--transform.print_tex(result)
\end{luacode*}
\end{document}
How to achieve this?

lua script. So, Its better to include all thelua codeinMain LaTeXfile, to maintain each and separateLaTeX Style file(for fixing any bugs our ends in future). – Balaji Jul 26 '23 at 15:39\directlua{require("Balaji")}I would put the lua in a .lua file and include it that way in the latex package, using lua inline in the tex especially via luacode is tricky and inefficient. – David Carlisle Jul 26 '23 at 15:43