# Tables in chunks of 25 rows

I need to import some data into a LaTeX document form CSV file, using, as suggested in many sites, datatool package.

But I would divide this data in many tables, in this manner:

Table #1
========
row1
row2
row3
...
row25

(New Page)

Table #2
=========
row26
row27
...
row50

(New Page)
Table #3
=========
row51
....
row75


and so on...

Edit: Thank you Werner for your solution. But I had some difficulties with table header. This is my code:

    \documentclass{article}

\usepackage[paperwidth=20em]{geometry}% Just for this example

\usepackage{datatool}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
rowID
row1   ,1
row2   ,1
row3    ,1
row4,1
row5,1
row6,1
row7,1
row8,1
row9,1
row10,1

\end{filecontents*}
\newcounter{rowmax}
\newcommand{\nextrow}{\stepcounter{rowmax}}
\newcommand{\restartrow}{\setcounter{rowmax}{0}}

\begin{document}

\begin{tabular}{cc}
\textbf{RowID} & \textbf{N} \\
\DTLforeach{mytable}{\rowID=rowID}{%
\ifnum\value{rowmax}=5
\restartrow
\end{tabular}
\newpage
\begin{tabular}{cc}
\textbf{RowID} & \textbf{N} \\
\fi
\\% Start a new row
\rowID & 1
\nextrow
}
\end{tabular}

\end{document}


This is error message:

! Incomplete \ifnum; all text was ignored after line 43.
<inserted text>
\fi
l.43   }


## migrated from stackoverflow.comFeb 21 '17 at 3:23

This question came from our site for professional and enthusiast programmers.

• See my updated answer on how to deal with a multi-column tabular. – Werner Feb 21 '17 at 20:52

Here's an option using datatool's \DTLforeach:

\documentclass{article}

\usepackage[paperwidth=20em]{geometry}% Just for this example

\usepackage{datatool}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
rowID
row1
row2
row3
row4
row5
row6
row7
row8
row9
row10
row11
row12
row13
row14
row15
row16
row17
row18
row19
row20
row21
row22
row23
row24
row25
row26
row27
row28
row29
row30
row31
row32
row33
row34
row35
row36
row37
row38
row39
row40
row41
row42
row43
row44
row45
row46
row47
row48
row49
row50
row51
row52
row53
row54
row55
row56
row57
row58
row59
row60
row61
row62
row63
row64
row65
row66
row67
row68
row69
row70
row71
row72
row73
row74
row75
row76
row77
row78
row79
row80
row81
row82
row83
row84
row85
row86
row87
row88
row89
row90
row91
row92
row93
row94
row95
row96
row97
row98
row99
\end{filecontents*}
\newcounter{rowmax}
\newcommand{\nextrow}{\stepcounter{rowmax}}
\newcommand{\restartrow}{\setcounter{rowmax}{0}}

\begin{document}

\begin{tabular}{c}
\textbf{RowID}
\DTLforeach{mytable}{\rowID=rowID}{%
\ifnum\value{rowmax}=25
\restartrow
\end{tabular}
\newpage
\begin{tabular}{c}
\textbf{RowID}
\fi
\\% Start a new row
\rowID\nextrow
}
\end{tabular}

\end{document}


With multiple columns within a tabular that you want to spread this way, there may be some grouping issues between columns. Here's one way to get around that:

\documentclass{article}

\usepackage[paperwidth=20em]{geometry}% Just for this example

\usepackage{datatool}

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
rowID,otherID
row1   ,1
row2   ,2
row3    ,3
row4,1
row5,2
row6,3
row7,1
row8,2
row9,3
row10,1
\end{filecontents*}

\newcounter{rowmax}
\newcommand{\nextrow}{\stepcounter{rowmax}}
\newcommand{\restartrow}{\setcounter{rowmax}{0}}
\newcommand{\newpagenewtabular}{%
\restartrow
\end{tabular}
\newpage
\begin{tabular}{cc}
\textbf{RowID} & \textbf{N}
}

\begin{document}

\begin{tabular}{cc}
\textbf{RowID} & \textbf{N}
\DTLforeach{mytable}{\rowID=rowID,\otherID=otherID}{%
\ifnum\value{rowmax}=5
\expandafter\newpagenewtabular
\fi
\\% Start a new row
\nextrow\rowID & \otherID%
}
\end{tabular}

\end{document}


The principle idea behind the second (multi-column) approach is to store the tabular-and-\newpage breaking inside a macro and complete the \if...\fi structure first using \expandafter\newpagenewtabular.