7

Similar to this question, I need to skip some rows both from the top and bottom. For example, in this 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}

, I want to skip the first 2 rows and the last 3 rows. How can I do that?

8

To skip the last rows, you need to know the number of rows first. Therefore, you should count the rows with a first reader, and then display the table with a second reader.

enter image description here

\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}

The auxiliary macro \totalrows holds the total number of rows.

For filtering, I used some new features of csvsimple version 1.20 which allows to incorporate etoolbox expression, here:

filter expr={
      test{\ifnumgreater{\thecsvinputline}{3}}
  and test{\ifnumless{\thecsvinputline}{\totalrows-1}}
}

Using an older package version, you could alternatively use ithen syntax (also valid for the new version):

filter={
      \(\thecsvinputline>3\)
 \and \(\thecsvinputline<\numexpr\totalrows-1\relax\)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.