我正在处理一张需要处理空单元格的表格。示例如下:
\documentclass{standalone}
\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}}
\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}{
\pgfmathsetmacro\colsum{\colsum+\thisrowno{\col}}
}
\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
1010125; 3.0; 5;
1010126; 4.0; 6; 8
1010127; 2.5; 7; 9
}\Marks
\addsumcol{\Marks}{1}{Sum}
\pgfplotstabletypeset[columns={ID, Points 1, Points 2, Points 3, Sum},
columns/Mark/.style={numeric type, fixed zerofill, precision=1}]\Marks
\end{document}
问题是我一直收到错误信息
缺失数字,视为零。\addsumcol{\Marks}{1}{Sum}
尽管如此,这个小例子的输出看起来还不错。
随着表的增大,表不再生成,而且由于这些错误太多,我无法找出问题所在。
我在这里使用其他几个包找到了一些提示,比如字符串在尝试添加值之前检查单元格是否为空\IfStrEq{}{}{}
,但我没有发现它不起作用的原因。
因为我想稍后检查是否有空单元格,以便决定不显示完整的行或标记它,以便如果剩下空单元格则有人必须提供缺失的值,所以我不想在使用之前用零替换空单元格\addsumcol{\Marks}{1}{Sum}
。
如能得到任何提示我将非常感激。
答案1
使用包的建议xstring
:
\IfStrEq{\thisrowno{\col}}{}% check if the cell is empty
{}% true
{\pgfmathsetmacro\colsum{\colsum+\thisrowno{\col}}}% false
代码:
\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}}
\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
1010125; 3.0; 5;
1010126; 4.0; 6; 8
1010127; 2.5; 7; 9
}\Marks
\addsumcol{\Marks}{1}{Sum}%
\pgfplotstabletypeset[columns={ID, Points 1, Points 2, Points 3, Sum},
columns/Mark/.style={numeric type, fixed zerofill, precision=1}]\Marks
\end{document}
请注意,我已删除一些不必要的空格。