4

Minimal working example:

%! TeX program = lualatex
\documentclass{article}
\usepackage{pyluatex}
\usepackage{tabularray}
\usepackage{xcolor} 

\begin{document}

\let\rows\relax
\begin{python}
print("\\global\\def\\rows{Alpha & Beta & Gamma & Delta \\\\  Epsilon & Zeta  & Eta & Theta \\\\ Iota & Kappa & Lambda  & Mu \\\\  Nu & Xi & Omicron & Pi \\\\}")
\end{python}

\let\trivgroups\relax
\begin{python}
mystr = ""
for i in range(1,5):
  mystr += 'cell{'+str(i)+'}{'+str(i)+'} = {red4},'
print("\\global\\def\\trivgroups{"+mystr+"}")
\end{python}

\ExplSyntaxOff
% This works
% \begin{tblr}[expand=\rows]{colspec={|c|c|c|c|},
%     cell{1}{1}={red4},
%     cell{2}{2}={red4},
%     cell{3}{3}={red4},
%     cell{4}{4}={red4}
% }
\begin{tblr}[expand=\rows]{colspec={|c|c|c|c|},\trivgroups}
\rows
\end{tblr}

\end{document}

Compilation still works

> lualatex --output-format=pdf --shell-escape pythontex-tabularray.tex

However, no colour in the output:

enter image description here

The commented version gives

enter image description here

Follow-up question of Generate rows for tabularray with pyluatex with possible related questions:

UPDATE

Before I used the new environment exptblr from the answer by egreg, I could rotate the text in the header row as following:

\documentclass{article}
\usepackage{pyluatex}
\usepackage{tabularray}
\usepackage{xcolor}

\usepackage{rotating} 
\usepackage{makecell} 
\UseTblrLibrary{diagbox}
\setlength\rotheadsize{5cm}
\renewcommand\theadfont{}

\ExplSyntaxOn
\NewDocumentEnvironment{exptblr}{O{}m}
 {
  \use:x
   {
    \exp_not:N \begin{tblr}
    [\exp_not:n{#1}]
    {#2}
   }
 }
 {
  \end{tblr}
 }
\ExplSyntaxOff

\begin{document}

\let\rows\relax
\begin{python}
print("\\global\\def\\rows{Alpha & Beta & Gamma & Delta \\\\ Epsilon & Zeta  & Eta & Theta \\\\ Iota & Kappa & Lambda  & Mu \\\\ Nu & Xi & Omicron & Pi \\\\}")
\end{python}

\begin{python}
mystr = ""
for i in range(2,5):
  mystr += 'cell{'+str(i)+'}{'+str(i)+'} = {red4},'
\end{python}

\begin{tblr}[expand=\rows]{
  colspec={|c|c|c|c|},
  cell{1}{2-Z} = {halign=c,cmd=\rothead},
  hlines,vlines,
  % \py{mystr},
}
\diagbox[height=4cm,width=4cm]{subgroups $H$}{supergroups $G$} &
G1 Very Long & G2 Very Long & G3 Very Long \\
\rows
\end{tblr}

\end{document}

enter image description here

But when I now use the new environment exptblr, I get following error:

! Use of \@gape doesn't match its definition.
\@ifnextchar ...eserved@d =#1\def \reserved@a {#2}
                                                  \def \reserved@b {#3}\futu...

l.45 }

?
2
  • Unrelated: You can omit \let\command\relax. I guess it's a PythonTeX pattern. PyLuaTeX doesn't need it because it does everything in a single run.
    – user263192
    Aug 16, 2022 at 19:51
  • 2
    Ah this is an issue with fragile macro I think. In any case LJR has the functional package which aims to simplify expansion issues (personal opinion: I think the idea is good, but I don't like the implementation & the user manual currently does a not-very-good job teaching non-expert users I think.) Proper use of evalWhole and tlUse should work here.
    – user202729
    Aug 17, 2022 at 16:11

1 Answer 1

3

tblr doesn't expand macros in its mandatory argument.

You can define a new environment:

\documentclass{article}
\usepackage{pyluatex}
\usepackage{tabularray}
\usepackage{xcolor}

\ExplSyntaxOn
\NewDocumentEnvironment{exptblr}{O{}m}
 {
  \use:x
   {
    \exp_not:N \begin{tblr}
    [\exp_not:n{#1}]
    {#2}
   }
 }
 {
  \end{tblr}
 }
\ExplSyntaxOff

\begin{document}

\let\rows\relax
\begin{python}
print("\\global\\def\\rows{Alpha & Beta & Gamma & Delta \\\\ Epsilon & Zeta  & Eta & Theta \\\\ Iota & Kappa & Lambda  & Mu \\\\ Nu & Xi & Omicron & Pi \\\\}")
\end{python}

\let\trivgroups\relax
\begin{python}
mystr = ""
for i in range(1,5):
  mystr += 'cell{'+str(i)+'}{'+str(i)+'} = {red4},'
print("\\global\\def\\trivgroups{"+mystr+"}")
\end{python}

\begin{exptblr}[expand=\rows]{colspec={|c|c|c|c|},\trivgroups}
\rows
\end{exptblr}

\end{document}

enter image description here

As user187803 points out in comments this can be simplified with \py{mystr}. I added also a couple of changes: \let\rows\relax is not needed and \global\def can be \gdef.

\documentclass{article}
\usepackage{pyluatex}
\usepackage{tabularray}
\usepackage{xcolor}

\ExplSyntaxOn
\NewDocumentEnvironment{exptblr}{O{}m}
 {
  \use:x
   {
    \exp_not:N \begin{tblr}
    [\exp_not:n{#1}]
    {#2}
   }
 }
 {
  \end{tblr}
 }
\ExplSyntaxOff

\begin{document}

\begin{python}
print("\\gdef\\rows{Alpha & Beta & Gamma & Delta \\\\ Epsilon & Zeta  & Eta & Theta \\\\ Iota & Kappa & Lambda  & Mu \\\\ Nu & Xi & Omicron & Pi \\\\}")
\end{python}

\begin{python}
mystr = ""
for i in range(1,5):
  mystr += 'cell{'+str(i)+'}{'+str(i)+'} = {red4},'
\end{python}

\begin{exptblr}[expand=\rows]{colspec={|c|c|c|c|},\py{mystr}}
\rows
\end{exptblr}

\end{document}
8
  • To simplify it, you could even replace \trivgroups with \py{mystr}
    – user263192
    Aug 16, 2022 at 20:23
  • @egreg: Thank you for your answer. However, I encountered now an issue when using \rothead for the cells in the header row. Expansion in LaTeX feels very complicated (even with LaTeX 3 which tries to simplify it). Can you have a look at the added example in my question?
    – Hotschke
    Aug 17, 2022 at 8:41
  • @Hotschke Expansion is complicated with tabularray.
    – egreg
    Aug 17, 2022 at 8:46
  • Should I use something else? I liked the user interface of tabularray. I want to create a matrix similar to this one commons.wikimedia.org/wiki/… from a graphml. The python code is finished. I only need a suitable tool to create a large matrix with row and column headers typeset in LaTeX. Would you recommend tikz-matrix?
    – Hotschke
    Aug 17, 2022 at 8:50
  • @Hotschke Without seeing the problematic table, I can't help much. And your present question seems not to address the real problem; but from the image I cannot see any usage of \rothead.
    – egreg
    Aug 17, 2022 at 8:53

You must log in to answer this question.

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