1

I have successfully loaded XML in LuaLaTeX with XML-Tables based on this example Count Number of Columns in XML.

I have need the table alignments from tbody instead of thead. And how to define 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}

Balaji
  • 2,282
  • I don't think you can change column alignment in this way. You can use siunitx's S colument to align table column to dot, but it doesn't work with this method: https://tex.stackexchange.com/a/47677/2891. – michal.h21 Aug 27 '21 at 16:41
  • @michal.h21: How to find attribute with value in table alignments. Example: align="char" char="." and find in tbody instead of thead. I had tried tbody tr[1] it's not working. – Balaji Aug 27 '21 at 16:49
  • you can find them with td[align='char'], but you will not be able to change the alignment in the LaTeX table cell – michal.h21 Aug 27 '21 at 17:11

1 Answers1

1

Try this:

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

It reuses code from my previous answer, only new code is this:

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}")

It sets actions for <td> elements with align attribute. We duplicate code for each selector and use the:first-child pseudo class. This is necessary to handle & characters, you would get runtime LaTeX error about spurious & character otherwise.

It uses feature provided by the siunitx package, namely that it supports alignment by decimal point. Unfortunately, it works correctly only when specified in tabular declaration, it doesn't seem to have any effect on individual table cells, which is understandable.

This is the result:

enter image description here

michal.h21
  • 50,697
  • How to get the continued IDs? Example: <xref ref-type="bibr" rid="B2">2</xref>&#x02013;<xref ref-type="bibr" rid="B5">5</xref>. I have need to convert `\citet{b2,b3,b4,b5}? Any help greatly appreciate... – Balaji Jul 22 '23 at 15:10