裁剪表格以适合页面上的可用空间

裁剪表格以适合页面上的可用空间

我有一个很长的数据列表,保存在 CSV 文件中,格式如下:

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

前几行比后面的行更重要,而且我不需要显示所有数据。

  • 我想将这些数据放在一个表格中,但只显示表格中可以显示并放在单个页面中的数据以及标题。
  • 如果表格中的 20 行只能在页面上显示,那么其余的行将被剪切而不显示。
  • 我不想缩放任何文本。

如何在 ConTeXt 中创建一个表格,使其仅显示可容纳单页的 CSV 文件中的数据量?

答案1

由于您没有提供最小示例来展示如何读取 CSV 数据(有几种选择),我将仅展示如何查找页面上剩余的行数。您只需将此数字插入 CSV 读取机制即可仅读取所需的行数。

首先定义一个度量

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

这里\pagegoal是需要填充的页面总高度,\pagetotal是已经填充的页面高度,\openlineheight是行高(考虑了行间空间)。

然后,您可以使用 \measure{remainingLines} 评估该度量以获得剩余行数,然后以您想要的任何方式使用它。

\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 还提供了宏和\getnoflines,它们接受一个 TeX 维度并将与该维度对应的行数存储在计数寄存器中。详情请参阅。这些宏在多列和网格宏的后台使用。\getrawnoflines\getroundednoflines\noflinessupp-box.mkiv

答案2

来自datatool文档(第 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}

或者,

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

因此您可以控制要打印的行数(这里是 3 行)。

平均能量损失您的数据库如下:

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

在此处输入图片描述

相关内容