
擴展所涵蓋的主題pgfplotstable:如何在保持樣式的同時變更儲存格內容的顏色?,正確解決了@弗魯岡,我需要更改中單一單元格的顏色string style
,我siunitx
在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}
當更改單元格的樣式時,舍入不再起作用,我得到:
答案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
。
似乎可以siunitx
與pgfplotstable
, 1一起使用,但是手冊siunitx
中沒有提到pgfplotstable
。因此,現在我只建議那裡提到的方法,即使用/pgfplots/table/dec sep align
.預設情況下,這在列內使用水平居中,但您可以使用dec sep align=l
或指定不同的對齊類型dec 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}
註腳
- 請參閱薛丁格的貓的答案!