3

Is there a simple way to get the number of rows of a given tabular ?

More precisely, I have a variable \t@b which contains something like

foo & bar \\ foo & bar \\ foo & bar

I will display the content of this variable inside a tabular environment. The number of columns is determined, but how can I find the number of rows of the resulting tabular ?

  • Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Oct 5 '14 at 9:23
  • 1
    I do not think there is a relevant MWE to produce here (I do not have an error to show). I just want to know how to count the number of rows of \begin{tabular} \t@b \end{tabular} given \t@b (or equivalently the number of occurences of ` \\ ` inside \t@b). – Arkandias Oct 5 '14 at 9:37
  • 1
    @Arkandias see, Christian had to make a test file as you did not supply one. It always helps people who are answering if you supply a test file. – David Carlisle Oct 5 '14 at 9:48
  • 1
    essentially a duplicate of this, although you apparently only want the final value, not have the number printed each row: tex.stackexchange.com/questions/21243/… – David Carlisle Oct 5 '14 at 9:49
  • It seems as if you're trying to reinvent some wheel. Not sure if this helps but the pgfplotstable and the datatool package provide high-level support for database table. Also see this database-related question. – user10274 Oct 6 '14 at 11:37
6

The easiest way is to use a new column type which automatically counts the number of rows, e.g. by using the array package and the \newcolumntype. I assumed to have a left justified 2nd column.

This does not work, if the L column is specified more than once, of course!

\documentclass{book}%

\usepackage{array}
\usepackage{etoolbox}%


\newcounter{numberofrows}%

\pretocmd{\tabular}{\setcounter{numberofrows}{0}}{}{} % Resetting the number of rows counter

\def\tablecontent{%
  foo & bar \\ foo & bar \\ foo & bar \\
}%

\newcolumntype{L}{>{\stepcounter{numberofrows}}l}

\begin{document}

\begin{table}

\begin{tabular}{Ll}%
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\tablecontent
\end{tabular}
\end{table}

The last table has \thenumberofrows~rows


\begin{table}
\begin{tabular}{L*{100}{l}}%
Hello \tabularnewline
World!\tabularnewline
This \tabularnewline
is \tabularnewline
a \tabularnewline
nice  \tabularnewline
table \tabularnewline
\end{tabular}
\end{table}

The last table has \thenumberofrows~rows




\end{document}

enter image description here

New version, counting the number of \\ in a macro

Using the xstring package and its \StrCount macro, it's possible to count the \\ in the \t@b macro. (However, it will not check, if there is a trailing \\ at the end and there should be no intermixing of \tabularnewline and \\ (I did not test this actually).

\documentclass{book}%

\usepackage{etoolbox}%
\usepackage{xstring}%

\def\tablecontent{%
  foo & bar \\ foo & bar \\ foo & bar \\ foo & bar \\ foo & bar \\ %
}%

\gdef\newlinecounts{}%


\gdef\newlinemacro{\\}%


\newcommand{\GetNumberOfRows}[2][\newlinecounts]{%
\expandarg%
\StrCount{#2}{\newlinemacro}[#1]%
}%



\begin{document}

\GetNumberOfRows{\tablecontent}
Number of table rows (looking ahead): \newlinecounts 

And now finally the table\par

\begin{center}
\begin{tabular}{ll}%
\tablecontent
\end{tabular}
\end{center}

\end{document}

enter image description here

| improve this answer | |
  • You may want to mention that if the "data" in the tabular environment are entered in a more conventional way than is the case in your MWE, one needs to remember to specify an extra & before the `\` line break directives, so that the count incrementor has a chance to kick in. – Mico Oct 5 '14 at 10:48
  • A more easy way to ensure the counting is to use the first column for this ;-) – user31729 Oct 5 '14 at 11:09
  • You should definitely mention in full what the easiest method may be. – Mico Oct 5 '14 at 11:42
  • 1
    @Arkandias: Isn't it easier to count the number of \` in your \t@b` macro? – user31729 Oct 6 '14 at 19:26
  • 1
    @Arkandias: I added a version that counts the number of `\` such it's possible to know the number of rows ahead, under special conditions – user31729 Oct 7 '14 at 14:26
2

Is there a simple way to get the number of rows in a given tabular?

Use some of the ideas in Coounter for use in array/tabular cells. Below I've added \savetabularrows{<label>} which stores the number of rows using the traditional \label-\ref system. This allows you to \ref{<label>} it elsewhere in the document, or use \thetabrow directly (after the table):

enter image description here

% https://tex.stackexchange.com/q/65649/5764
\documentclass{article}
\usepackage{array}% http://ctan.org/pkg/array

\makeatletter
\def\insert@column{%
   \the@toks \the \@tempcnta
   \global\advance\c@tabcol\@ne
   \ignorespaces \@sharp \unskip
   \the@toks \the \count@ \relax}

\let\old@arraycr\@arraycr
\def\@arraycr{\global\c@tabcol\z@\global\advance\c@tabrow\@ne\old@arraycr}

\let\old@tabarray\@tabarray
\def\@tabarray{\global\c@tabrow\@ne\global\c@tabcol\z@\old@tabarray}

\newcommand{\savetabularrows}[1]{\edef\@currentlabel{\arabic{tabrow}}\label{#1}}

\makeatother
\newcounter{tabcol}\newcounter{tabrow}
\begin{document}

The following \verb|array| has \ref{arrayrows}~rows.

\[
  \begin{array}{ccc}
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol)
  \end{array}
  \savetabularrows{arrayrows}
\]

The following \verb|tabular| has \ref{tabularrows}~rows.

\begin{center}
  \begin{tabular}{ccc}
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) \\
    (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol) & (\thetabrow,\thetabcol)
  \end{tabular}%
  \savetabularrows{tabularrows}%
\end{center}

\end{document}​
| improve this answer | |
  • Thanks, but in order to determine the number or rows you have to print the tabular first, don't you? I would like to get the number of rows before printing the tabular... – Arkandias Oct 7 '14 at 11:48
  • 1
    @Arkandias: I've updated the answer to use the \label-\ref system. You can now use the row count wherever you wish via \ref{<label>}. – Werner Oct 7 '14 at 14:21

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.