Eu tenho um arquivo .csv como este:
\begin{filecontents}{data.csv}
type,Description,Value
typA,Description0,Value0
typA,Description1,Value1
typB,Description2,Value2
typA,Description3,Value3
\end{filecontents}
Dependendo do conteúdo da type
coluna, quero distinguir entre colunas múltiplas e colunas únicas.
Neste MWE simples, sempre deve haver colunas únicas além da linha onde typB
é mencionada. Neste caso, apenas a Descrição deverá ser impressa no centro da linha, conforme mostra a imagem.
Eu acho que isso é parecido com issohttps://tex.stackexchange.com/a/459848/104358pergunta, mas no meu caso não quero definir as linhas específicas codificadas. Eles devem ser calculados dinamicamente, verificando o conteúdo da coluna de tipo.
Tentei conseguir isso com a ajuda de \pgfplotstablegetelem
mas isso parece não estar definido no typeset cell/.code
ambiente.
\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}
Responder1
Encontrei uma solução para resolver meu problema! Talvez não seja o melhor, mas ainda funciona!
O truque era ter um caso especial para \pgfplotstablerow=-1
Isto é necessário, porque \pgfplotstablerow
começa a contar a partir de -1
(Linha da Cabeça incluída), mas \pgfplotstablegetelem{}{}\of{\csvdata}
começa a partir de 0
(Atrás da Linha da Cabeça).
\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}
RESULTADO