Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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
add comment

2 Answers

up vote 2 down vote accepted
+100

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.

share|improve this answer
add comment

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).

MWE for your database follows:

\documentclass[12pt]{article}
%\usepackage[charter]{mathdesign}
%\usepackage[a4paper,left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{datatool}
\DTLloaddb{animals}{dbase.csv}
%==================================================================
\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}

enter image description here

share|improve this answer
3  
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. –  qubyte Apr 2 '12 at 12:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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