有没有简单的方法来获取给定表格的行数?
更准确地说,我有一个变量\t@b
,其中包含类似
foo & bar \\ foo & bar \\ foo & bar
我将在表格环境中显示此变量的内容。列数已确定,但如何找到结果表格的行数?
答案1
最简单的方法是使用一种新的列类型,它可以自动计算行数,例如通过使用包array
和\newcolumntype
。我假设有一个左对齐的第二列。
L
当然,如果该列被指定多次,那么这将不起作用!
\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}
\\
新版本,在宏中计算数量
使用xstring
包及其\StrCount
宏,可以对宏\\
中的进行计数\t@b
。(但是,它不会检查\\
末尾是否有尾随,并且不应混合\tabularnewline
和\\
(我实际上没有测试过这一点)。
\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}
答案2
有没有简单的方法来获取给定的行数
tabular
?
使用一些想法用于array
/tabular
单元格的计数器。下面我添加了使用传统系统\savetabularrows{<label>}
存储行数的行。这样您可以在文档的其他地方使用它,或者直接使用它(在表格之后):\label
\ref
\ref{<label>}
\thetabrow
% 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}
答案3
该软件包提供了与经典的类似的nicematrix
环境,但具有更多功能。{NiceTabular}
{tabular}
array
其中,在环境的末尾,LaTeX 计数器iRow
包含表格的总行数。
实际上,它不完全是一个 LaTeX 计数器,因为它在环境结束时会消失(按照设计)。因此,如果您想在环境之外使用它的值,您必须通过 中的指令将其值存储在另一个 LaTeX 计数器\CodeAfter
中{NiceTabular}
。
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\newcounter{NumberOfRows}
\begin{NiceTabular}{cc}
foo & bar \\
foo & bar \\
foo & bar \\
foo & bar \\
foo & bar \\
foo & bar \\
foo & bar \\
foo & bar \\
foo & bar \\
\CodeAfter
\setcounter{NumberOfRows}{\value{iRow}}
\end{NiceTabular}
The number of rows is \arabic{NumberOfRows}.
\end{document}