1

The following code works as I would expect.

\documentclass{scrartcl}
\usepackage{csvsimple,filecontents}

\begin{document} 

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\csvreader[no head,before filter= \ifthenelse{\thecsvrow<4}{\csvfilteraccept}{\csvfilterreject}]{chunk.csv}{}{%
\begin{itemize} 
\item \csvcoli , \csvcolii
\end{itemize}}

\end{document} 

When I change the filter condition from "\thecsvrow<4" to "\thecsvrow=4" or "\thecsvrow>4" the code breaks. Pointers as to how to make this work would be greatly appreciated.

  • Note that "as expected" here is incorrect, since \thecsvrow<4 should (intuitively) only print 3 rows while it actually prints 2. You should use \value{csvrow}<4 instead. – Werner Dec 6 '13 at 22:46
2

The per-item testing should be performed when setting the item, rather than in the header, since the filter are set globally. From the csvsimple documentation:

\csvfilterreject

All following data lines will be ignored. This command overwrites all previous filter settings.

enter image description here

\documentclass{article}
\usepackage{csvsimple,filecontents}% http://ctan.org/pkg/{csvsimple,filecontents}

\begin{document} 

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\begin{itemize}
\csvreader[no head]{chunk.csv}{}{\ifnum\value{csvrow}<4\relax\item \csvcoli, \csvcolii\fi}
\end{itemize}

\hrulefill

\begin{itemize}
\csvreader[no head]{chunk.csv}{}{\ifnum\value{csvrow}=4\relax\item \csvcoli, \csvcolii\fi}
\end{itemize}

\hrulefill

\begin{itemize}
\csvreader[no head]{chunk.csv}{}{\ifnum\value{csvrow}>4\relax\item \csvcoli, \csvcolii\fi}
\end{itemize}

\end{document}

Low-level conditionals are used to (not) print each item, testing the value of csvrow.


Here is an alternative option using datatool. The interface is very similar, and the transition should therefore not be a problem:

enter image description here

\documentclass{article}
\usepackage{datatool,filecontents}% http://ctan.org/pkg/{datatool,filecontents}

\begin{document} 

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\DTLloaddb[noheader,keys={first,last}]{chunk}{chunk.csv}
\begin{itemize}
  \DTLforeach{chunk}{\First=first,\Last=last}{\ifnum\value{DTLrowi}<4\relax\item \First, \Last\fi}
\end{itemize}

\hrulefill

\begin{itemize}
  \DTLforeach{chunk}{\First=first,\Last=last}{\ifnum\value{DTLrowi}=4\relax\item \First, \Last\fi}
\end{itemize}

\hrulefill

\begin{itemize}
  \DTLforeach{chunk}{\First=first,\Last=last}{\ifnum\value{DTLrowi}>4\relax\item \First, \Last\fi}
\end{itemize}

\end{document}

Low-level conditionals are used to (not) print each item, testing the value of DTLrowi.

| improve this answer | |
3

The csvrow counter is used for accepted input lines (after filtering). If you want to count line numbers of the input file to build a filter on it, you should use the counter csvinputline:

\documentclass{article}
\usepackage{csvsimple,filecontents}

\begin{document}

\begin{filecontents*}{chunk.csv}
AAAA,aaaaa
BBBB,bbbbb
CCCC,ccccc
DDDD,ddddd
EEEE,eeeee
FFFFF,fffffff
GGGGG,ggggg
HHHHHH,hhhhh
\end{filecontents*}

\begin{itemize}
\csvreader[no head,filter={\value{csvinputline}<4}]{chunk.csv}{}{\item \csvcoli, \csvcolii}
\end{itemize}

\hrulefill

\begin{itemize}
\csvreader[no head,filter={\value{csvinputline}=4}]{chunk.csv}{}{\item \csvcoli, \csvcolii}
\end{itemize}

\hrulefill

\begin{itemize}
\csvreader[no head,filter={\value{csvinputline}>4}]{chunk.csv}{}{\item \csvcoli, \csvcolii}
\end{itemize}

\end{document}

The resulting output is the same as in the answer of @Werner.

| improve this answer | |

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.