3

I am using the package pgfplotstable to create and typeset several large tables. My challenge is to make several rows bold conditional on the value of a variable in my data.

After searching, reading and testing, I have managed to get a column bold, a cell bold and a row bold (first, last and specific), but not conditional on a variable's value.

In the small example below, I want the rows where ID = 1 to be bold:

\documentclass{article}

\usepackage{pgfplotstable, booktabs}

\pgfplotsset{compat=1.7}

\begin{document}

\pgfplotstabletypeset[
    col sep= semicolon ,
    sci zerofill
]{
A;B;C;ID 
1;4;2;0 
2;1;6;1 
3;6;1;1 
4;2;3;1 
5;1;9;0 
6;4;2;0
}

\end{document}

EDIT:

I did not include any formatting in the example above. Hence, I have added some more details in the MWE below. The follow-up question is how do I keep the formatting specified before making the specified rows bold?

\documentclass[]{report}

\usepackage{pgfplotstable,booktabs, ifthen}

\pgfplotsset{compat=1.14}

\begin{document}

\pgfplotstableread[col sep= semicolon]{
A;B;C;ID 
C00;100;0.9;0
C01;31;16.9;1
C03;46;1;1 
C04;62;0.3;2 
C05;18;1.9;1 
C06;69;2.0;1
}\mytable

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

\pgfplotstabletypeset[
    col sep = semicolon,
    columns = {A, B, C, ID },
    columns/A/.style={ string type, column type = {l}},
    columns/B/.style={ column type = {r}},
    columns/C/.style={ column type = {r}, precision = 1},
    columns/ID/.style={ column type = {r}, precision = 1},
    every column/.style={
        postproc cell content/.append code={
            \pgfplotstablegetelem{\pgfplotstablerow}{ID}\of{\mytable}
            \ifthenelse{ \pgfplotsretval < 2 }{
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{\bfseries##1}
            }
            {
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}
            } 
        }
    }
    ]{\mytable}

\end{document}
5

Here is one possibility to do it.

  • Read the inline table into a macro using \pgfplotstableread, in order to be able to reference the elements in the table using \pgfplotstablegetelem.

  • \pgfplotstablegetelem{\pgfplotstablerow}{[index]3}\of{\mytable} returns the value of the fourth column (= column with index 3) of the current row (stored in \pgfplotstablerow) in the macro \pgfplotsretval.

enter image description here

\documentclass{article}

\usepackage{pgfplotstable, booktabs,ifthen}
\pgfplotsset{compat=1.7}

\begin{document}
\pgfplotstableread[col sep= semicolon]{
A;B;C;ID 
1;4;2;0 
2;1;6;1 
3;6;1;1 
4;2;3;1 
5;1;9;0 
6;4;2;0
}\mytable

\pgfplotstabletypeset[
    sci zerofill,
    every column/.style=
     {assign cell content/.code=
       {\pgfplotstablegetelem{\pgfplotstablerow}{[index]3}\of{\mytable}%
        \ifthenelse{\equal{\pgfplotsretval}{1}}%
          {\pgfkeyssetvalue{/pgfplots/table/@cell content}{\bfseries##1}}%
          {\pgfkeyssetvalue{/pgfplots/table/@cell content}{##1}}%
       }
     }
]{\mytable}
\end{document}

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.