答案1
建议使用expl3
:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableread{
A B
1 2
0 2
1 0
0 2
0 2
1 0
}\test
\pgfplotstablegetcolsof{\test}
\pgfmathtruncatemacro\colmax{\pgfplotsretval-1}
\usepackage{expl3}
\ExplSyntaxOn
\int_step_inline:nnnn
{0}{1}{\colmax}
{
\pgfplotstablegetcolumnnamebyindex{#1}\of\test\to\colname
\pgfplotstablemodifyeachcolumnelement{\colname}\of\test\as\cell
{\pgfmathparse{\cell==0?"\colname":\cell}\xdef\cell{\pgfmathresult}}
}
\ExplSyntaxOff
\begin{document}
\pgfplotstabletypeset[string type]{\test}
\end{document}
答案2
可能的:
\pgfplotsinvokeforeach{0,1} {%
\pgfplotstableset{
columns/#1/.style = {
string replace={0}{\pgfplotstablegetelem{0}{[index]#1}\of\test \pgfplotsretval}
}}}%
但这不是很好,因为我不得不说\of\test
,这并不适合一般的风格……
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotstableread[header=false]{
A B
1 2
0 2
1 0
0 2
0 2
1 0
}\test
\begin{document}
% Nullen ersetzen
\pgfplotsinvokeforeach{0,1} {%
\pgfplotstableset{
columns/#1/.style = {
string replace={0}{\pgfplotstablegetelem{0}{[index]#1}\of\test \pgfplotsretval}
}}}%
\pgfplotstabletypeset[string type,
skip rows between index={0}{1},% Benennungszeile ausblenden
every head row/.style={output empty row},% keinen header anzeigen
]{\test}
%Test: \pgfplotstablegetelem{0}{[index]0}\of\test \pgfplotsretval
%\pgfplotstablegetelem{0}{[index]1}\of\test \pgfplotsretval
\end{document}