1

I'm unable to get alternate row colors to display for any of my tables inside sections. What am I missing? My MWE for a sample section: 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}

graticule.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*}

data.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"
            }
        ]
    }
}

and json.lua file that parses the JSON is from http://regex.info/blog/lua/json

this is the result: enter image description here

2
  • 1
    Please remove all lua code and write TeX code directly for a table. Row colors have nothing to do with lua code.
    – L.J.R.
    Commented Aug 2, 2021 at 15:30
  • I'm reading the table entries from a json file and hence the need for using Lua. But row colors dont alternate in the table when using \rowcolor{lightgray!50}
    – 24suns
    Commented Aug 2, 2021 at 16:42

2 Answers 2

1

It is easier to make alternating row colors with the new LaTeX3 package tabularray. Also your code line

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

should be

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

Here is the revised complete tex code (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}

enter image description here

5
  • I see an issue with the table borders disappearing at certain zoom levels when viewing the generated PDF
    – 24suns
    Commented Aug 3, 2021 at 16:52
  • @24suns Take it easy. When you print the document, the borders are always there. It is the fault of the PDF viewer.
    – L.J.R.
    Commented Aug 3, 2021 at 17:34
  • right....but the report i'm working with is usually viewed with a built in PDF reader and so is preferable if table borders show.
    – 24suns
    Commented Aug 5, 2021 at 19:31
  • Then you may increase the line width by changing hlines to hlines={2pt} in the above code.
    – L.J.R.
    Commented Aug 5, 2021 at 21:58
  • 1
    @24suns With version 2021N, the problem of disappeared borders in some PDF readers with tblr environment has been solved.
    – L.J.R.
    Commented Sep 2, 2021 at 7:24
0

With {NiceTabular} of 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}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

1
  • I didnt get a chance to test this yet but thanks for pointing me to this.
    – 24suns
    Commented Sep 21, 2021 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .