该场景来自于对解决方案的使用这里。
实际情况要复杂得多,涉及自定义环境和长表。但是,可以使用以下 MWE 生成问题。
\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcounter{tp}
\fancyfoot[L]{\stepcounter{tp}\thetp/\ref{tptotal\cc}}
\usepackage{lipsum}
\def\anotherset#1#2{\setcounter{tp}{0}\gdef\cc{#1}
\section{#2}}
\begin{document}
\anotherset{a}{This works fine, no table, text only}
\lipsum[1-10]
% Remember to adjust double increment of the same counter
\refstepcounter{tp}\label{tptotal\cc}\addtocounter{tp}{-1}
\clearpage
\anotherset{b}{This DOES NOT work, table ONLY is forced to move to next page}
\lipsum[1-9]
\begin{tabular}{c}
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
\end{tabular}
% Remember to adjust double increment of the same counter
\refstepcounter{tp}\label{tptotal\cc}\addtocounter{tp}{-1}
\clearpage
\anotherset{c}{This works as well, table remains in the same page}
\lipsum[1-20]
\begin{tabular}{c}
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
\end{tabular}
% Remember to adjust double increment of the same counter
\refstepcounter{tp}\label{tptotal\cc}\addtocounter{tp}{-1}
\end{document}
我的改编工作正常,除了当表格单独被强制移至下一页时。类似的对象(例如 minipage)也会发生同样的情况。当同一页中的表格之前有一些文本时,这种方法工作正常。
答案1
这一切所做的就是用 替换\label
(以及相关内容\refstepcounter
等)\tplabel
,它基于更通用的\mylable
(参见引用方程的“名称”,同时使用这些名称生成方程列表和通过名称和编号引用定理)。
\protected@write
如果你看看来源2e您将看到\let\thepage\relax
哪些原因导致\thepage
局部不可扩展。它最终将在发货时扩展。可以将附加项添加\let\thetp=\relax
到其中一个参数中,从而导致其也延迟扩展直到发货。
\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\newcounter{tp}
\fancyfoot[L]{\stepcounter{tp}\thetp/\ref{tptotal\cc}}
\usepackage{lipsum}
\def\anotherset#1#2{\setcounter{tp}{0}\gdef\cc{#1}
\section{#2}}
\makeatletter
\newcommand{\tplabel}[1]% #1 = label name
{\protected@write\@auxout{\let\thetp=\relax}{\string\newlabel{#1}{{\thetp}{\thepage}}}}
\makeatother
\begin{document}
\anotherset{a}{This works fine, no table, text only}
\lipsum[1-10]
% Remember to adjust double increment of the same counter
\tplabel{tptotal\cc}
\clearpage
\anotherset{b}{This DOES NOT work, table ONLY is forced to move to next page}
\lipsum[1-9]
\begin{tabular}{c}
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
\end{tabular}
% Remember to adjust double increment of the same counter
\tplabel{tptotal\cc}
\clearpage
\anotherset{c}{This works as well, table remains in the same page}
\lipsum[1-20]
\begin{tabular}{c}
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
One row\\
\hline
\end{tabular}
% Remember to adjust double increment of the same counter
\tplabel{tptotal\cc}
\end{document}