XML 中的表 CHAR 对齐

XML 中的表 CHAR 对齐

我已经根据此示例使用 XML 表成功在 LuaLaTeX 中加载了 XML 计算 XML 中的列数

我需要tabletbody而不是 的对齐thead。如何定义alignment with char

\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>
  <thead valign="top">
  <tr>
  <th align="center">Case n&#xb0;</th>
  <th align="center">1</th>
  <th align="center">2</th>
  <th align="center">3</th>
  <th align="center">4</th>
  <th align="center">5</th>
  </tr>
  </thead>
  <tbody valign="top">
  <tr>
  <td align="left">Sl. No</td>
  <td align="char" char=".">0.1</td>
  <td align="char" char=".">0.1</td>
  <td align="char" char=".">0.1</td>
  <td align="center">100</td>
  <td align="char" char=".">1.0</td>
  </tr>
  <tr>
  <td>B1</td>
  <td>B2</td>
  <td>B3</td>
  <td>B4</td>
  <td>B5</td>
  <td>B6</td>
  </tr>
  </tbody>
  </table-wrap>
</datas>]]
\end{luacode*}
\end{document}

答案1

尝试这个:

 \documentclass{book}
\usepackage{luacode}
\usepackage{siunitx}

\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>
  <thead valign="top">
  <tr>
  <th align="center">Case n&#xb0;</th>
  <th align="center">1</th>
  <th align="center">2</th>
  <th align="center">3</th>
  <th align="center">4</th>
  <th align="center">5</th>
  </tr>
  </thead>
  <tbody valign="top">
  <tr>
  <td align="left">Sl. No</td>
  <td align="char" char=".">0.1</td>
  <td align="char" char=".">0.1</td>
  <td align="char" char=".">0.1</td>
  <td align="center">100</td>
  <td align="char" char=".">1.0</td>
  </tr>
  <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[align='left']", "& \\multicolumn{1}{l}{%s}")
transform.add_action("td[align='left']:first-child", "\\multicolumn{1}{l}{%s}")
transform.add_action("td[align='char'][char='.']", "& \\multicolumn{1}{S}{%s}")
transform.add_action("td[align='char'][char='.']:first-child", "\\multicolumn{1}{S}{%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("th")) 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}

它重用了我以前的答案中的代码,只有新代码是这样的:

transform.add_action("td[align='left']", "& \\multicolumn{1}{l}{%s}")
transform.add_action("td[align='left']:first-child", "\\multicolumn{1}{l}{%s}")
transform.add_action("td[align='char'][char='.']", "& \\multicolumn{1}{S}{%s}")
transform.add_action("td[align='char'][char='.']:first-child", "\\multicolumn{1}{S}{%s}")

<td>它为具有属性的元素设置操作align。我们为每个选择器复制代码并使用:first-child伪类。这对于处理字符是必要的&,否则您将收到有关伪&字符的运行时 LaTeX 错误。

它使用包提供的功能siunitx,即支持按小数点对齐。不幸的是,它只有在tabular声明中指定时才能正常工作,它似乎对单个表格单元格没有任何影响,这是可以理解的。

结果如下:

在此处输入图片描述

相关内容