[Tex/LaTex] Fix tables at a given PAGE so that Table is not moving down when adding texts above it

positioningtables

I have looked over all the possible duplicate questions this whole day, and I still can not find an explicit solution to my problem.

I'm thinking to fix my table, wrote in tabular, at a given PAGE. I looked through all the instructions finding nothing about this. All I got from them is to float the table or figure by using either float or placeins pakages. I did that, and I can put my table separate from the texts, but I got a problem: I want to type some texts just before my table, as I'm typing, say over one page, the table just underneath the texts is moving down to a new page, for instance, before typing in the texts, page number of this table is page 3, after texts, it might go to page 5. I don't want to see this. What I want is keep this table at a particular page, say page 3, and no matter how many texts i would add before this table it always stays at page 3, which means that if the volume of my texts is bigger than two pages, the text will automatically jump to page 4 instead of starting occupy page 3 and pushing down the table to the next page.

The reason I want to do this is that sometimes I have to go back to my text and adding or deleting some words. I would end up the page just above the table or figure is filled with only a few sentences, leaving the entire space empty, and because at the same time the table is already rotated to sidewaytable due to its size, the table has to take a separate page and can not fill in the page even only with a few sentences.

To summarize, I'd like to fix my table at a given page, not simply floating it on a random page changing according to the text above it.

Best Answer

This approach will set the designated table at the top of the specified page, and seems to work with surrounding floats. It uses an everypage hook to add the table on the specified page. EDITED to automate the process. RE-EDITED to fix bug for case when another table(s) appeared on the same page, previously resulting in mis-numbering.

After \begin{document}, you should invoke

\fixedtable{page number}{table content, including \captionof{table}{}, as necessary}

You can play with the setting \aftertablegap as well as uncomment additional table rows to see that the result is well behaved.

\documentclass{article}
\usepackage{everypage}
\usepackage{lipsum}
\usepackage{caption}
\newlength\tableheight
\newlength\Xheaderheight
\setlength{\Xheaderheight}{\dimexpr\topmargin+\headsep+\headheight\relax}
\def\aftertablegap{12pt}% AFTER TABLE GAP

\newcommand\fixedtable[2]{%
  \global\newcommand\myfixedtable{%
    \begin{minipage}[t]{\textwidth}%
      \vspace{\Xheaderheight}%
      #2%
    \end{minipage}%
  }
  \setbox0=\hbox{\addtocounter{table}{-1}\myfixedtable}%
  \global\setlength\tableheight{\dp0}
  \AddEverypageHook{%
    \ifnum\thepage=#1%
      \edef\savethetable{\thetable}%
      \setcounter{table}{\fixedtableno}%
      \hspace{\oddsidemargin}%
      \myfixedtable%
      \vspace{\dimexpr-\Xheaderheight+\aftertablegap}%
      \global\addtolength\textheight{\dimexpr%
        -\Xheaderheight+\tableheight+\aftertablegap}%
      \setcounter{table}{\savethetable}%
    \fi%
  }
  \AddEverypageHook{%
    \ifnum\thepage=\numexpr#1-1\relax%
      \xdef\fixedtableno{\thetable}%
      \refstepcounter{table}%
      \global\addtolength\textheight{\dimexpr%
        +\Xheaderheight-\tableheight-\aftertablegap}
    \fi%
  }
}

\begin{document}
\fixedtable{3}{%
    \centering\captionof{table}{This is my caption}
    \begin{tabular}{|c|c|} 
      \hline This & is\\ \hline a & test\\ \hline A & B\\ \hline 
      C & D\\ \hline
%      C & D\\ \hline
%      C & D\\ \hline
%      C & D\\ \hline
%      C & D\\ \hline
%      C & D\\ \hline
%      C & D\\ \hline
    \end{tabular}%
}

\lipsum[1-8]
\begin{table}[ht]
  \caption{My table}
  \centering\rule{1in}{.5in}
\end{table}
\lipsum[9-12]
\begin{table}[ht]
  \caption{My table}
  \centering\rule{1in}{.5in}
\end{table}
\begin{table}[ht]
  \caption{My table}
  \centering\rule{1in}{.5in}
\end{table}
\lipsum[13-15]
\begin{table}[ht]
  \caption{My table}
  \centering\rule{1in}{.5in}
\end{table}
\lipsum[15-35]
\end{document}

enter image description here

Related Question