使用后tex.stackexchange.com/a/373987/134144,我有一些代码可以制作一个跨多页的表格,并在每个表格的顶部显示页数。我遇到的问题是,如果表格实际上不覆盖多页,我就不需要页数。例如,如果表格从页面顶部开始并且可以放在它开始的页面上,则不应包含页数。如果同一张表格从页面中间开始,并且会溢出到新页面上,那么我希望显示页数。这是我目前所拥有的,但我不确定如何仅在表格跨多页时才显示某些内容。
\documentclass{article}
\usepackage{booktabs,supertabular}
\usepackage{lastpage}
\usepackage[table]{xcolor}
\begin{document}
\tablehead{
\multicolumn{2}{l}{\textbf{Table 1.1 (Sheet~\thepage~of~\pageref{LastPage})}}\\
\hline\multicolumn{1}{c|}{\cellcolor{gray!25}\textbf{Foo}} &
\multicolumn{1}{|c}{\cellcolor{gray!25}\textbf{Bar}}\\
\hline }
\tablefirsthead{
\multicolumn{2}{l}{\textbf{Table 1.1 (Sheet~\thepage~of~\pageref{LastPage})}}\\
\hline\multicolumn{1}{c|}{\cellcolor{gray!25}\textbf{Foo}} &
\multicolumn{1}{|c}{\cellcolor{gray!25}\textbf{Bar}}\\
\hline }
\tabletail{\hline}
\tablelasttail{\hline}
\begin{supertabular}{l|l}
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
Lorem Ipsum & John Doe \\[5pt] \hline
\end{supertabular}
\end{document}
答案1
我能够使用计数器和 if 条件来解决这个问题。当您向表中添加大量内容时,工作表计数将添加到表头。可能有更好的方法,但这是我的解决方案:
\documentclass{article}
\usepackage{booktabs,supertabular}
\usepackage{lastpage}
\usepackage[table]{xcolor}
\usepackage{refcount}
\begin{document}
Hello
\clearpage
%Begin table stuff
\newcounter{StartTableCounter}
\newcounter{EndTableCounter}
\setcounter{StartTableCounter}{\getpagerefnumber{startTable}}
\setcounter{EndTableCounter}{\getpagerefnumber{endTable}}
\newcommand{\TableHeader}{Table 1.1}
\ifnum\value{StartTableCounter}=\value{EndTableCounter}
\renewcommand{\TableHeader}{Table 1.1}
\else
\renewcommand{\TableHeader}{Table 1.1 (Sheet~\the\numexpr\value{page}+1-\the\numexpr\value{StartTableCounter}~of~\the\numexpr\value{EndTableCounter}+1-\the\numexpr\value{StartTableCounter})}
\fi
\tablehead{
\multicolumn{2}{l}{\textbf{\TableHeader}}\\\hline
\multicolumn{1}{c|}{\cellcolor{gray!25}\textbf{Foo}} &
\multicolumn{1}{|c}{\cellcolor{gray!25}\textbf{Bar}}\\\hline
}
\tablefirsthead{
\multicolumn{2}{l}{\textbf{\TableHeader}}\\\hline
\multicolumn{1}{c|}{\cellcolor{gray!25}\textbf{Foo}} &
\multicolumn{1}{|c}{\cellcolor{gray!25}\textbf{Bar}}\\\hline
}
\tabletail{\hline}
\tablelasttail{\hline}
\label{startTable}
\begin{supertabular}{l|l}
Lorem Ipsum & John Doe \\ \hline
Lorem Ipsum & John Doe \\ \hline
Lorem Ipsum & John Doe \\ \hline
\end{supertabular}
\label{endTable}
\clearpage
goodbye
\end{document}