2

Expanding the topic covered in pgfplotstable: how can I change the color of a cell's content while maintaining the style?, correctly resolved by @frougon, I need to change the color of a single cell in string style, I use the siunitxto round numbers in string style using type column={S}.

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{
  round-mode          = places,
  round-precision     = 2, % precisión
}

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

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

\begin{document}

\begin{table}
\pgfplotstabletypeset[
            display columns/0/.style={column type={c},
                column name=\textbf{$Story$}, string type},
            display columns/1/.style={column type={S},
                column name=\textbf{$U_x$}, string type},
            display columns/2/.style={column type={S},
                column name=\textbf{$U_y$},string type},
            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 style={
            my special cell/.expanded={%
            \pgfkeysvalueof{/pgfplots/table/@cell content}%
            },
            },
        },]{\myLoadedTable}
\end{table}

\end{document}

when changing the style of a cell, rounding ceases to have an effect, i get:

6
  • 1
    All you need to is to use my special cell/.style={@cell content=\color{blue}#1}.
    – user194703
    Dec 28, 2019 at 0:36
  • thanks @Schrödinger'scat, but when I want to apply more styles it seems to not work, for instance: my special cell/.style={@cell content=\color{blue}\textbf{#1}}, it spoils again
    – 4lrdyD
    Dec 28, 2019 at 1:02
  • 1
    In this case, use my special cell/.style={@cell content=\color{blue}\bfseries #1}.
    – user194703
    Dec 28, 2019 at 2:10
  • It doesn't work as expected, it's curious.
    – 4lrdyD
    Dec 28, 2019 at 2:42
  • 1
    It does, in principle. Add to the options of \sisetup the option detect-weight=true. Otherwise siunitx ignores the font weight.
    – user194703
    Dec 28, 2019 at 2:47

2 Answers 2

1

All you need to is to use my special cell/.style={@cell content=\color{blue}#1}.

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{
  round-mode          = places,
  round-precision     = 2, % precisión
}

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

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

\begin{document}

\begin{table}
\pgfplotstabletypeset[
            display columns/0/.style={column type={c},
                column name=\textbf{$Story$}, string type},
            display columns/1/.style={column type={S},
                column name=\textbf{$U_x$}, string type},
            display columns/2/.style={column type={S},
                column name=\textbf{$U_y$},string type},
            every head row/.style={before row={\toprule},after row=\midrule},
        every last row/.style={after row={\bottomrule}},
            my special cell/.style={@cell content=\color{blue}#1},
            every row 3 column 2/.append style={
            postproc cell content/.append style={
            my special cell/.expanded={%
            \pgfkeysvalueof{/pgfplots/table/@cell content}%
            },
            },
        },]{\myLoadedTable}
\end{table}

\end{document}

enter image description here

ADDENDUM: If you want to play with the font weight and so on, you may want to instruct siunitx not to ignore you. One way of doing that is to add the detect-all=true option to \sisetup.

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{
  round-mode          = places,
  round-precision     = 2, % precisión
  detect-all=true
}

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

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

\begin{document}

\begin{table}
\pgfplotstabletypeset[
            display columns/0/.style={column type={c},
                column name=\textbf{$Story$}, string type},
            display columns/1/.style={column type={S},
                column name=\textbf{$U_x$}, string type},
            display columns/2/.style={column type={S},
                column name=\textbf{$U_y$},string type},
            every head row/.style={before row={\toprule},after row=\midrule},
        every last row/.style={after row={\bottomrule}},
            my special cell/.style={@cell content=\color{blue}\bfseries#1},
            every row 3 column 2/.append style={
            postproc cell content/.append style={
            my special cell/.expanded={%
            \pgfkeysvalueof{/pgfplots/table/@cell content}%
            },
            },
        },]{\myLoadedTable}
\end{table}

\end{document}

enter image description here

1

Please note that I updated the other answer to use the \pgfkeysgetvalue{/pgfplots/table/@cell content}{...} approach rather than my special cell/.expanded={...} in order to retrieve cell contents without expanding it to early, before wrapping it inside \textcolor.

It appears to be possible to use siunitx with pgfplotstable,1 however siunitx is not mentioned in the pgfplotstable manual. Thus, for now, I'll only propose the approach mentioned there, which is to use /pgfplots/table/dec sep align. By default, this uses horizontal centering inside the column, but you can sepcify a different alignment type with dec sep align=l or dec sep align=r. Here we go. :-)

\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.4334
Story2 & 37.7156 & 37.7156
Story1 & 38.4056 & 38.4034
\end{filecontents*}

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

\begin{document}

\pgfplotstabletypeset[
  my numeric col/.style={fixed, fixed zerofill, precision=2, dec sep align},
  columns/c1/.style={column type={c}, column name=\textbf{Story}, string type},
  columns/c2/.style={my numeric col, column name=$U_x$},
  columns/c3/.style={my numeric col, column name=$U_y$},
  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}{\myTmpCellContents}%
      \pgfkeysalso{my special cell/.expand once={\myTmpCellContents}}%
    },
  }]{\myLoadedTable}

\end{document}

enter image description here

Footnote

  1. See the answer of Schrödinger's cat!
2
  • It seems to be better, I'll try it, thanks a lot again for your time
    – 4lrdyD
    Dec 28, 2019 at 1:45
  • 1
    You're welcome, it's good to have both methods here. For a new user, you've provided decent minimal working examples, thanks to you. :-)
    – frougon
    Dec 28, 2019 at 9:04

You must log in to answer this question.

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