计算 XML 中的列数

计算 XML 中的列数

我已经成功加载XML但是LuaLaTeX如何加载XML-Tables

\documentclass{book}
\usepackage{luacode}

\begin{document}

\begin{luacode*}
local domobject = require "luaxml-domobject"
sample = [[
<datas>
  <arttitle>Quantum vacuum under mixed boundary conditions: the case for curved spacetime</arttitle>
  <table-wrap id="tab1" position="float">
  <label>Table 1ABC.</label>
  <caption id="t1"><p>The large <italic>x</italic> behavior for different <italic>w</italic>.</p></caption>
  <table>
  <colgroup>
  <col align="left"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  </colgroup>
  </table>
  <thead>
  <tr>
  <th>A1</th>
  <th>A2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>B1</td>
  <td>B2</td>
  </tr>
  </tbody>
  </table-wrap>
</datas>]]
local dom = domobject.parse(sample)
tex.sprint(dom:query_selector("arttitle")[1]:get_text())

\end{luacode*}
\end{document}

它正在打印文章标题PDF。如何在 PDF 中执行和创建number of columnsXML

答案1

编辑:

以下是如何将此 XML 文件转换为 LaTeX 并打印在文档中。我们可以利用luaxml-transform开发版本中的新库LuaXML

以下是更新后的 TeX 文件:

% https://tex.stackexchange.com/a/574004/2891
\documentclass{book}
\usepackage{luacode}

\begin{document}

\begin{luacode*}
local domobject = require "luaxml-domobject"
local transform = require "luaxml-transform"
sample = [[
<datas>
  <arttitle>Quantum vacuum under mixed boundary conditions: the case for curved spacetime</arttitle>
  <table-wrap id="tab1" position="float">
  <label>Table 1ABC.</label>
  <caption id="t1"><p>The large <italic>x</italic> behavior for different <italic>w</italic>.</p></caption>
  <table>
  <colgroup>
  <col align="left"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="char" char="."/>
  </colgroup>
  <thead>
  <tr>
  <th>A1</th>
  <th>A2</th>
  <th>A3</th>
  <th>A4</th>
  <th>A5</th>
  <th>A6</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>B1</td>
  <td>B2</td>
  <td>B3</td>
  <td>B4</td>
  <td>B5</td>
  <td>B6</td>
  </tr>
  </tbody>
  </table>
  </table-wrap>
</datas>]]
local dom = domobject.parse(sample)
local alignments = {left="l", center="c", right="r"}

-- rules for conversion
transform.add_action("p", "%s\n\n")
transform.add_action("italic", "\\textit{%s}")
transform.add_action("arttitle", "\\noindent{\\large\\bfseries %s\\par\\noindent}")
transform.add_action("table-wrap", [[
\begin{table}
%s
\end{table}
]])

transform.add_action("label", "\\label{%s}")
transform.add_action("caption", "\\caption{%s}")
transform.add_action("table", [[
\begin{tabular}{@{data-columns}}
%s
\end{tabular}
]])

transform.add_action("tr", "%s \\\\")
transform.add_action("td", "& %s ")
transform.add_action("td:first-child", "%s ")
transform.add_action("th", "& %s ")
transform.add_action("th:first-child", "%s ")

 
-- get table columns
local function get_columns(tbl)
  local columns = {}
  for _, column in ipairs(tbl:query_selector("col")) do
    -- save column alignment. left is default
    local align = column:get_attribute("align") or "left"
    table.insert(columns, alignments[align])
  end
  return table.concat(columns, " ")
end

for i, tbl in ipairs(dom:query_selector("table")) do
  -- save table columns in table attribute, so it is available in transformation rules
  local columns = get_columns(tbl)
  tbl:set_attribute("data-columns", columns)
end

local converted = transform.process_dom(dom)
print(converted)
transform.print_tex(converted)


\end{luacode*}
\end{document}

在这种情况下,我们使用以下代码在转换之前设置表格列:

-- get table columns
local function get_columns(tbl)
  local columns = {}
  for _, column in ipairs(tbl:query_selector("col")) do
    -- save column alignment. left is default
    local align = column:get_attribute("align") or "left"
    table.insert(columns, alignments[align])
  end
  return table.concat(columns, " ")
end

for i, tbl in ipairs(dom:query_selector("table")) do
  -- save table columns in table attribute, so it is available in transformation rules
  local columns = get_columns(tbl)
  tbl:set_attribute("data-columns", columns)
end

它设置自定义属性,data-columns并根据属性为表格环境提供所需的值align,就像l c c c c c本例一样。

转换规则声明如下:

transform.add_action("label", "\\label{%s}")

第一个参数是需要转换的 CSS 选择器。最简单的情况是,它只是元素名称。第二个参数是将要使用的模板。%s将替换为元素内容。

