如果第 1 列不等于第 2 列,则条件突出显示 pgfplots 表中的整行

如果第 1 列不等于第 2 列,则条件突出显示 pgfplots 表中的整行

如果第 1 列不等于第 2 列,我想用红色突出显示整行并更改文本的颜色。我正在使用带有 longtable 的 pgfplotstable。

在此示例中,应突出显示第 3、11、16、17 和 40 行。

我看到如果你输入以下命令:

postproc cell content/.append code={\ifnum\numexpr\pgfplotstablerow+1=\pgfplotstablerows %
\pgfkeys{/pgfplots/table/@cell content/.add={\cellcolor{red!30!white}}{}}\fi},

您可以突出显示最后一列。我是否必须执行此操作 5 次才能突出显示上述单元格?

编辑

虽然上述命令改变了特定单元格的颜色,但它弄乱了表格最后一列的数字......这个请求有最佳解决方案吗?

感谢您的帮助!

这是我的初始代码:

\documentclass{book}

\usepackage{pgfplots, pgfplotstable, booktabs, colortbl, siunitx, array}
\pgfplotsset{compat=newest}
\usepackage{longtable}
\usepackage{lipsum}

\usepackage{xcolor}
\usepackage{colortbl}

\usepackage[a4paper,top=1in,bottom=1in,right=1in,left=1in]{geometry}

\usepackage{filecontents}
\begin{filecontents}{t1PCA_KNN1.dat}
  Test  Recog eucl_min_dist
  1   1 2117152161.2166634
  2   2 1598439136.3832090
  3  25 1647045456.7444425
  4   4 886205782.4485940
  5   5 271948956.2398851
  6   6 653878414.5153273
  7   7 1340345307.4232824
  8   8 431759462.3791042
  9   9 1051308071.6470116
 10  10 1436531402.8133149
 11  19 2304006442.0983062
 12  12 1134781789.9087639
 13  13 286127652.4132110
 14  14 2523226457.7814655
 15  15 539120697.4791107
 16   1 2969931960.3896394
 17   3 2383371156.2993927
 18  18 916307981.3529621
 19  19 1239837377.0928159
 20  20 1016379508.3387643
 21  21 732692978.7041084
 22  22 558054042.5147226
 23  23 1609871871.0636196
 24  24 1673370888.3668804
 25  25 846578307.1819449
 26  26 1086392635.0525291
 27  27 1378061314.2426493
 28  28 1129282753.1800313
 29  29 468945796.8113546
 30  30 1144627615.8123975
 31  31 894231645.8568283
 32  32 1609676354.0799763
 33  33 649323260.0691450
 34  34 709194888.7879194
 35  35 1515425478.3608196
 36  36 2357533800.8898063
 37  37 824739660.8160644
 38  38 514565882.2159572
 39  39 717110151.5839378
 40   5 1102296112.0351143
\end{filecontents}%

\begin{document}

\lipsum[1-3]

%%% Code from Dr. Christian ------ for not using headers.----------------------
\pgfkeysifdefined{/pgfplots/table/output empty row/.@cmd}{
    % upcoming releases offer this more convenient option:
    \pgfplotstableset{
        empty header/.style={
          every head row/.style={output empty row},
        }
    }
}{
    % versions up to and including 1.5.1 need this:
    \pgfplotstableset{
        empty header/.style={
            typeset cell/.append code={%
                \ifnum\pgfplotstablerow=-1 %
                    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
                \fi
            }
        }
    }
}
%%%-----------------------------------------------

\pgfplotstabletypeset[
    every head row/.style={
    before row=\toprule,after row=\midrule},
    %every last row/.style={
    %after row=\bottomrule},
    every odd row/.style={
    %before row={\rowcolor[gray]{0.9}}},
    before row={\rowcolor{blue!40}}},
    postproc cell content/.append code={\ifnum\numexpr\pgfplotstablerow+1=\pgfplotstablerows %
    \pgfkeys{/pgfplots/table/@cell content/.add={\cellcolor{red!30!white}}{}}\fi},
    empty header,
    begin table=\begin{longtable},
    columns/Test/.style={column type={c},precision=0},
    columns/Recog/.style={column type={c},precision=0},
    columns/eucl_min_dist/.style={sci, sci zerofill, dec sep align, precision=2},
    every first row/.append style={before row={%
    \caption{This is a Table with Data}%
    \label{tab:DataTable}\\\toprule
    \multicolumn{1}{p{1.7cm}}{\centering{\bfseries{Test Subject}}} & \multicolumn{1}{p{1.7cm}}{\centering{\bfseries{Recog Subject}}} & \multicolumn{2}{p{1.7cm}}{\centering{\bfseries{Euclidean Min Dist}}}\\ \toprule
    \endfirsthead
    %
    \multicolumn{4}{c}%
    {{\bfseries Table \thetable\ Continued from previous page}} \\
    \toprule
    %
    \multicolumn{1}{p{1.7cm}}{\centering{\bfseries{Test Subject}}} & \multicolumn{1}{p{1.7cm}}{\centering{\bfseries{Recog Subject}}} & \multicolumn{2}{p{1.7cm}}{\centering{\bfseries{Euclidean Min Dist}}}\\ \toprule
    \endhead
    %
    \midrule \multicolumn{4}{r}{{Continued on next page}} \\ \bottomrule
    \endfoot
    %
    \midrule
    \multicolumn{4}{r}{{End of Table}} \\ \bottomrule
    \endlastfoot
    }},
    end table=\end{longtable},
    col sep=space,
    string type,
    ]{t1PCA_KNN1.dat}

\end{document} 

答案1

我没有查看您的所有代码,但您可以按照以下方式突出显示特定的行:

\pgfplotstabletypeset[
    ...
    every odd row/.style={before row={\rowcolor{blue!40}}},
    highlight row/.style={every row no #1/.style={before row={\rowcolor{red!40}}}}, 
    highlight row/.list={2,10,15,16,39},
    empty header,
    ...
]{t1PCA_KNN1.dat}

您可以列出所有应突出显示的行索引。请注意,行索引从 0 开始。

自动查找行索引可能要困难得多。

相关内容