带有锚标记的同一文档中的图形文本

带有锚标记的同一文档中的图形文本

我曾提到过这个链接:使用 LuaLaTeX 创建单独的文件

但我需要同一份文件。

我想在figure标题中添加锚标签,并将图形标题文本移动到文档的顶部。

我的 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 components <figure id="ab8cf1" position="float"><label>Figure 1.</label><caption id="ab8cf1"><p>Carter&#x2013;Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> &#x3d; 0 are repulsive singularities, the intersecting lines <italic>x</italic> &#x3d; 1 form a Killing horizon, and <italic>x</italic> &#x3d; &#x221e; is a temporal infinity.</p></caption><graphic id="ab8cf1" content-type="print" xlink:href="ab8cf1.pdf"/><graphic id="ab8cf1" content-type="high" xlink:href="ab8cf1.jpg"/></figure> used in engineering applications that are capable of sensing or reacting to their environment <figure id="ab8cf2" position="float"><label>Figure 2.</label><caption id="ab8cf2"><p>Carter&#x2013;Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> &#x3d; 0 are repulsive singularities, the intersecting lines <italic>x</italic> &#x3d; 1 form a Killing horizon, and <italic>x</italic> &#x3d; &#x221e; is a temporal infinity.</p></caption><graphic id="ab8cf2" content-type="print" xlink:href="ab8cf2.pdf"/><graphic id="ab8cf2" content-type="high" xlink:href="ab8cf2.jpg"/></figure> 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 dom = domobject.parse(sample)
dom:traverse_elements(function(el)
--local FigNo = el:get_element_name():match("%<figure id=\"(.-)\"")
--local FigCount = el:get_element_name():match("%<figure id=\"(.-)f([0-9]+)\"")
print("FigNo IS:",FigNo)
--dom=string.gsub(dom,"<figure id(.-)</fig>,"\\InsertFigure{FigNo}{f..FigCount}{}{b}")
end)
\end{luacode*}
\end{document}

我希望的结果是:

    \begin{luacode*}
    local domobject = require "luaxml-domobject"
    sample = [[
    <?xml version="1.0" encoding="utf-8"?>
    <art>
<figure id="ab8cf1" position="float"><label>Figure 1.</label><caption id="ab8cf1"><p>Carter&#x2013;Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> &#x3d; 0 are repulsive singularities, the intersecting lines <italic>x</italic> &#x3d; 1 form a Killing horizon, and <italic>x</italic> &#x3d; &#x221e; is a temporal infinity.</p></caption><graphic id="ab8cf1" content-type="print" xlink:href="ab8cf1.pdf"/><graphic id="ab8cf1" content-type="high" xlink:href="ab8cf1.jpg"/></figure>
<figure id="ab8cf2" position="float"><label>Figure 2.</label><caption id="ab8cf2"><p>Carter&#x2013;Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> &#x3d; 0 are repulsive singularities, the intersecting lines <italic>x</italic> &#x3d; 1 form a Killing horizon, and <italic>x</italic> &#x3d; &#x221e; is a temporal infinity.</p></caption><graphic id="ab8cf2" content-type="print" xlink:href="ab8cf2.pdf"/><graphic id="ab8cf2" content-type="high" xlink:href="ab8cf2.jpg"/></figure>
    <title>Scattering of flexural waves an electric current</title>
    <para>Smart testing structures are components \InsertFigure{ab8cf1}{f1}{}{t} used in engineering applications that are capable of sensing or reacting to their environment \InsertFigure{ab8cf2}{f2}{}{t} 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>
    ]]

答案1

我认为在这种情况下 LaTeX 代码没什么用。只需创建一个独立的 Lua 文件并使用texlua它执行它:

kpse.set_program_name "luatex"

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 <figure id="ab8cf1" position="float"><label>Figure 1.</label><caption id="ab8cf1"><p>Carter&#x2013;Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> &#x3d; 0 are repulsive singularities, the intersecting lines <italic>x</italic> &#x3d; 1 form a Killing horizon, and <italic>x</italic> &#x3d; &#x221e; is a temporal infinity.</p></caption><graphic id="ab8cf1" content-type="print" xlink:href="ab8cf1.pdf"/><graphic id="ab8cf1" content-type="high" xlink:href="ab8cf1.jpg"/></figure> used in engineering applications that are capable of sensing or reacting to their environment <figure id="ab8cf2" position="float"><label>Figure 2.</label><caption id="ab8cf2"><p>Carter&#x2013;Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> &#x3d; 0 are repulsive singularities, the intersecting lines <italic>x</italic> &#x3d; 1 form a Killing horizon, and <italic>x</italic> &#x3d; &#x221e; is a temporal infinity.</p></caption><graphic id="ab8cf2" content-type="print" xlink:href="ab8cf2.pdf"/><graphic id="ab8cf2" content-type="high" xlink:href="ab8cf2.jpg"/></figure> 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 dom = domobject.parse(sample)
local figures = {}
local figno = 0
-- insert \InsertFigure
dom:traverse_elements(function(el)
if  el:get_element_name() == "figure" then
  figno = figno + 1
  local figid = el:get_attribute "id"
  -- save the figure element for later processing
  table.insert(figures, el)
  -- replace figure with \InsertFigure
  local parent = el:get_parent()
  local new = parent:create_text_node(string.format("\\InsertFigure{%s}{%i}{}{b}", figid, figno))
  el:replace_node(new)
end
end)
-- move <figure> elements
local art = dom:query_selector("art")[1]
if art then
  -- we want to insert figures at beginning of the <art> element
  -- we saved <figure> elements in dom:traverse_elements
  for i, figure in ipairs(figures) do
    art:add_child_node(figure, i)
  end
end

-- print transformed xml
print(dom:serialize())

跑步:

texlua sample.lua

它将在终端打印以下结果:

<?xml version='1.0' encoding='utf-8' ?>
<art><figure id='ab8cf1' position='float'><label>Figure 1.</label><caption id='ab8cf1'><p>Carter–Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> = 0 are repulsive singularities, the intersecting lines <italic>x</italic> = 1 form a Killing horizon, and <italic>x</italic> = ∞ is a temporal infinity.</p></caption><graphic content-type='print' id='ab8cf1' xlink:href='ab8cf1.pdf'></graphic><graphic content-type='high' id='ab8cf1' xlink:href='ab8cf1.jpg'></graphic></figure><figure id='ab8cf2' position='float'><label>Figure 2.</label><caption id='ab8cf2'><p>Carter–Penrose diagram for the inverted black hole spacetime. The lines <italic>x</italic> = 0 are repulsive singularities, the intersecting lines <italic>x</italic> = 1 form a Killing horizon, and <italic>x</italic> = ∞ is a temporal infinity.</p></caption><graphic content-type='print' id='ab8cf2' xlink:href='ab8cf2.pdf'></graphic><graphic content-type='high' id='ab8cf2' xlink:href='ab8cf2.jpg'></graphic></figure>
<title>Scattering of flexural waves an electric current</title>
<para>Smart testing structures are components \InsertFigure{ab8cf1}{1}{}{b} used in engineering applications that are capable of sensing or reacting to their environment \InsertFigure{ab8cf2}{2}{}{b} 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>

相关内容