[Tex/LaTex] tabularx repeat headers at the start of each new page

environmentslongtablemacrostablestabu

I am trying to write a latex template for math dissertations for my university and I have gotten most of the formating built into the appropriate class file. However, one issue that I'm not sure how to solve is their requirements for tables. They need to be full-text-width (which I achieve fine with using tabularx environment), have dedicated headers (which is easy enough), and have a title/label (again, not too difficult). The part I'm having trouble with is the following: If the table extends to be past the current page, it should be split and continued on the next page with a reference to the table's title with "continued" appended, the headers need to be restated, and then the table continued. So far the following is my general idea of structuring a macro:

\documentclass{article}
\usepackage{tabularx}
\newcommand{\dissertationTable}[5]{
    \begin{table}
    \centering
        \caption{#4}
        \begin{tabularx}{\textwidth}{#1}
        #2\\ \hline% This is the header line
        #3\\ \hline% This is the body of the table, 
                   % which should include all the relevant line breaks etc.
        \end{tabularx}
        \label{#5}
    \end{table}
    }
\begin{document}

\dissertationTable{lXX}{These & Are & Headings}{These are & some rows& to see\\ how it & works & and to\\ demonstrate & how to & enter info.}{Some Table}{tableref}

\end{document}

The above works decently well to make a table, and I could reuse the #2 argument to reprint the heading if I could detect a pagebreak, and use the label in the #5 argument to cite the name (or just use the #4 caption directly) and append "continue". But I am not sure how to detect the pagebreak automatically. I also have issues trying to get the entire header in bold text and none of the content in bold text; the alignment tabs seem to break the scope of the \bfseries command.

I looked into this more generally and saw the longtable package and/or the tabu package, but I can't seem to figure out the syntax correctly to get what I'm after. I'm pretty sure the tabu package is capable of doing most/everything I want and I'm probably just being dumb, but I can't get it to compile more than a basic table without errors so I'm doing something stupid.

Regardless of whether one uses tabu or some clever macro, my goal is to generate some kind of command or environment that hides as much off the require formatting as possible (most of the students using this template will have very minimal TeX understanding, so the less they need to know the better). Ideally I'd use something that detects the number of align characters and allows me to set the left/right align characters within the command itself, instead of requiring it of the student (eg the first argument; {lXX}, in the MWE above).

Thus, if someone could either help me out with the syntax with longtable/tabu or explain how to detect a page break to make such a command myself, I'd very much appreciate it. And if there is a nice way to preformat the table for the alignment of each column without knowing how many columns there will be, that would be the cherry on top.

Thanks!

Best Answer

an example of using longtabu table with repeated headers and footers:

\documentclass[11pt]{book}
\usepackage[a4paper,showframe]{geometry}
\usepackage{longtable, tabu}
    \tabulinesep = ^2mm_2mm
\usepackage[skip=1ex]{caption}

\usepackage{lipsum}

\begin{document}

\begin{longtabu} to \linewidth { l X[1,l] X[1,l]}
    \caption{Some table}
\label{tablere}                             \\
    \tabucline [1pt] -
These & Are & Headings                      \\
    \tabucline -
\endfirsthead
%---------------------------------------------------------------%
    \caption{Some table \hfill(continued)}            \\
    \tabucline [1pt]-
These & Are & Headings                      \\
    \tabucline -
\endhead
%---------------------------------------------------------------%
\multicolumn{3}{r}{\footnotesize\textit{continued on the next page}}
\endfoot
    \tabucline [1pt]-
\endlastfoot
%---------------------------------------------------------------%
% table body
    \everyrow{\tabucline - }
first row   & \lipsum*[2]    & \lipsum*[2]  \\
%    \midrule
second row  & \lipsum*[13]   & \lipsum*[13] \\
%    \midrule
third row  & \lipsum*[23]   & \lipsum*[23]  \\
%    \midrule
forth row   & \lipsum*[32]   & \lipsum*[32] \\
%    \midrule
fifth row   & \lipsum*[42]   & \lipsum*[43] \\
\end{longtabu}

\end{document}

note:

  • for the final form of the table, the document must be compiled at least twice
  • tables used longtable and longtabu (as well others) can break between pages only between rows and not inside them. consequently at long cells' contents can arose empty spaces below some parts of tables
  • (edit) for use of longtabe from package tabu you should be aware, that this package is still broken. see @Ulrike Fischer comment below answer.
  • tabularx is intended for (short) tables (usually enclosed in table float environment), which can be fit on one page
  • tabularx can be extend to long table by use of the package ltablex:

    \documentclass[11pt]{book}
    \usepackage[a4paper,showframe]{geometry}
    \usepackage{booktabs,ltablex}    % <---
        \keepXColumns
    \usepackage[skip=1ex]{caption}
    
    \usepackage{lipsum}
    
    \begin{document}
    
    \begin{tabularx}{\linewidth}{ l X X}
        \caption{Some table}
    \label{tablere}                             \\
        \toprule
    These & Are & Headings                      \\
        \midrule
    \endfirsthead
    %---------------------------------------------------------------%
        \caption{Some table \hfill(continued)}            \\
        \toprule
    These & Are & Headings                      \\
        \midrule
    \endhead
    %---------------------------------------------------------------%
    \multicolumn{3}{r}{\footnotesize\textit{continued on the next page}}
    \endfoot
        \bottomrule
    \endlastfoot
    %---------------------------------------------------------------%
    % table body
    first row   & \lipsum*[2]    & \lipsum*[2]  \\
        \midrule
    second row  & \lipsum*[13]   & \lipsum*[13] \\
        \midrule
    third row  & \lipsum*[23]   & \lipsum*[23]  \\
        \midrule
    forth row   & \lipsum*[32]   & \lipsum*[32] \\
        \midrule
    fifth row   & \lipsum*[42]   & \lipsum*[43] \\
    \end{tabularx}
    
    \end{document}
    

result is similar as before. for more details about settings long tables read documentation of packages which you use.