Pgfplotstable。如何从排序范围中排除一行?

Pgfplotstable。如何从排序范围中排除一行?

我是 LaTeX 的初学者,正在尝试工业文档。我找不到合适的代码来解决以下一些问题:

  1. 我希望能够对表格进行排序,但不包括最后一行,这应该是一个摘要。
  2. 我找不到如何将fixed, zerofill, dec sep align, precision=2参数应用于除第一列之外的所有列。
  3. 我无法使最后一行粗体字体的代码起作用。
\documentclass{article}
\usepackage{colortbl,hhline} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{booktabs}

\begin{filecontents}{data.csv} 
{Items},{Rating},{Design 1},{Design 2},{Design 3}
Design complexity, 6.3,3.5, 8.5, 8.12
Cost,9.2, 3.8, 5.77, 5.9
Energy consumption,6.62,4.8, 8.1,9.5
Average weighted normalized,, 3.7, 7.92,7.35
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}

\centering
\pgfplotstabletypeset[font=\small,
every even row/.style={
before row={\rowcolor[gray]{0.9}}},
every head row/.style={
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
%every first column/.style={string type},%does not work
columns/Items/.style={string type}, %works
column 2/.style={fixed, zerofill, dec sep align, precision=2}, %does not work
every last column/.style={fixed, zerofill, dec sep align, precision=1}, 
%sort, sort key={Design 2}, %Works but should not include last row
%sort cmp={float >}, %Works but should not include last row
every first column/.style={column type={l}}, %Does not work
every head row/.style={
before row={\toprule},
after row=\midrule
},
every last row/.style={before row={\bottomrule}, 
after row={\bottomrule}, 
postproc cell content/.style={@cell content=\textbf{##1}}}, %does not work
]{\datatable}

\end{document}

答案1

  1. 对除一行之外的所有行进行排序的任务可以通过临时列来完成,其中“特殊”行的排序权重具有极值。
  2. 列样式需要应用于各个列。您可以按列名或(输出)列索引指定样式。对于您的情况,您可能希望创建一个numeric column可供所有其他样式引用的共享样式。
  3. 这是 pgfplotstable 的可用性问题;另请参阅 PGFplotstable:是否可以为“行”而不是“列”设置数字格式?

这是您修改后的示例。我添加了%% CF修改内容。我还解决了您的“不起作用”标记:

\documentclass{article}
\usepackage{colortbl,hhline} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\usepackage{filecontents} \usepackage{booktabs}

\begin{filecontents}{data.csv} 
{Items},{Rating},{Design 1},{Design 2},{Design 3}
Design complexity, 6.3,3.5, 8.5, 8.12
Cost,9.2, 3.8, 5.77, 5.9
Energy consumption,6.62,4.8, 8.1,9.5
Average weighted normalized,, 3.7, 7.92,7.35
\end{filecontents}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}{\datatable}

\centering
\pgfplotstabletypeset[
% CF:
numeric column/.style={fixed, zerofill, dec sep align, precision=2},
display columns/1/.style={numeric column},
display columns/2/.style={numeric column},
display columns/3/.style={numeric column},
display columns/4/.style={numeric column},
display columns/5/.style={numeric column},
display columns/6/.style={numeric column},
%%%
font=\small,
every even row/.style={
before row={\rowcolor[gray]{0.9}}},
every head row/.style={
before row=\toprule,after row=\midrule},
every last row/.style={
after row=\bottomrule},
%every first column/.style={string type},%does not work
%% CF: yes, it does: but you overwrite it a couple of lines below
%%
columns/Items/.style={string type}, %works
column 2/.style={fixed, zerofill, dec sep align, precision=2}, %does not work
%% CF: this is the wrong style  'display columns/2/.style='
%%
every last column/.style={fixed, zerofill, dec sep align, precision=1}, 
%sort, sort key={Design 2}, %Works but should not include last row
%sort cmp={float >}, %Works but should not include last row
%% CF:
sort, sort key={sort key},
sort cmp={float >},
create on use/sort key/.style={
    create col/assign/.code={
        \begingroup
            \global\edef\entry{\thisrow{Design 2}}
            \count0=\pgfplotstablerow
            \advance \count0 by1
            \ifnum\count0=\pgfplotstablerows
                \global\def\entry{-1e20}%
            \fi
        \endgroup
        %\message{sort key entry no \pgfplotstablerow / \pgfplotstablerows: \entry^^J}%
        \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    },
},
every first column/.style={column type={l}}, %Does not work
%% CF: see above
every head row/.style={
before row={\toprule},
after row=\midrule
},
every last row/.style={before row={\bottomrule}, 
after row={\bottomrule}, 
postproc cell content/.style={@cell content=\textbf{##1}}}, %does not work
%% CF: confirmed... the style every last row does not support this.
%% Sorry; not properly documented or reported.
%% see https://tex.stackexchange.com/questions/65546/pgfplotstable-is-it-possible-to-set-a-number-format-for-a-row-instead-of-a-co
]{\datatable}

\end{document}

声明create on use/sort key定义了如何创建sort key在 的值中引用的列sort key。我使用一个临时整数寄存器将“+1”添加到行索引,将其与行数进行比较,并为最后一行分配一些特殊的极值。由于 TeX 的编程语言很丑陋,这很尴尬……

这对你有帮助吗?

相关内容