2

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}

enter image description here

10
  • Ui, it's been a long time and I don't know if I understand your question 100%. But, if we assume A=0 and B=1; then you could perhaps add something like \ifnum\coltest=1 <makemulticolumn> \else <makenormalcolumn> \fi.
    – cis
    Commented Apr 29 at 22:18
  • Sorry I did not get it… what is ‘\coltest‘ in this example? I don‘t know such a variable in ‘pgfplotstable‘
    – Pascal
    Commented Apr 29 at 22:45
  • Instead of trying to change the number of columns, just add \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. Commented Apr 30 at 1:11
  • I‘m not sure if I got you correctly, but the problem is, that I can not distinguish between a row with typeA and typeB. The rest of the code works. So I just search for a solution to get the current value of type column in „thisrow“ and then execute statement 1 or statement 2.
    – Pascal
    Commented Apr 30 at 5:50
  • 2
    @PascalS The message is automatic, I only linked the two questions because they are the same. It doesn't change anything to you, but it can be useful to other users.
    – CarLaTeX
    Commented May 4 at 19:23

1 Answer 1

3

I found a solution to solve my problem! Maybe it is not the best one, but it still works!

The trick was to have a special case for \pgfplotstablerow=-1 This is needed, because \pgfplotstablerow starts counting from -1 (Head Row included), but \pgfplotstablegetelem{}{}\of{\csvdata} starts from 0 (Behind the Head Row).

\documentclass{standalone}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\pgfplotsset{compat=newest}

\begin{filecontents}{data.csv}
Type,Description,Value
typA,descriptionA0,value0
typB,descriptionA1,value1
typA,descriptionB2,value2
\end{filecontents}
\pgfplotstableread[col sep=comma]{data.csv}{\csvdata}
\pgfplotstablegetrowsof{\csvdata}
\pgfmathtruncatemacro\CSVDataRows{\pgfplotsretval-1} 

\newcounter{endRowCounter}

\pgfplotstableset{
    col sep=comma,
    string type,
    typeset cell/.code={%
        \setcounter{endRowCounter}{\pgfplotstablerows}
        \addtocounter{endRowCounter}{1}

        \ifthenelse{\pgfplotstablerow=-1}{
            \ifnum\pgfplotstablecol=\pgfplotstablecols
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
            \else
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}
            \fi
        }{
            \ifthenelse{\pgfplotstablerow<\value{endRowCounter}}{
                \pgfplotstablegetelem{\pgfplotstablerow}{Type}\of{\csvdata}
                \ifthenelse{\equal{\pgfplotsretval}{typB}}{
                    \ifnum\pgfplotstablecol=\pgfplotstablecols
                        \pgfkeyssetvalue{/pgfplots/table/@cell content}{\\}%
                    \else
                        \ifnum\pgfplotstablecol=1
                            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\multicolumn{3}{c}{#1}}%
                        \else
                            \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                        \fi
                    \fi
                }{
                    \ifnum\pgfplotstablecol=\pgfplotstablecols
                        \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1\\}%
                    \else
                        \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1 &}%
                    \fi
                }
            }{}
        }
    },
}

\begin{document}
\pgfplotstabletypeset{\csvdata}
\end{document}

RESULT

enter image description here

2
  • 1
    Fine :) // I suggest using package booktabs to include \toprule, \bottomrule, and several \midrule s in said table, e.g. to let the row with typB stand out better, e.g. assist our eyes to grasp what you meant.
    – MS-SPO
    Commented May 1 at 11:09
  • 1
    Absolutely, I did this but I removed it for the MWE 👍
    – Pascal
    Commented May 1 at 14:17

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