如果给定列中的单元格为空,我想删除已排序列表中的一行。
到目前为止,我已经知道如果我不对表格进行排序,该行将不再显示。但如果我对它进行排序,错误的行将被删除。我认为这与完成事情的顺序有关,但我不知道如何修复它。
\documentclass{standalone}
\usepackage{xstring}
\usepackage{pgfplotstable}
\usepackage{booktabs, colortbl}
\pgfplotstableset{col sep=semicolon, use comma, fixed, set thousands separator={},
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}}
\begin{document}
\pgfplotstableread{
ID; Points 1; Points 2; Points 3
1010121; 1.0; 1; 3
1010122; 1.0; 2; 4
1010123; 5.0; 3; 5
1010124; 5.0; 4; 6
1010127; 3.0; 5;
1010126; 4.0; 6; 8
1010125; 2.5; 7; 9
}\Marks
\pgfplotstabletypeset[%
columns={ID, Points 1, Points 2, Points 3}]\Marks
\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{Points 3}\of{\Marks}
\IfStrEq{\pgfplotsretval}{}%
{\pgfplotstableuserowfalse}%true
{}%false
},
columns={ID, Points 1, Points 2, Points 3}]\Marks
\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{Points 3}\of{\Marks}
\IfStrEq{\pgfplotsretval}{}%
{\pgfplotstableuserowfalse}%true
{}%false
},
sort, sort key={ID}, sort cmp={int <}, columns={ID, Points 1, Points 2, Points 3}]\Marks
\end{document}
代码创建了以下三个表格:
第一个显示完整,第二个是未经排序的表格,最后一个是所有内容汇总在一起。
答案1
有两种可能。如果您需要未排序的表格,您可以添加以下行
\pgfplotstablesort[sort key={ID}, sort cmp={int <}]\MarksSorted\MarksUnsorted
在表格之后,额外获取排序后的表格。或者,如果你不需要未排序的表格,你可以更改
\pgfplotstableread{ ... }\MarksUnsorted
到
\pgfplotstablesort[sort key={ID}, sort cmp={int <}]\MarksSorted{ ... }
(参见下面代码中的注释部分)。
\documentclass[border=2mm]{standalone}
\usepackage{xstring}
\usepackage{pgfplotstable}
\usepackage{booktabs, colortbl}
\pgfplotstableset{col sep=semicolon, use comma, fixed, set thousands separator={},
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}}
\begin{document}
% no unsorted table available
% \pgfplotstablesort[sort key={ID}, sort cmp={int <}]\MarksSorted{
% ID; Points 1; Points 2; Points 3
% 1010121; 1.0; 1; 3
% 1010122; 1.0; 2; 4
% 1010123; 5.0; 3; 5
% 1010124; 5.0; 4; 6
% 1010127; 3.0; 5;
% 1010126; 4.0; 6; 8
% 1010125; 2.5; 7; 9
% }
% both, sorted and unsorted table available
\pgfplotstableread{
ID; Points 1; Points 2; Points 3
1010121; 1.0; 1; 3
1010122; 1.0; 2; 4
1010123; 5.0; 3; 5
1010124; 5.0; 4; 6
1010127; 3.0; 5;
1010126; 4.0; 6; 8
1010125; 2.5; 7; 9
}\MarksUnsorted
\pgfplotstablesort[sort key={ID}, sort cmp={int <}]\MarksSorted\MarksUnsorted
\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{Points 3}\of{\MarksUnsorted}% <-- spurious space removed
\IfStrEq{\pgfplotsretval}{}%
{\pgfplotstableuserowfalse}%true
{}%false
},
columns={ID, Points 1, Points 2, Points 3}]\MarksUnsorted
{ }
\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{Points 3}\of{\MarksSorted}% <-- spurious space removed
\IfStrEq{\pgfplotsretval}{}%
{\pgfplotstableuserowfalse}%true
{}%false
},
columns={ID, Points 1, Points 2, Points 3}]\MarksSorted
\end{document}
答案2
作为一种解决方法,您可以将不包含已删除行的表保存到文件中:
\documentclass{standalone}
\usepackage{xstring}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepackage{booktabs, colortbl}
\pgfplotstableset{col sep=semicolon, use comma, fixed, set thousands separator={},
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}}
\newcommand{\addsumcol}[3]{%
% #1=table name
% #2=first column name
% #3=name of new column
% Sums for each column
\pgfplotstablecreatecol[%
create col/assign/.code={%
\def\entry{}%
\def\colsum{0}%
\pgfmathtruncatemacro\maxcolindex{\pgfplotstablecols-1}%
\pgfplotsforeachungrouped \col in {#2,...,\maxcolindex}{%
\IfStrEq{\thisrowno{\col}}{}% check if the cell is empty
{}% true
{\pgfmathsetmacro\colsum{\colsum+\thisrowno{\col}}}% false
}%
\xdef\entry{\colsum}%
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
]{#3}#1%
}%
\begin{document}
\pgfplotstableread{
ID; Points 1; Points 2; Points 3
1010121; 1.0; 1; 3
1010122; 1.0; 2; 4
1010123; 5.0; 3; 5
1010124; 5.0; 4; 6
1010127; 3.0; 5;
1010126; 4.0; 6; 8
1010125; 2.5; 7; 9
}\Marks
\pgfplotstabletypeset[%
columns={ID, Points 1, Points 2, Points 3},
columns/Mark/.style={numeric type, fixed zerofill, precision=1}]\Marks
\pgfplotstabletypeset[row predicate/.code={%
\pgfplotstablegetelem{#1}{Points 3}\of{\Marks}
\IfStrEq{\pgfplotsretval}{}%
{\pgfplotstableuserowfalse}%true
{}%false
},
columns={ID, Points 1, Points 2, Points 3},
columns/Mark/.style={numeric type, fixed zerofill, precision=1}
]\Marks
\pgfplotstablesave[
row predicate/.code={%
\pgfplotstablegetelem{#1}{Points 3}\of{\Marks}
\IfStrEq{\pgfplotsretval}{}%
{\pgfplotstableuserowfalse}%true
{}%false
},
columns={ID, Points 1, Points 2, Points 3},
col sep=semicolon
]\Marks{marks.csv}
\pgfplotstabletypeset[
sort, sort key={ID}, sort cmp={int <},
columns={ID, Points 1, Points 2, Points 3},
columns/Mark/.style={numeric type, fixed zerofill, precision=1}
]{marks.csv}
\end{document}
结果: