pgfplotstable:以字符串类型更改单元格内容的颜色,同时保持样式不变

pgfplotstable:以字符串类型更改单元格内容的颜色,同时保持样式不变

扩展所涵盖的主题pgfplotstable:如何在保持样式的同时改变单元格内容的颜色?,正确解析为@frougon,我需要更改中单个单元格的颜色string style,我使用来对siunitx中的数字进行舍入。string styletype 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}

当改变单元格的样式时,舍入不再有效,我得到:

答案1

您需要做的就是使用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}

在此处输入图片描述

附录:如果您想调整字体粗细等,您可能希望指示siunitx不要忽略您。一种方式是将选项添加detect-all=true\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}

在此处输入图片描述

答案2

请注意,我更新了其他答案使用该\pgfkeysgetvalue{/pgfplots/table/@cell content}{...}方法而不是my special cell/.expanded={...}为了检索单元格内容不会过早扩大,然后将其包裹在里面\textcolor

似乎可以使用siunitxpgfplotstable但手册中没有提到1。因此,目前,我仅建议那里提到的方法,即使用。默认情况下,这会在列内使用水平居中,但您可以使用或指定不同的对齐类型。让我们开始吧。:-)siunitxpgfplotstable/pgfplots/table/dec sep aligndec sep align=ldec sep align=r

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

在此处输入图片描述

脚注

  1. 查看薛定谔猫的答案

相关内容