pgfplotstable:使用一列来控制另一列的样式

pgfplotstable:使用一列来控制另一列的样式

我想打印一个 pgfplotstable,其中一列(未打印)用于更改另一列的样式。在下面的 MWE 中,如果列包含值,我想更改列font=中选择值的样式。但在我的实际用例中,我将更改许多属性。Itemhighlight1

我尝试过使用\getthisrowin preproc cell content/.code,但在这种情况下我收到“未定义控制序列”错误。我还能尝试什么?

其次,在中preproc cell content/.code我尝试font使用来更改\pgfplotstableset,但这似乎没有任何效果(否则“不是我想要的”表中的项目将全部变成橙色)。

渲染

\documentclass{standalone}

\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{color}

\begin{filecontents}{data.csv}
highlight,Item,Unrelated
1,Lorem,f
0,ipsum,o
0,dolor,o
1,sit,
0,amet,bar
\end{filecontents}

\begin{document}
\pgfplotstabletypeset[
    col sep=comma,
    columns={Item,Unrelated},
    columns/Item/.style={string type},
    columns/Unrelated/.style={string type},
    every head row/.style={before row={
        \textcolor{red}{Not what I want:}\\\toprule},
        after row=\midrule},
    every last row/.style={after row=\bottomrule},
    preproc cell content/.code={
        %\getthisrow{highlight}\highlight % <--Undefined control sequence
        %use \highlight here to set pgf value
        \pgfplotstableset{font=\color{orange}} % <--change styling for current cell (has no effect)
    },
]{data.csv}

\pgfplotstabletypeset[
    col sep=comma,
    columns={Item,Unrelated},
    columns/Item/.style={string type},
    columns/Unrelated/.style={string type},
    every head row/.style={before row={
        \textcolor{green}{Desired result:}\\\toprule},
        after row=\midrule},
    every last row/.style={after row=\bottomrule},
]{
highlight,Item,Unrelated
1,\textcolor{orange}{Lorem},f
%color should be set by pgf key, not \textcolor
0,ipsum,o
0,dolor,o
1,\textcolor{orange}{sit},
%color should be set by pgf key, not \textcolor
0,amet,bar
}
\end{document}

答案1

不管您的代码是否有效,您首先需要访问感兴趣的列,因此您拥有的代码与该列无关,Item而是设置一个通用的列。\getthisrow正如您所指出的,在该上下文中也没有定义。

我使用了一种相当慢的方式来达到正确的值,并将\pgfplotstablegetelem其用于条件。我还使用了后处理,因为条件只会改变表格的外观,而不会改变表格的任何数据。

\documentclass{standalone}

%\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{booktabs}
%\usepackage{colortbl}
%\usepackage{color} %No need for this. TikZ family uses the better "xcolor"

\pgfplotstableread[col sep=comma]{
highlight,Item,Unrelated
1,Lorem,f
0,ipsum,o
0,dolor,o
1,sit,
0,amet,bar
}\mytable

\begin{document}
\pgfplotstabletypeset[
    columns={Item,Unrelated},
    columns/Item/.style={string type,
        postproc cell content/.prefix code={%
            \pgfplotstablegetelem{\pgfplotstablerow}{highlight}\of\mytable%Get the boolean
            \ifnum\pgfplotsretval>0\relax%  If boolean is 1
            \begingroup\edef\temp{\endgroup\noexpand%  Start a dummy expanded variable
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\noexpand\color{orange}%Set the content
            \pgfkeysvalueof{/pgfplots/table/@preprocessed cell content}% with the orange version
            }}\temp% End definition and invoke the defined macro for expansion
            \fi% End of conditional
        }
    },
    columns/Unrelated/.style={string type},
    every head row/.style={before row={\textcolor{red}{Also what I want:}\\\toprule},after row=\midrule},
    every last row/.style={after row=\bottomrule}
]\mytable

\pgfplotstabletypeset[
    col sep=comma,
    columns={Item,Unrelated},
    columns/Item/.style={string type},
    columns/Unrelated/.style={string type},
    every head row/.style={before row={
        \textcolor{green}{Desired result:}\\\toprule},
        after row=\midrule},
    every last row/.style={after row=\bottomrule},
]{
highlight,Item,Unrelated
1,\textcolor{orange}{Lorem},f
%color should be set by pgf key, not \textcolor
0,ipsum,o
0,dolor,o
1,\textcolor{orange}{sit},
%color should be set by pgf key, not \textcolor
0,amet,bar
}
\end{document}

在此处输入图片描述

相关内容