如同这问题,我需要跳过顶部和底部的一些行。例如,在此 MWE 中:
\documentclass{article}
\usepackage{csvsimple,filecontents}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
X1, Y1, 1
X2, Y2, 2
X3, Y3, 3
X4, Y4, 4
\end{filecontents*}
\begin{document}
\csvreader[tabular=|l|l|c|,
table head=\hline & Name & Age\\\hline,
late after line=\\\hline,
%filter={\value{csvinputline}>2},
%filter={\value{csvrow}<2},
]%
{scientists.csv}{name=\name,surname=\surname,age=\age}%
{\thecsvrow & \surname~\name & \age }%
\end{document}
,我想跳过前 2 行和后 3 行。我该怎么做?
答案1
要跳过最后几行,您需要先知道行数。因此,您应该使用第一个读取器计算行数,然后使用第二个读取器显示表格。
\documentclass{article}
\usepackage{csvsimple,filecontents}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
X1, Y1, 1
X2, Y2, 2
X3, Y3, 3
X4, Y4, 4
\end{filecontents*}
\begin{document}
%%%%%%%%%%%%%%%%%
Full table for comparison:
\csvreader[tabular=|l|l|c|,
table head=\hline & Name & Age\\\hline,
late after line=\\\hline,
]%
{scientists.csv}{name=\name,surname=\surname,age=\age}%
{\thecsvrow & \surname~\name & \age }%
\bigskip
%%%%%%%%%%%%%%%%%
Reduced table (without the first 2 rows and the last 3 rows):
% count rows
\csvreader{scientists.csv}{}{}%
\edef\totalrows{\thecsvrow}
% show table
\csvreader[tabular=|l|l|c|,
table head=\hline & Name & Age\\\hline,
late after line=\\\hline,
filter expr={
test{\ifnumgreater{\thecsvinputline}{3}}
and test{\ifnumless{\thecsvinputline}{\totalrows-1}}
}]%
{scientists.csv}{name=\name,surname=\surname,age=\age}%
{\thecsvrow & \surname~\name & \age }%
\end{document}
辅助宏\totalrows
保存总行数。
为了进行过滤,我使用了一些允许合并表达式的csvsimple
版本的新功能,如下所示:1.20
etoolbox
filter expr={
test{\ifnumgreater{\thecsvinputline}{3}}
and test{\ifnumless{\thecsvinputline}{\totalrows-1}}
}
使用旧版本的软件包,您也可以使用ithen
语法(也适用于新版本):
filter={
\(\thecsvinputline>3\)
\and \(\thecsvinputline<\numexpr\totalrows-1\relax\)
}