2

I am trying to get the page number to display in the header of a tabularx on each page the table spans.

The problem I am having is that the headers are created at the same time the table is, they all display the same page number.

Is there a way to have the page number displayed as a part of the header of a tabularx?

MWE:

\documentclass{article}

\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{ltxtable} 
\usepackage{ltablex}
\usepackage{multirow}
\usepackage{array}
\usepackage{boldline}

\begin{document}

\newcounter{TableStartTableCounter}
\newcounter{TableEndTableCounter}
\setcounter{TableStartTableCounter}{\getpagerefnumber{TableStartTable}}
\setcounter{TableEndTableCounter}{\getpagerefnumber{TableEndTable}}

\begin{tabularx}{40pc}{X|X}
    \caption{Table 1 - Page \the\numexpr\value{page}} \\ \hline
    \nopagebreak \textbf{Header 1} & \textbf{Header 2} \\ \hline \endfirsthead
    \caption{Table 1 - Page \the\numexpr\value{page}} \\ \hline
    \nopagebreak \textbf{Header 1} & \textbf{Header 2} \\ \hline \endhead
    \label{TableStartTable} Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \\ \hline
    Content & Content \label{TableEndTable} \\ \hline
\end{tabularx}

\end{document}

And this gives these two pages:

enter image description here enter image description here

For the second image, I want it to say "Table 1 - Page 2".

CarLaTeX
  • 62,716
ShoeIace
  • 83
  • 6
  • 1
    There is already a page number at the bottom of the page. – Heiko Oberdiek May 30 '18 at 19:38
  • @HeikoOberdiek I also want the page number to be displayed in the table header though – ShoeIace May 30 '18 at 19:40
  • 2
    @ShoeIace: If your table starts on a page numbered 3, do you want the header to display 'Table 1 - Page 3' or 'Table 1 - Page 1'? Here are also some related topisc, regarding the first example: Use page number in longtable and regarding the second: How to print a page number to a repeating header of a longtable? – leandriis May 30 '18 at 19:44
  • Why? The user expects the page number at the bottom of the page and there is the page number as expected. An additional page number in the caption is rather confusing than helping IMHO. – Heiko Oberdiek May 30 '18 at 19:45
  • 1
    A header on the second table page like Table1: (continued) and/or a footer continued on the next page makes more sense IMHO. – Heiko Oberdiek May 30 '18 at 19:48
  • Also, take a look at the documentation of package booktabs how to make more professional looking tables with less lines. – Heiko Oberdiek May 30 '18 at 19:51
  • 1
    @leandriis Your second suggestion is effectively what I am shooting for. The problem I am having is actually getting the page number in the header of a tabularx. I was using supertabular before and the solution worked fine, but new requirements say I must use tabularx to accomplish the same task. And using tabularx, it will only print the page where the caption is defined. – ShoeIace May 30 '18 at 19:53
  • @HeikoOberdiek having "continued" would be easier and may look better, but sadly, that decision is out of my control. – ShoeIace May 30 '18 at 19:57
  • 1
    If your table has only two pages you can do \caption{Table 1 - Page 1} \\ \hline \nopagebreak \textbf{Header 1} & \textbf{Header 2} \\ \hline \endfirsthead \caption{Table 1 - Page 2} \\ \hline \nopagebreak \textbf{Header 1} & \textbf{Header 2} \\ \hline \endhead. – CarLaTeX May 31 '18 at 03:37
  • @CarLaTeX The solution I need should be extendable to any number of pages – ShoeIace May 31 '18 at 15:12

1 Answers1

1

You are using ltxtable which means that the tabularx is effectively a longtable. longtable head and foot are set in boxes at the start of the table so it is not possible to make them have varying text without extensive changes to the package.

David Carlisle
  • 757,742