当我运行LuaLaTeX filename.tex
所有 PI 指令标签时,仅像<?LuaLaTeX Sample XXX1?>
和<?LuaLaTeX \hspace*{12pt}Abc?>
应该存储到名为的单独文件中LuaLaTeXpi.tex
。
它应该是获取前后文本,如their environment change <?LuaLaTeX Sample XXX1?> their mechanical propert
。前后的计数应至少为 25 个字符。
因此,预期文件LuaLaTeXPi.TeX
如下所示:
their environment change Sample XXX1 their mechanical propert
rrying mechanical loads, \hspace*{12pt}Abc alleviate vibration, red
\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>
</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}
答案1
您可以使用此代码获取周围的文本并将其保存到额外的文件中:
\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>
</art>]]
-- open LuaLaTeXpi file
local apifile = io.open("LuaLaTeXpi.tex", "w")
local dom = domobject.parse(sample)
local function save_pi(el)
-- get preceding and following element
local before = el:get_sibling_node(-1)
local after = el:get_sibling_node(1)
local before_text, after_text = "", ""
-- get surrounding 25 characters
if before then
before_text = before:get_text()
before_text = before_text:sub(-25)
end
if after then
after_text = after:get_text()
after_text = after_text:sub(1,25)
end
local text = before_text .. el._attr[ "_text" ] .. after_text .. "\n"
apifile:write(text)
end
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
save_pi(child)
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())
-- don't forget to close the file
apifile:close()
local transformer = transform.new()
transformer:add_action("title", "\\section{@<.>}")
transformer:add_action("para", "@<.>\\par", {verbatim=true})
-- 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}
这是的内容LuaLaTeXpi.tex
:
their environment change Sample XXX1 their mechanical propert
rrying mechanical loads, \hspace*{12pt}Abc alleviate vibration, red
原始答案:
我不完全理解这个问题,所以我会从两个方面回答:
- 要在 PDF 结果中打印转换后的 XML 代码,您的代码大部分都可以正常工作。但是,XML 代码片段中的 LaTeX 数学存在问题,它将显示为原始 LaTeX 代码,而不是格式化的数学。在最新版本的 LuaXML 中,您可以使用该
{verbatim=true}
属性来防止字符被转义:
\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>
</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", {verbatim=true})
-- 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}
- 对你的问题的第二种解释是你想将输出写入单独的文件。在这种情况下,你不会使用 TeX 文档,而只是使用 Lua 脚本。你可以使用 shell 管道将其输出重定向到文件。
kpse.set_program_name "luatex"
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>
</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", {verbatim=true})
-- handle the processing instruction
transformer:add_custom_action("lualatex-instruction", function(el)
return el:get_attribute("text")
end)
print(transformer:process_dom(dom))
与第一个变体中的代码相比,唯一的变化是使用kpse.set_program_name "luatex"
,如果您想要加载 LuaTeX 库,这是必需的,以及print(transformer:process_dom(dom))
,因为我们想要将转换后的文档打印到终端输出。
您可以使用以下方式执行它:
$ texlua myscript.lua > LuaLaTeXPi.TeX
这是生成的 TeX 文件:
\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