4

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.

1
  • 2
    Welcome to TeX.SX!. this request is going to be hard for latex to comply with, and perhaps impossible if your document is a book, and the table appears in a chapter after the first. your best approach is probably to assign it to a page by itself, and position the input earlier than it is really needed. a tactic for making changes easier to cope with would be to put the data for the table in a file by itself and use \input{<filename>} to pull it in; it's much easier to move one line in your input file than a whole tabular structure. Commented Apr 7, 2014 at 20:33

2 Answers 2

2

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

0

Experimental; work in progress; unstable when mixing with regular floating

Oh yes, this is a typographer's nightmare! I am preparing a general solution to this task, I think I have found a solution (after a decade of trying it, I will tell you my story later), but I am not fully ready to publish/post it yet. I can offer you this solution with several known issues for now.

I am using header to deliver the typeset material on a specific page (\ifnum\thepage) and I am using \afterpage command from the afterpage package to place it from the header to the document body. Therefore I must substract 2 pages from the wanted page as you will see in a minute in the example.

The known limitations and issues are these:

  • This approach cannot be used when dealing with the first page. We need two pages to float the material from header back to the document. We would need to place it directly without header+afterpage if the wanted page would be page 1. It would flow to page 2 or even 3.
  • It is also not working when dealing with only two pages, in such a case only \afterpage without header must be used. Otherwise, it could flow to page 3.
  • If there is not enough typeset material (paragraphs) on the last page of the document which is the wanted page, the floating material will flow on page + 1. You can see that when commenting out line with \input{table2.tex}. The other table will flow on page 6, not on page 5. We must add one more paragraph manually, so please uncomment \par\ and that table will flow to the wanted page 5.
  • We can place floating bodies (here, top, bottom, even page) to this floating procedure, but typesetting is becoming unstable as it depends on other typeset material in the document body. If we change [!b] to [!p] in the original example both floating bodies flow to undesired pages 6 and even 7.

My recommendation is not to use common floating in this absolute floating situation. Just use \begin{tabular} and \begin{figure} without additional parameters. It will be placed on top of the page, therefore specifying h or H is not necessary, specifying b is quite stable (it depends on other parts of the document), but using p is unstable in many cases.

In the following example there are two floating tables, flowing to page 3 and page 5. The first floating table is becoming real table 2 on page 5 (it is controlled externally by the \mypage count), the second floating table is becoming real table 1 on page 3 (it is controlled directly from the header by \ifnum\thepage=1, it is wanted page 3 minus 2).

%! *latex mal-floating-table.tex
\documentclass[a4paper]{article}
\usepackage{afterpage}
\usepackage{kantlipsum}
\usepackage{xcolor}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{filecontents}
%
\begin{filecontents*}{table1.tex}
\begin{table}[!ht]
\centering
\begin{tabular}{|c|c|c|}
  a&B&Cd\\
  e&F&Gh\\
  i&j&Kl\\
\end{tabular}
\caption{\color{blue}\bfseries My first floating table is becoming table~\ref{table1} on page~\pageref{table1}}\label{table1}
\end{table}%
\end{filecontents*}
%
\begin{filecontents*}{table2.tex}
\begin{table}[!b] % [!b] (working well) or [p] (unstable)
\centering
\begin{tabular}{|c|c|c|}
  a&B&Cd\\
  e&F&Gh\\
  i&j&Kl\\
\end{tabular}
\caption{\color{red}\bfseries My second floating table is becoming  table~\ref{table2} on page~\pageref{table2}}\label{table2}
\end{table}%
\end{filecontents*}

\newcount\mypage \mypage=5
\advance\mypage by -2% Wanted page minus 2...
\fancyhead[L]{%
%\typeout{mypage: \the\mypage}% Which page it is?
\ifnum\thepage=\mypage
  \afterpage{%
  \input{table1.tex}%
  }% End of \afterpage...
\fi% End of including table 1.
\ifnum\thepage=1% --> page 3
  \afterpage{%
  \input{table2.tex}% To be commented out...
  }% End of \afterpage...
\fi% End of including table 2.
}% End of \fancyhead...

\begin{document}
\kant[1-15]
%\par\ 
\end{document}

mwe

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .