3

I want to print a pgfplotstable where one column (which is not printed) used to change the style of another column. In my MWE below, I want to change the font= style of select values in the Item column if the highlight column contains a value 1. But in my actual use case, I will be changing a number of properties.

I have tried using \getthisrow in preproc cell content/.code, but in that context I get an "Undefined control sequence" error. What else can I try?

Secondly, in preproc cell content/.code I am attempting to change the font, using \pgfplotstableset, which does not seem to have any effect (otherwise the items in the "Not what I want"-table would all be orange).

rendering

\documentclass{standalone}

\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{color}

\begin{filecontents}{data.csv}
highlight,Item,Unrelated
1,Lorem,f
0,ipsum,o
0,dolor,o
1,sit,
0,amet,bar
\end{filecontents}

\begin{document}
\pgfplotstabletypeset[
    col sep=comma,
    columns={Item,Unrelated},
    columns/Item/.style={string type},
    columns/Unrelated/.style={string type},
    every head row/.style={before row={
        \textcolor{red}{Not what I want:}\\\toprule},
        after row=\midrule},
    every last row/.style={after row=\bottomrule},
    preproc cell content/.code={
        %\getthisrow{highlight}\highlight % <--Undefined control sequence
        %use \highlight here to set pgf value
        \pgfplotstableset{font=\color{orange}} % <--change styling for current cell (has no effect)
    },
]{data.csv}

\pgfplotstabletypeset[
    col sep=comma,
    columns={Item,Unrelated},
    columns/Item/.style={string type},
    columns/Unrelated/.style={string type},
    every head row/.style={before row={
        \textcolor{green}{Desired result:}\\\toprule},
        after row=\midrule},
    every last row/.style={after row=\bottomrule},
]{
highlight,Item,Unrelated
1,\textcolor{orange}{Lorem},f
%color should be set by pgf key, not \textcolor
0,ipsum,o
0,dolor,o
1,\textcolor{orange}{sit},
%color should be set by pgf key, not \textcolor
0,amet,bar
}
\end{document}
1

Whether your code works or not, you first need to access to the column of interest so the code you have doesn't relate to the Item column but sets a general one. Also \getthisrow is not defined in that context as you have pointed out.

I put a rather slow way of reaching to the correct value by \pgfplotstablegetelem and used that for the conditional. Also I used the post processing since the conditional only changes the appearance not any data of the table.

\documentclass{standalone}

%\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{booktabs}
%\usepackage{colortbl}
%\usepackage{color} %No need for this. TikZ family uses the better "xcolor"

\pgfplotstableread[col sep=comma]{
highlight,Item,Unrelated
1,Lorem,f
0,ipsum,o
0,dolor,o
1,sit,
0,amet,bar
}\mytable

\begin{document}
\pgfplotstabletypeset[
    columns={Item,Unrelated},
    columns/Item/.style={string type,
        postproc cell content/.prefix code={%
            \pgfplotstablegetelem{\pgfplotstablerow}{highlight}\of\mytable%Get the boolean
            \ifnum\pgfplotsretval>0\relax%  If boolean is 1
            \begingroup\edef\temp{\endgroup\noexpand%  Start a dummy expanded variable
            \pgfkeyssetvalue{/pgfplots/table/@cell content}{\noexpand\color{orange}%Set the content
            \pgfkeysvalueof{/pgfplots/table/@preprocessed cell content}% with the orange version
            }}\temp% End definition and invoke the defined macro for expansion
            \fi% End of conditional
        }
    },
    columns/Unrelated/.style={string type},
    every head row/.style={before row={\textcolor{red}{Also what I want:}\\\toprule},after row=\midrule},
    every last row/.style={after row=\bottomrule}
]\mytable

\pgfplotstabletypeset[
    col sep=comma,
    columns={Item,Unrelated},
    columns/Item/.style={string type},
    columns/Unrelated/.style={string type},
    every head row/.style={before row={
        \textcolor{green}{Desired result:}\\\toprule},
        after row=\midrule},
    every last row/.style={after row=\bottomrule},
]{
highlight,Item,Unrelated
1,\textcolor{orange}{Lorem},f
%color should be set by pgf key, not \textcolor
0,ipsum,o
0,dolor,o
1,\textcolor{orange}{sit},
%color should be set by pgf key, not \textcolor
0,amet,bar
}
\end{document}

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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