XML 键盘字符转 TeX 字符 - LuaLaTeX

XML 键盘字符转 TeX 字符 - LuaLaTeX

我想将所有 XML 键盘字符转换为 LaTeX 代码转换。例如,$应转换为\${应在 LaTeX 中转换为\{。如何实现?

我知道可以进行find and replace格式化。但我想这样做Lua function

我的 MWE 是:

\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}

答案1

luaxml-transform库确实进行了转义:

\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}

在此示例中,我已配置<title>生成一个部分和<para>一个段落。@<.>字符串被元素的内容替换。结果如下所示:

在此处输入图片描述

您可以添加新的转义。来自LuaXML文档:

您可能希望转义某些字符,或使用 LaTeX 命令替换它们。您可以使用 Transform 对象中包含的 unicodes 表:

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)

编辑:

您可以这样处理<?LuaLaTeX处理指令:

\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}

相关内容