# XML Keyboard Characters to TeX Characters - LuaLaTeX

I would like to convert all the XML Keyboard Characters to LaTeX Code Conversion. For Example $ should convert to \$, { should convert to \{ in LaTeX. How to achieve this?

I know it's possible to do find and replace format. But i would like to do Lua function.

My MWE IS:

\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 $a+b$ components Reduce acoustic noise. used in engineering applications that are capable of sensing or reacting to their environment change their mechanical properties. in a predictable and desired manner. In addition to carrying mechanical loads, alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para>
</art>
]]
local dom = domobject.parse(sample)
dom=string.gsub(sample,"%$(.-)%$","\\$%1\\$")
print(dom)
\end{luacode*}
\end{document}

• do you need to convert $to \$ ? You could more simply print the string back to tex with a catcode table in which $had catcode 12 so wasn't special, similarly { Aug 11 '21 at 11:43 • @DavidCarlisle: How about to convert XML Processing Instructions tag (like <?LuaLaTeX This is some text need to print in LaTeX?> should convert to LaTeX Code This is some text need to print in LaTeX. Aug 11 '21 at 11:52 • Maybe a tool like xml2tex could be useful here. Aug 11 '21 at 12:24 ## 1 Answer The luaxml-transform library does escaping: \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 their mechanical properties. in a predictable and desired manner. In addition to carrying mechanical loads, alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para> <para>Another paragraph.</para> </art> ]] local dom = domobject.parse(sample) local transformer = transform.new() transformer:add_action("title", "\\section{@<.>}") transformer:add_action("para", "@<.>\\par") transform.print_tex(transformer:process_dom(dom)) \end{luacode*} \end{document}  In this example, I've configured <title> to produce a section and <para> to produce a paragraph. The @<.> string is replaced by the element's content. The result looks like this: You can add a new escapes. From the LuaXML docs: You may want to escape certain characters, or replace them with LaTeX commands. You can use the unicodes table contained in the Transform object: local transformer = transform.new() -- you must use the Unicode character code transformer.unicodes[124] = "\\textbar" local text = '<x>|</x>' local result = transformer:parse_xml(text) transform.print_tex(result)  Edit: This is how you can process the <?LuaLaTeX  processing instructions: \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 their mechanical properties. in a predictable and desired manner. In addition to carrying mechanical loads, alleviate vibration, reduce acoustic noise, change their mechanical properties as required or monitor their own condition.</para>
<para>Another paragraph.  <?LuaLaTeX Sample \textit{Text Here}?></para>
</art>
]]

local dom = domobject.parse(sample)

-- the transform library doesn't support procesing instructions transformation, so we need to
-- replace them with elements that can be then transformed
local function process_instructions(el)
for _, child in ipairs(el:get_children()) do
local ntype = child:get_node_type()
-- replace <?LuaLaTeX ... ?>
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
-- process children elements
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}

• How to transform xml processing instruction tags? Example: <?LuaLaTeX Sample Text Here?> should convert LaTeX Code to Sample Text Here. Aug 11 '21 at 12:42
• @Balaji you need to convert them to elements first. See the update Aug 11 '21 at 15:37
• Its working fine except \vspace command. <?LuaLaTeX \vskip12pt Sample \hspace{8pt} \textit{Text Here} and \vspace{12pt} vertical space?>. For \vskip12pt and \hspace{8pt} is working and \vspace{12pt} is not working. How to do this? Aug 12 '21 at 6:14
• @Balaji this works in the same way as if you wrote it directly in LaTeX Aug 12 '21 at 7:57