1

I use csvsimpl-l3 and tabularray packages to create tables from a CSV file. In the CSV file, some data are marked with "significant". The sign "|" pipe is used to separate data from the word "significant". Data modified in this way should be formatted differently in the output table compared to other data. Namely, the cell of the table should have a gray background, the data should be bold and colored in red. Of course, the output table should not show "significant|".

My example gives a partially good result because the \SetCell command does not work at all and does not format the table cell correctly.

I note that it is important for me to keep the use of the package csvsimple=l3 and tabulararray, as well as the command \SetCell.

If anyone knows how to solve this problem and can help, it would be of great help to me.

\documentclass{article}

\begin{filecontents}{test.csv} significant|a,b,c 1,significant|2,3 4,5,significant|6 \end{filecontents}

\usepackage{csvsimple-l3} \usepackage{tabularray} \usepackage{xcolor} \usepackage{xstring}

\NewDocumentCommand{\applystyle}{m}{% \IfSubStr{#1}{|}{% \expandafter\SetCell{fg=black!10,bg=red}\bfseries\applystylehelper#1\relax }{% \strut#1% }% }

\def\applystylehelper#1|#2\relax{% \helperstyle{#1}{#2}% }

\NewDocumentCommand{\helperstyle}{mm}{% \IfStrEq{#1}{significant}{% {\strut}#2% }{% \strut#2% }% }

\begin{document}

\csvreader[ no head, generic collected table = longtblr, generic table options = {[label = none]{ rowhead = 1, colspec = {|X[1,c]|X[1,c]|X[1,c]|}, width=0.5\textwidth }}, table head = \hline, late after line = \\hline ]{test.csv}{}{% \applystyle{\csvcoli} & \applystyle{\csvcolii} & \applystyle{\csvcoliii}% }%

\end{document}

enter image description here

1 Answers1

2

The problem seems to be that the tabularray package requires a specific expansion mechanism for \SetCell (that is, it has to be expanded at a very specific point in time) which is due to the way the tabularray package processses tabular material. In combination with processing via csvreader, it is not easy to adhere to the correct expansion order if you don't exactly know what should be expanded at which point in time.

However, you can make use of the command \ifcsvstrcmp provided by the csvsimple-l3 package. But you also need to watch after proper expansion when manipulating strings. Using the solution from this great answer and adding some custom commands for string manipulation, you could do the following:

\documentclass{article}

\begin{filecontents}{test.csv} significant|a,b,c 1,significant|2,3 4,5,significant|6 \end{filecontents}

\usepackage{csvsimple-l3} \usepackage{tabularray} \usepackage{xcolor}

\ExplSyntaxOn \cs_generate_variant:Nn \str_range:nnn { e } \NewExpandableDocumentCommand{\clipbehind}{ m }{ \str_range:enn { #1 } { 1 } { 11 } } \NewExpandableDocumentCommand{\clipbefore}{ m }{ \str_if_in:nnTF { #1 } { | } { \str_range:enn { #1 } { 13 } { -1 } } { #1 } } \ExplSyntaxOff

\begin{document}

\csvreader[ no head, generic collected table = longtblr, generic table options = {[ label = none ]{ rowhead = 1, colspec = {|X[1,c]|X[1,c]|X[1,c]|}, width=0.5\textwidth }}, table head = \hline, late after line = \\hline ]{test.csv}{}{ \ifcsvstrcmp{\clipbehind{\csvcoli}}{significant}{ \SetCell{fg=black!10, bg=red, font=\noexpand\bfseries} }{} \clipbefore{\csvcoli} & \ifcsvstrcmp{\clipbehind{\csvcolii}}{significant}{ \SetCell{fg=black!10, bg=red, font=\noexpand\bfseries} }{} \clipbefore{\csvcolii} & \ifcsvstrcmp{\clipbehind{\csvcoliii}}{significant}{ \SetCell{fg=black!10, bg=red, font=\noexpand\bfseries} }{} \clipbefore{\csvcoliii} }

\end{document}

enter image description here


This variant allows for math mode in cells:

\documentclass{article}

\begin{filecontents}{test.csv} significant|a,$+$,c 1,significant|2,significant|$\sim$ 4,$\frac{1}{2}$,significant|6 \end{filecontents}

\usepackage{csvsimple-l3} \usepackage{tabularray} \usepackage{xcolor}

\ExplSyntaxOn \cs_generate_variant:Nn \tl_range:nnn { e } \NewExpandableDocumentCommand{\clipbehind}{ m }{ \tl_range:enn { #1 } { 1 } { 11 } } \NewExpandableDocumentCommand{\clipbefore}{ m }{ \str_if_in:nnTF { #1 } { | } { \tl_range:enn { #1 } { 13 } { -1 } } { #1 } } \ExplSyntaxOff

\begin{document}

\csvreader[ no head, generic collected table = longtblr, generic table options = {[ label = none ]{ rowhead = 1, colspec = {|X[1,c]|X[1,c]|X[1,c]|}, width=0.5\textwidth }}, table head = \hline, late after line = \\hline ]{test.csv}{}{ \ifcsvstrcmp{\clipbehind{\csvcoli}}{significant}{ \SetCell{fg=black!10, bg=red, font=\noexpand\bfseries} }{} \clipbefore{\csvcoli} & \ifcsvstrcmp{\clipbehind{\csvcolii}}{significant}{ \SetCell{fg=black!10, bg=red, font=\noexpand\bfseries} }{} \clipbefore{\csvcolii} & \ifcsvstrcmp{\clipbehind{\csvcoliii}}{significant}{ \SetCell{fg=black!10, bg=red, font=\noexpand\bfseries} }{} \clipbefore{\csvcoliii} }

\end{document}

enter image description here

  • A few days ago you gave me an answer to my question and a suitable example. I noticed that when I use the mathematical environment and commands in a cell that requires special formatting, for example, $\sim$, instead of the executed command I get the text $“sim $. You can help fix this bug. – Zeljko Hrcek Nov 29 '23 at 18:24
  • @ZeljkoHrcek Hm, I edited the code a bit, but you won't be able to use any command in the cells this way. For example \emph{a} in a cell would probably break. – Jasper Habicht Nov 29 '23 at 18:45
  • Thank you for the update. For now, this solution covers all my requirements. – Zeljko Hrcek Nov 29 '23 at 19:20