2

The scenario comes from a usage of the solution found here.

The actual scenario is much more complex, involving custom environments and longtables. However, the problem can be produced using the following 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}

My adaptation works fine except when a table alone is forced to move to next page. The same scenario happens for similar objects, e.g., minipage. This works fine when there are some texts before the table in the same page.


enter image description here


enter image description here


enter image description here


enter image description here


enter image description here


enter image description here


enter image description here


enter image description here


enter image description here

  • Do you always start \anotherset after \clearpage? That is, the \section starts on a new page? – Werner Aug 18 '16 at 19:02
  • @Werner Yes, on the first one. In the actual scenario, I set the label to some new value based on a number of parameters. No, on the second one. \sections are not present there. It was used in the MWE only to improve readability. – Masroor Aug 19 '16 at 2:37
  • So you want to have sequential parts of your document to have a "page N of M" look. What defines the transition from on part to the next? – Werner Aug 19 '16 at 5:34
  • @Werner Yes, I am looking for that look. For switching from part to the next (for each part actually), a custom environment is used, where the counter is reset at start, the label is given a customized name based on some parameters, and the reference is saved at environment close. Each part renders a longtable, along with some starting text. The long table is followed by another shorter three row table. When this last short table is forced alone in the last page of part, the problem occurs. – Masroor Aug 19 '16 at 5:57
  • @Werner The long table is of dynamic length and is actually produced from a database. – Masroor Aug 19 '16 at 6:04
1

All this does is replace \label (and the associated \refstepcounter etc.) with \tplabel, which is based on the more general \mylable (see Refer to the "name" of an equation, while a List of Equations is generated using these names and Cite a theorem by its name and number).

If you look at \protected@write in source2e you will see \let\thepage\relax which causes \thepage to be locally unexpandable. It will finally be expanded at shipout. One can add an addition \let\thetp=\relax into one of the arguments, causing it to delay expansion until shipout as well.

\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}
  • Indeed it works. Please allow me a couple of days before accepting, as I always do. In the meantime, could you please add bit of explanation to let us understand how it is working. Perhaps an elaboration of your previous comment will be fine. – Masroor Aug 20 '16 at 4:14

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.