Tabularx 交替行颜色不起作用

Tabularx 交替行颜色不起作用

我无法让任何表格中的部分显示替代行颜色。我遗漏了什么?我的 MWE 示例部分:main.tex

\documentclass{article}
\usepackage[document]{ragged2e}
\usepackage[nohead,paperheight=11.0in,paperwidth=8.5in,left=0.5in,right=0.5in,top=0.5in,bottom=1.0in]{geometry}

\usepackage{fontspec}
\usepackage{datatool}
\usepackage{longtable}
\usepackage{tabularx}
\usepackage{amsmath}

\usepackage{hhline}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{color, colortbl}
\usepackage[table]{xcolor}

\usepackage{etoolbox}
\usepackage{array}
\usepackage{luacode}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage[compact]{titlesec}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}{\thesection}{1em}{}
\renewcommand{\familydefault}{\sfdefault}

\renewcommand{\arraystretch}{1.5}
\titlespacing*{\section}{0pt}{*0}{*0}
\font\myfont=cmr12 at 16pt


\begin{document}
\centering
\normalfont\Large\bfseries\filcenter{HEADER\\ }

\input{graticule}

\end{document}

经纬网.tex

\section*{}
\newcolumntype{L}{|>{\raggedright\arraybackslash\bfseries}X|X}
\catcode10=9\

\definecolor{tableShade}{gray}{0.9}

\begin{luacode*}
local json = require("json")
local file = io.open("data.json")
tab = json.parse(file:read("*all"))
file:close()


tex.sprint(
    [[\begin{table}[ht!]
    \centering
    \begin{tabularx}{\textwidth}{@{} LL @{}}
    \rowcolor{lightgray!50}
    \hline]])
    for k, v in ipairs(tab["Graticule"]["Header"]) do
        tex.sprint(
        [[\textbf]],
        v.key, [[& ]],
        v.val, [[\\]],
        [[\hhline{--}]])
    end  
tex.sprint(
    [[\hhline{--}
    \end{tabularx}
    \end{table}]])

\end{luacode*}

数据.json

{
    "Graticule": {
        "title":"ZONE 29",
        "Header": [
            {
                "key": "A",
                "val": "a"
            },
            {
                "key": "C",
                "val": "d"
            },
            {
                "key": "D",
                "val": "e"
            },
            {
                "key": "F",
                "val": "f"
            },
            {
                "key": "G",
                "val": "h"
            }
        ]
    }
}

解析 JSON 的 json.lua 文件来自http://regex.info/blog/lua/json

结果如下: 在此处输入图片描述

答案1

使用新的 LaTeX3 包可以更轻松地制作交替行颜色tabularray. 还有你的代码行

tab = json.parse(file:read("*all"))

应该

tab = json:decode(file:read("*all"))

以下是修改后的完整 tex 代码 ( main.tex+ graticule.tex):

\documentclass{article}
\usepackage[document]{ragged2e}
\usepackage[nohead,paperheight=11.0in,paperwidth=8.5in,left=0.5in,right=0.5in,top=0.5in,bottom=1.0in]{geometry}

\usepackage{fontspec}
\usepackage{amsmath}

\usepackage{tabularray}
\usepackage{xcolor}

\usepackage{etoolbox}
\usepackage{luacode}
\usepackage{graphicx}
\usepackage[compact]{titlesec}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}{\thesection}{1em}{}
\renewcommand{\familydefault}{\sfdefault}

\titlespacing*{\section}{0pt}{*0}{*0}

\begin{document}
\centering
\normalfont\Large\bfseries\filcenter{HEADER\\ }

%\input{graticule}

\section*{}
\catcode10=9\relax

\begin{luacode*}
local json = require("json")
local file = io.open("data.json")
tab = json:decode(file:read("*all"))
file:close()

tex.sprint(
    [[\begin{table}[ht!]
    \centering
    \begin{tblr}{
      colspec={@{}X[font=\bfseries]|X@{}}, hlines,
      row{odd}={azure9}, row{even}={gray9},
    }]])
    for k, v in ipairs(tab["Graticule"]["Header"]) do
        tex.sprint(v.key, [[& ]], v.val, [[\\]])
    end  
tex.sprint(
    [[\end{tblr}
    \end{table}]])

\end{luacode*}

\end{document}

在此处输入图片描述

答案2

与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage[nohead,paperheight=11.0in,paperwidth=8.5in,left=0.5in,right=0.5in,top=0.5in,bottom=1.0in]{geometry}

\usepackage{fontspec}
\usepackage{amsmath}

\usepackage{ninecolors}
\usepackage{nicematrix}
\usepackage{xcolor}

\usepackage{etoolbox}
\usepackage{luacode}
\usepackage{graphicx}
\usepackage[compact]{titlesec}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}{\thesection}{1em}{}
\renewcommand{\familydefault}{\sfdefault}

\titlespacing*{\section}{0pt}{*0}{*0}

\begin{document}
\centering
\normalfont\Large\bfseries\filcenter{HEADER\\ }


\catcode10=9\relax

\begin{luacode*}
local json = require("json")
local file = io.open("data.json")
tab = json:decode(file:read("*all"))
file:close()

tex.sprint(
    [[\begin{table}[ht!]
    \centering
    \begin{NiceTabular}{@{}>{\bfseries}X|X@{}}[hlines]
    \CodeBefore
      \rowcolors{1}{azure9}{gray9}
    \Body
    ]])
    for k, v in ipairs(tab["Graticule"]["Header"]) do
        tex.sprint(v.key, [[& ]], v.val, [[\\]])
    end  
tex.sprint(
    [[\end{NiceTabular}
    \end{table}]])

\end{luacode*}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

相关内容