1

I would like to highlight the whole row in red and change the color of the text if column 1 is not equal to column 2. I am using pgfplotstable with longtable.

In this example, rows 3, 11, 16, 17, and 40 should be highlighted.

I saw that if you put the command:

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

you can highlight the last column. Do I have to do this 5 times to get the above mentioned cells highlighted?

EDIT

Although the above command changes the color of the particular cell, it messes the digits in the last column of the table...is there a optimum solution to this request?

Thanks for your help!

Here is my initial code:

\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} 
3
4
+50

I didn't look at all your code, but you can highlight specific rows in the following way:

\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}

You can just list all row indices that should be highlighted. Note that the row index starts at 0.

Finding the row indices automatically is probably much more difficult.

2
  • Thanks for your input...with your solution, when I put highlight row/.list={0,10,15,16,39},...I lose my headers...It appears that if "0" is included in the list, the headers disappears. Is there a work around for this? Thanks! – Joe Nov 25 '16 at 20:44
  • 1
    Try to change every first row/.append style to every head row/.append style. – tim Nov 26 '16 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.