使用输入文件时动态着色表格元素

使用输入文件时动态着色表格元素

我喜欢从文件创建一个表(例子)并动态着色各个元素(例子)。我没有被 pgfsplottable 所束缚,甚至没有分享一些可以产生所需效果的宏组合。

在下面的 MWE 中(谢谢!),我希望将大于 0.8 的任何单元格都设为绿色。我不想对表格进行硬编码,因为输入文件可能会更改。

\documentclass{scrreprt}
\usepackage{pgfplotstable,booktabs}
% I did it to create your file 
\usepackage{filecontents} % <-- To create files on the fly

\begin{filecontents*}{avg_value.csv}
,M23,F231,M36,F44,F63,M70
M23,1.00,0.81,0.88,0.83,0.41,0.82
F231,,1.00,0.52,0.56,0.25,0.94
M36,,,1.00,0.62,0.94,0.96
F44,,,,1.00,0.43,0.23
F63,,,,,1.00,0.22
M70,,,,,,1.00
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfplotstabletypeset[
col sep = comma,
string replace*={_}{\textsubscript},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
display columns/0/.style={string type,column name={}}
]
{avg_value.csv}

\end{document}

MWE 表

答案1

这是一个很好的练习机会assign cell content

\documentclass{scrreprt}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable,booktabs}
% I did it to create your file 
\usepackage{filecontents} % <-- To create files on the fly

\begin{filecontents*}{avg_value.csv}
,M23,F231,M36,F44,F63,M70
M23,1.00,0.81,0.88,0.83,0.41,0.82
F231,,1.00,0.52,0.56,0.25,0.94
M36,,,1.00,0.62,0.94,0.96
F44,,,,1.00,0.43,0.23
F63,,,,,1.00,0.22
M70,,,,,,1.00
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfplotstabletypeset[
    col sep = comma,
    string replace*={_}{\textsubscript},
    every head row/.style={before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule},
    display columns/0/.style={string type,column name={}},
    assign cell content/.code={
        \edef\tempcellcontent{#1}
        \ifx\tempcellcontent\empty
            \pgfkeyslet{/pgfplots/table/@cell content}{\tempcellcontent}
            %\message{^^J^^J#1}
        \else
            \ifdim#1pt>0.8pt
                \pgfmathparse{#1*400-300}
                \edef\tempcellcontent{\noexpand\cellcolor{green!\pgfmathresult!}#1}
                \pgfkeyslet{/pgfplots/table/@cell content}{\tempcellcontent}
            \else
                \edef\tempcellcontent{#1}
                \pgfkeyslet{/pgfplots/table/@cell content}{\tempcellcontent}
            %   \pgfkeyssetvalue{/pgfplots/table/@cell content}{#1}
            \fi
        \fi
    },
]
{avg_value.csv}

\end{document}

相关内容