I have a .csv file like this:
\begin{filecontents}{data.csv}
type,Description,Value
typA,Description0,Value0
typA,Description1,Value1
typB,Description2,Value2
typA,Description3,Value3
\end{filecontents}
Depending on the content of the type
column, I want to distinguish between multicolumn and single column.
In this simple MWE, there should always be single columns apart from the row where typB
is mentioned. In this case, just the Description should be printed in the center of the row, as shown in the image.
I think this is quit similar to this https://tex.stackexchange.com/a/459848/104358 question, but in my case I don't want to set the specific rows hard coded. They should be calculated on the fly by checking the content of the type column.
I tried to achieve this with the help of \pgfplotstablegetelem
but this seems not to be defined in the typeset cell/.code
environment.
\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\pgfplotsset{compat=newest}
\begin{filecontents}{data.csv}
type,Description,Value
typA,Description0,Value0
typA,Description1,Value1
typB,Description2,Value2
typA,Description3,Value3
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1}
\pgfplotstableset{
typeset cell/.code={%
%\pgfplotstablegetelem{\pgfplotstablerow}{type}\of{\csvdata}
%\ifthenelse{\equal{\pgfplotsretval}{typB}}{
% Equivalent to: \ifnum\pgfplotstablerow = 2
%}{
% Equivalent to: \ifnum\pgfplotstablerow != 2
%}
\ifnum\pgfplotstablerow=2 %=> This row depends on the content of column type!
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
\else%
\ifnum\pgfplotstablecol=2%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{3}{c}{#1}}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
\fi
\fi
\else%
\ifnum\pgfplotstablecol=\pgfplotstablecols
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
\else%
\pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
\fi
\fi
},
}
\begin{document}
\pgfplotstabletypeset[col sep = comma,
string type,
column type = l,
multicolumn names,
]{\csvdata}
\end{document}
\ifnum\coltest=1 <makemulticolumn> \else <makenormalcolumn> \fi
.\space
or\empty
to the columns to be ignored. I've always added new columns instead of changing the contents of old columns (which I htought was impossible). As for the test itself, try using\ifx
with two macro names (same catcode) or look at the xstring package.