pgfplotstable - 使用两列的值将行加粗并添加背景颜色

pgfplotstable - 使用两列的值将行加粗并添加背景颜色

我正在尝试让数据中的某些行既显示为粗体又显示为背景色。更具体地说,我希望我的表格中 Group = 1 的行显示为粗体,同时我希望 Color = 1 的行显示为蓝色背景。在此过程中,我不想丢失任何列的精度。

我尝试了几种组合,但无法解决这个问题。下面发布了一个 MWE:

\documentclass[]{report}

\usepackage{pgfplotstable,booktabs, ifthen}
\usepackage{colortbl}

\pgfplotsset{compat=1.14}

\begin{document}

\pgfplotstableread[col sep= semicolon]{
A;B;C;Group;Color 
C0;100;0.9;0;1
C1;90;16.0;1;1
C2;80;1.6;2;0
C3;70;1.0;0;1
C4;60;12.0;0;1
C5;50;13.5;1;0
}\mytable

\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill, precision=0, set thousands separator={}} 

\pgfplotstabletypeset[
    col sep = semicolon,
    columns = {A, B, C, Group, Color },
    columns/A/.style={ string type, column type = {l}},
    columns/B/.style={ column type = {r}},
    columns/C/.style={ column type = {r}, precision = 1},
    columns/Group/.style={ column type = {r}, precision = 1},
    columns/Color/.style={ column type = {r}, precision = 1},
    %
    every column/.style={
        postproc cell content/.append code={
            \pgfplotstablegetelem{\pgfplotstablerow}{Group}\of{\mytable}
            \ifthenelse{ \pgfplotsretval = 1 }
                {\pgfkeysalso{/pgfplots/table/@cell content/.add={$\bf}{$}}}
            %
            % Want to add red color to the rows where Color = 1, and keep the rows bold where Group = 1. 
            %
            % Commented out: My attempt that did not work!
            %
            % \pgfplotstablegetelem{\pgfplotstablerow}{Color}\of{\mytable}
            % \ifthenelse{ \pgfplotsretval = 1 }
            %    {\pgfkeyssetvalue{/pgfplots/table/@cell content}{\relax\cellcolor{red}##1}} 
            %
        }
    }
    ]{\mytable}

\end{document}

答案1

  • 顺便说一句:我认为,令人惊讶的是,没有命令every column/.style={...}
    €dit: ...in older versions of pgfplotstable. But, it does not matter, because we do not need this command in the following.

  • 看来,由于您的列样式不同,您必须为每个相关列重复您的 if 条件;同时尊重(和添加)当前列的特定样式。

您想要的常见风格是

\pgfplotstableset{
CellColor/.style={postproc cell content/.append code={
\pgfplotstablegetelem{\pgfplotstablerow}{Color}\of{\mytable}%
\ifthenelse{\pgfplotsretval = 1 }% if 
{\pgfkeysalso{/pgfplots/table/@cell content/.add={\cellcolor{red}}{}}}% then
{}% else
}},
CellBold/.style={postproc cell content/.append code={
\pgfplotstablegetelem{\pgfplotstablerow}{Group}\of{\mytable}%
\ifthenelse{\pgfplotsretval = 1 }% if 
{\pgfkeysalso{/pgfplots/table/@cell content/.add={$\bf}{$} }}% then
{}% else
}},
}

因此你必须说:
columns/A/.style={ string type, column type = {l},% specific column A CellColor, CellBold % common }
B 列的类比,但是
columns/C/.style={column type=r, precision = 1,% specific column C CellColor, CellBold % common }

全部一起:

在此处输入图片描述

\documentclass[border=5pt, varwidth]{standalone}
%\documentclass[]{report}

\usepackage{pgfplotstable,booktabs, ifthen}
\usepackage{colortbl}
\pgfplotsset{compat=1.13}

\begin{document}
\pgfplotstableread[col sep= semicolon]{
A;B;C;Group;Color 
C0;100;0.9;0;1
C1;90;16.0;1;1
C2;80;1.6;2;0
C3;70;1.0;0;1
C4;60;12.0;0;1
C5;50;13.5;1;0
}\mytable

\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill, precision=0, set thousands separator={}} 

\pgfplotstableset{
CellColor/.style={postproc cell content/.append code={
\pgfplotstablegetelem{\pgfplotstablerow}{Color}\of{\mytable}%
\ifthenelse{\pgfplotsretval = 1 }% if 
{\pgfkeysalso{/pgfplots/table/@cell content/.add={\cellcolor{red}}{}}}% then
{}% else
}},
CellBold/.style={postproc cell content/.append code={
\pgfplotstablegetelem{\pgfplotstablerow}{Group}\of{\mytable}%
\ifthenelse{\pgfplotsretval = 1 }% if 
{\pgfkeysalso{/pgfplots/table/@cell content/.add={$\bf}{$} }}% then
{}% else
}},
}


\pgfplotstabletypeset[, 
    col sep = semicolon,
    columns = {A, B, C, Group, Color },
%    columns/A/.style={ string type, column type = {l}},
%    columns/B/.style={ column type = {r}},
%    columns/C/.style={ column type = {r}, precision = 1},
    columns/Group/.style={ column type = {r}, precision = 1},
    columns/Color/.style={ column type = {r}, precision = 1},
%
%every column/.style={} % 
%
columns/A/.style={ string type, column type = {l},% specific column A
CellColor, CellBold % common
},
columns/B/.style={column type=r,% specific column B
CellColor, CellBold % common
},
columns/C/.style={column type=r, precision = 1,% specific column C
CellColor, CellBold % common
},
]{\mytable}

\end{document}

相关内容