2

I need to change the color of a single cell.

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepackage{booktabs}

\begin{document}

\begin{table}
\pgfplotstabletypeset[
            display columns/0/.style={column type={c},
                column name=\textbf{$Story$}, string type},
            display columns/1/.style={column type={c},
                column name=\textbf{$U_x$}, sci, precision=2},
            display columns/2/.style={column type={c},
                column name=\textbf{$U_y$},sci, precision=2},
            every head row/.style={before row={\toprule},after row=\midrule},
        every last row/.style={after row={\bottomrule}},
            every row 3 column 2/.style={sci,precision=2,
            assign cell content/.style={@cell content=\textcolor{blue}{##1}}
            },
            col sep=&, row sep=\\]{c1 & c2 & c3\\
Story4 & 33.7835 & 33.7878\\
Story3 & 38.4334 & 38.4367\\
Story2 & 37.7156 & 37.7156\\
Story1 & 38.4056 & 38.4034\\}
\end{table}
\end{document}

but, it seems that the sci andprecision styles don't work, I get:

what is the correct way to change the color in this context?, my real table is generated using a csv file.

1 Answer 1

3

I propose the following:

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepackage{booktabs}

\begin{document}

\pgfplotstabletypeset[
  columns/c1/.style={column type={c}, column name=Story, string type},
  columns/c2/.style={column type={c}, column name=$U_x$, sci, precision=2},
  columns/c3/.style={column type={c}, column name=$U_y$, sci, precision=2},
  every head row/.style={before row={\toprule}, after row=\midrule},
  every last row/.style={after row={\bottomrule}},
  my special cell/.style={@cell content=\textcolor{blue}{#1}},
  every row 3 column 2/.append style={
    postproc cell content/.append code={%
      \pgfkeysgetvalue{/pgfplots/table/@cell content}{\myTmpVal}%
      \pgfkeysalso{my special cell/.expand once={\myTmpVal}}%
    },
  },
  col sep=&, row sep=\\]{%
c1     & c2      & c3\\
Story4 & 33.7835 & 33.7878\\
Story3 & 38.4334 & 38.4367\\
Story2 & 37.7156 & 37.7156\\
Story1 & 38.4056 & 38.4034\\}

\end{document}

enter image description here

For your specially-colored cell, this uses the following style:

my special cell/.style={@cell content=\textcolor{blue}{#1}}

to which I pass the cell contents after sci formatting has been applied (the value of /pgfplots/table/@cell content} is retrieved at the right time, then passed to the my special cell style).

Note that if you replace:

every row 3 column 2/.append style={
  postproc cell content/.append code={%
    \pgfkeysgetvalue{/pgfplots/table/@cell content}{\myTmpVal}%
    \pgfkeysalso{my special cell/.expand once={\myTmpVal}}%
  },
},

with:

every row 3 column 2/.append style={
  postproc cell content/.append style={
    my special cell=##1,
  },
},

you'll see this instead:

enter image description here

because the first argument of /pgfplots/table/postproc cell content is defined as

the unprocessed input argument as it has been found in the input table

(see Postprocessing Cell Content in the pgfplotstable manual).

Regarding your last sentence, you can use a .csv file even for your minimal working example, thanks to the filecontents* environment of the LaTeX kernel:

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepackage{booktabs}

\begin{filecontents*}{my-data.csv}
c1     & c2      & c3
Story4 & 33.7835 & 33.7878
Story3 & 38.4334 & 38.4367
Story2 & 37.7156 & 37.7156
Story1 & 38.4056 & 38.4034
\end{filecontents*}

\pgfplotstableread[col sep=&]{my-data.csv}\myLoadedTable

\begin{document}

\pgfplotstabletypeset[
  columns/c1/.style={column type={c}, column name=Story, string type},
  columns/c2/.style={column type={c}, column name=$U_x$, sci, precision=2},
  columns/c3/.style={column type={c}, column name=$U_y$, sci, precision=2},
  every head row/.style={before row={\toprule}, after row=\midrule},
  every last row/.style={after row={\bottomrule}},
  my special cell/.style={@cell content=\textcolor{blue}{#1}},
  every row 3 column 2/.append style={
    postproc cell content/.append code={%
      \pgfkeysgetvalue{/pgfplots/table/@cell content}{\myTmpVal}%
      \pgfkeysalso{my special cell/.expand once={\myTmpVal}}%
    },
  }]{\myLoadedTable}

\end{document}
8
  • thanks @frougon, it works!, but not in string type, Do I need to do something additional for this type?
    – 4lrdyD
    Dec 27, 2019 at 22:26
  • What do you mean? If I take the example from my answer and replace every row 3 column 2 with every row 3 column 0, I get the expected result (“Story1” in blue). Column 0 is string type here.
    – frougon
    Dec 27, 2019 at 22:30
  • the previous table is just an example, I have other problems with the real tables that I use, would I put it in another post?
    – 4lrdyD
    Dec 27, 2019 at 22:34
  • This may be due to the .expanded={...}. Do you have fragile macros in the stuff that causes problem? If you give me one short line to add to the example, I can see and test something, otherwise probably better start a new question.
    – frougon
    Dec 27, 2019 at 22:39
  • Can you try this? every row 3 column 0/.append style={ postproc cell content/.append code={\pgfkeysgetvalue{/pgfplots/table/@cell content}{\tmp}\pgfkeysalso{my special cell/.expand once={\tmp}}}}. It is careful not to expand the special cell's retrieved contents (it only expands a macro whose replacement text is said contents).
    – frougon
    Dec 27, 2019 at 22:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .