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 }

?

Hotschke
  • 5,300
  • 5
  • 33
  • 63
  • 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. –  Aug 16 '22 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 '22 at 16:11

1 Answers1

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}

egreg
  • 1,121,712
  • To simplify it, you could even replace \trivgroups with \py{mystr} –  Aug 16 '22 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 '22 at 08:41
  • @Hotschke Expansion is complicated with tabularray. – egreg Aug 17 '22 at 08:46
  • Should I use something else? I liked the user interface of tabularray. I want to create a matrix similar to this one https://commons.wikimedia.org/wiki/File:Z2%5E4;_Lattice_of_subgroups_Hasse_diagram_adjacency_matrix.svg 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 '22 at 08: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 '22 at 08:53
  • I am confused: the added image to the question contains rotated text in the header row. The strings G <N> Very Long are rotated. Do you really not see it? – Hotschke Aug 17 '22 at 08:56
  • @Hotschke Oh, the image in the question, not the one at Wikimedia! Try with \noexpand\rothead. – egreg Aug 17 '22 at 08:58
  • @egreg Yes, that did. It's easier than one expects. The LaTeX error message was misleading. But knowing the context what the new env does, the fix seems very obvious. Thanks for all the help. Do you have a preferred tool to create a large matrix like the one on wikimedia but with LaTeX labels in header rows/columns. The author of tabularray already said that from a performance view one should look for something else. – Hotschke Aug 17 '22 at 09:05