transform.add_action("table", [[
\begin{tabular}{@{data-columns}}
%s
\end{tabular}
]])

这个更复杂的转换配置了表。@{data-columns}选择data-columns我们之前在 DOM 处理函数中设置的属性。

transform.add_action("td", "& %s ")
transform.add_action("td:first-child", "%s ")

这是更具体的 CSS 规则的示例。&字符附加在表格单元格之前,除每行的第一个单元格外。

生成的 PDF 如下:

在此处输入图片描述

另一项编辑:

您可以使用此代码作为独立脚本,从命令行将 XML 转换为 LaTeX bxmltolatex.lua

kpse.set_program_name "luatex"
local domobject = require "luaxml-domobject"
local transform = require "luaxml-transform"

-- read input from stdio
local sample = io.read("*all")

local dom = domobject.parse(sample)


-- rules for conversion
transform.add_action("p", "%s\n\n")
transform.add_action("italic", "\\textit{%s}")
transform.add_action("arttitle", "\\noindent{\\large\\bfseries %s\\par\\noindent}")
transform.add_action("table-wrap", [[
\begin{table}
%s
\end{table}
]])

transform.add_action("label", "\\label{%s}")
transform.add_action("caption", "\\caption{%s}")
transform.add_action("table", [[
\begin{tabular}{@{data-columns}}
%s
\end{tabular}
]])

transform.add_action("tr", "%s \\\\")
transform.add_action("td", "& %s ")
transform.add_action("td:first-child", "%s ")
transform.add_action("th", "& %s ")
transform.add_action("th:first-child", "%s ")

-- ignore colgroup
transform.add_action("colgroup", "")

 
-- get table columns
local alignments = {left="l", center="c", right="r"}

local function get_columns(tbl)
  local columns = {}
  for _, column in ipairs(tbl:query_selector("col")) do
    -- save column alignment. left is default
    local align = column:get_attribute("align") or "left"
    table.insert(columns, alignments[align])
  end
  return table.concat(columns, " ")
end

for i, tbl in ipairs(dom:query_selector("table")) do
  -- save table columns in table attribute, so it is available in transformation rules
  local columns = get_columns(tbl)
  tbl:set_attribute("data-columns", columns)
end

local converted = transform.process_dom(dom)
print(converted)

file.xml 是:

<datas>
  <arttitle>Quantum vacuum under mixed boundary conditions: the case for curved spacetime</arttitle>
  <table-wrap id="tab1" position="float">
  <label>Table 1ABC.</label>
  <caption id="t1"><p>The large <italic>x</italic> behavior for different <italic>w</italic>.</p></caption>
  <table>
  <colgroup>
  <col align="left"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  </colgroup>
  <thead>
  <tr>
  <th>A1</th>
  <th>A2</th>
  <th>A3</th>
  <th>A4</th>
  <th>A5</th>
  <th>A6</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>B1</td>
  <td>B2</td>
  <td>B3</td>
  <td>B4</td>
  <td>B5</td>
  <td>B6</td>
  </tr>
  </tbody>
  </table>
  </table-wrap>
</datas>

用途:

texlua bxmltolatex.lua < file.xml > TransformedLatex.tex

原始答案:

您可以query_selector在特定元素上使用:

\documentclass{book}
\usepackage{luacode}

\begin{document}

\begin{luacode*}
local domobject = require "luaxml-domobject"
sample = [[
<datas>
  <arttitle>Quantum vacuum under mixed boundary conditions: the case for curved spacetime</arttitle>
  <table-wrap id="tab1" position="float">
  <label>Table 1ABC.</label>
  <caption id="t1"><p>The large <italic>x</italic> behavior for different <italic>w</italic>.</p></caption>
  <table>
  <colgroup>
  <col align="left"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  <col align="center"/>
  </colgroup>
  </table>
  <thead>
  <tr>
  <th>A1</th>
  <th>A2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>B1</td>
  <td>B2</td>
  </tr>
  </tbody>
  </table-wrap>
</datas>]]
local dom = domobject.parse(sample)


tex.sprint(dom:query_selector("arttitle")[1]:get_text())

tex.sprint("\\par")

local function get_columns(tbl)
  return #tbl:query_selector("col")
end
for i, tbl in ipairs(dom:query_selector("table")) do
  tex.sprint("Table: " .. i .. " has " .. get_columns(tbl) .. " columns")
end


\end{luacode*}
\end{document}

重要的部分是这样的:

local function get_columns(tbl)
  return #tbl:query_selector("col")
end
for i, tbl in ipairs(dom:query_selector("table")) do
  tex.sprint("Table: " .. i .. " has " .. get_columns(tbl) .. " columns")
end

它处理文档中的所有表格并打印#tbl:query_selector("col")每个表格计算的列数。

结果如下:

在此处输入图片描述

相关内容