Cutting a table to fit the available space on a page

I have a long list of data, in a CSV file, formatted like this:

tiger, 300, 10.0, 7, 4.0
lion, 200, 15.0, 5, 3.0
bear, 100, 15.0, 3, 1.0
fish, 10, 10.0, 2, 0.1


The first rows are more important than the later rows and I do not need to display all of the data.

• I want to put this data in a table, but only show as much data as can be displayed in the table and fit in a single page, along with a caption.
• If only 20 lines from the table can be displayed on the page, then the rest of the lines are cut and not displayed.
• I do not want to scale any of the text.

How can I create a table in ConTeXt which only displays the amount of data from the CSV file which can fit onto a single page?

-
You could probably reverse the logic from the solution of Show last N rows from input datafile only in \pgfplotstabletypeset. Assuming your font size is fixed you could hard code the number of lines to extract as it won't change. –  Peter Grill Mar 28 '12 at 1:13
Please clarify your question: LaTeX or ConTeXt? –  Martin Schröder Apr 8 '12 at 14:03
I am looking for a solution that works with ConTeXt. –  Village Apr 8 '12 at 15:03
How do you create the table in ConTeXt in the first place? –  egreg Apr 8 '12 at 19:22

Since you don't provide a minimal example showing how you are reading CSV data (there are a few options), I'll just show how to find the remaining number of lines on a page. You just need to plug this number into the CSV reading mechanism to read only the required number of lines.

First define a measure

\definemeasure[remainingLines][\the\numexpr(\pagegoal-\pagetotal-\lineheight)/\openlineheight]


Here \pagegoal is the total height of the page to be filled, \pagetotal is the height of the page that has been already filled, and \openlineheight is the lineheight (taking the interline space into account).

You can then evaluate this measure using \measure{remainingLines} to get a number of remaining lines, and then use it in whatever way you want.

\definemeasure[remainingLines][\the\numexpr(\pagegoal-\pagetotal-\lineheight)/\openlineheight]

% Use  a smaller pagesize
\setuppapersize[A6]

% For visual debugging
\showgrid

\starttext
\input tufte

There are \measure{remainingLines} lines left.

\page

\dorecurse{15}{random text }

There are \measure{remainingLines} lines left.

\page

% For some reason this does not work on the first line of the page, presumably
% because \pagetotal is not set properly
\null
There are \measure{remainingLines} lines left.

\stoptext


ConTeXt also provides macros \getnoflines, \getrawnoflines and \getroundednoflines that take a TeX dimen and store the number of lines corresponding to that dimen in the count register \noflines. See supp-box.mkiv for details. These macros are used behind the scenes in the multi-column and grid macros.

-

From datatool documentation (page 45) :

\begin{table}[htbp]
\caption{First Three Rows}
\centering
\begin{tabular}{llr}
\bfseries First Name & \bfseries Surname & \bfseries Score (\%)%
\DTLforeach*{scores}%
{\firstname=FirstName,\surname=Surname,\score=Score}{%
\ifthenelse{\DTLcurrentindex=3}{\dtlbreak}{}%
\\\firstname & \surname & \score
}%
\end{tabular}
\end{table}


Alternatively,

\DTLforeach{scores}%
{\firstname=FirstName,\surname=Surname,\score=Score}{%
\\\firstname & \surname & \score
\ifthenelse{\value{DTLrowi}=3}{\dtlbreak}{}%
}%


Hence you can control the number of rows to be printed (here 3 rows).

\documentclass[12pt]{article}
%\usepackage[charter]{mathdesign}
%\usepackage[a4paper,left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{datatool}
%==================================================================
\begin{document}
%
We print only the first three rows from your database that has four rows.

The first approach follows.
\begin{table}[htbp]
\caption{First three Rows}\label{tab:20rows}
\centering
\begin{tabular}{lllll}
\DTLforeach*{animals}%
{\animal=animal,\one=one,\two=two,\three=three,\four=four}{%
\ifthenelse{\DTLcurrentindex=3}{\dtlbreak}{}%
\\\animal & \one & \two & \three & \four
}%
\end{tabular}
\end{table}
%

Now the second approach:
\begin{table}[htbp]
\caption{First three Rows}\label{tab:20rows}
\centering
\begin{tabular}{lllll}\hline
\DTLforeach*{animals}%
{\animal=animal,\one=one,\two=two,\three=three,\four=four}{%
\animal & \one & \two & \three & \four \\\hline
\ifthenelse{\value{DTLrowi}=3}{\dtlbreak}{}%
}%
\end{tabular}
\end{table}

You can decide how many lines will fit in a page and change the number of rows accordingly.
\end{document}
`

-
I may be mistaken, but I think the question is asking for a way to automatically find the number of lines of the table that will fit on the page. This approach requires the author to make the choice manually. In addition, the question specifies (albeit not so clearly) a solution with ConTeXt rather than LaTeX. –  Mark S. Everitt Apr 2 '12 at 12:03