4

Is it possible to declare in TeX array of integer numbers ? What I am trying to do is to declare new tabular environment for very long tables spanning accross multiple columns and (or) pages. My current solution looks like this (simplified):

\begin{tabular}{>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}}
    \toprule
    \myth{Začiatok úseku}&\myth{Koniec úseku}&\myth{Hodnota} \\
    \midrule
    \partialinput{2}{27}{data/KrivkaTvorivosti.csv}
    \bottomrule
\end{tabular}

\begin{tabular}{>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}}
    \toprule
    \myth{Začiatok úseku}&\myth{Koniec úseku}&\myth{Hodnota} \\
    \midrule
    \partialinput{28}{53}{data/KrivkaTvorivosti.csv}
    \bottomrule
\end{tabular}

\begin{tabular}{>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}}
    \toprule
    \myth{Začiatok úseku}&\myth{Koniec úseku}&\myth{Hodnota} \\
    \midrule
    \partialinput{54}{79}{data/KrivkaTvorivosti.csv}
    \bottomrule
\end{tabular}

Macro partialinput is defined here. Every part of long table defined in tabular environment reads particular part of external CSV file based on row indexes. This is relatively convenient in comparison with pure LaTeX but with very long tables consisting of many parts its tedious task to correctly set begin & end row indexes for every part. With increasing number of sections the probability that I make error somewhere rises. When doing error somewhere in the beginning I had to recalculate row indexes for all sections that follow. Currently long table I print is consisting from 12 sections ! Sometimes it happens that content must be moved a few lines above or below and every time I had to go through 12 tabular environments and calculate correct indexes. It would be good for me to declare array with number of rows for every sections something like

\DeclareArray SectionLengths={20,20,20,20,30,30,30,30,10,10,10,10}

and the define new environment longtabular which would iterate through this array and calculate begin & end row index for current section automatically. Is it possible using TeX or LaTeX ?

3

Here's a possibility with expl3.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[slovak]{babel}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\declarechunks}{m}
 {
  \seq_set_from_clist:Nn \g_truth_chunks_seq { #1 }
 }

\NewDocumentCommand{\partialtable}{}
 {
  \int_gincr:N \g_truth_arrayindex_int
  \int_gset:Nn \g_truth_start_int { \g_truth_end_int + 1 }
  \int_gadd:Nn \g_truth_end_int
   {
    \seq_item:Nn \g_truth_chunks_seq { \g_truth_arrayindex_int }
   }
  \truth_partial_table:VV \g_truth_start_int \g_truth_end_int
 }

\cs_new_protected:Npn \truth_partial_table:nn #1 #2
 {
\tl_to_str:n {% <--------------- remove this line
  \begin{tabular}{>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}>{\ttfamily}p{2cm}}
    \toprule
    \myth{Začiatok~úseku}&\myth{Koniec~úseku}&\myth{Hodnota} \\
    \midrule
    \partialinput{#1}{#2}{data/KrivkaTvorivosti.csv}
    \bottomrule
  \end{tabular}
}% <------------------ remove this line
 }

\cs_generate_variant:Nn \truth_partial_table:nn { VV }
\seq_new:N \g_truth_chunks_seq
\int_new:N \g_truth_arrayindex_int
\int_new:N \g_truth_start_int
\int_new:N \g_truth_end_int

\ExplSyntaxOff

\declarechunks{20,12,13,15,2}

\begin{document}

\ttfamily\footnotesize

\partialtable

\partialtable

\partialtable

\partialtable

\partialtable

\end{document}

The \declarechunks command states the number of lines each \partialtable command should print.

When \partialtable is called, an index counter is stepped, the starting point is set to the previous end point plus one and the end point is set as the sum of the previous end point plus the current length of the chunk.

Then the inner function \truth_partial_table:VV is called, so that \partialinput will “see” the explicit values.

I used \tl_to_str:n just to show the calls, as I can't simulate your setup. Remove the two marked lines and don't worry about the strange letters that appear below, which are just by-products of “stringified UTF-8”.

enter image description here

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.