我有一个这样的.csv 文件:
\begin{filecontents}{data.csv}
type,Description,Value
typA,Description0,Value0
typA,Description1,Value1
typB,Description2,Value2
typA,Description3,Value3
\end{filecontents}
根据type
列的内容,我想区分多列和单列。
在这个简单的 MWE 中,除了typB
提及的行之外,应该始终有单独的列。在这种情况下,应该只在行的中央打印说明,如图所示。
我觉得这和这个很相似https://tex.stackexchange.com/a/459848/104358问题,但就我而言,我不想对特定行进行硬编码。它们应该通过检查类型列的内容来动态计算。
我尝试借助来实现这一点,\pgfplotstablegetelem
但这似乎没有在环境中定义typeset cell/.code
。
\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}
答案1
我找到了一个解决方案来解决问题!也许这不是最好的,但它仍然有效!
诀窍是针对\pgfplotstablerow=-1
需要这种情况提出一个特殊情况,因为从(包括头行)\pgfplotstablerow
开始计数,但是从(头行后面)开始。-1
\pgfplotstablegetelem{}{}\of{\csvdata}
0
\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}
结果