1

I'm working on a document that contains several long tables. For consistency, I'd like all of these tables to be the same width. I'm assuming that the easiest way to accomplish this is to simple expand the tables to the with of the page. I'd like the contents of the first row of each table to be centered. I'd also like the rows to alternate colors.

I followed several answers I've found (specifically: This one, this one, and this one.) but they all seem to give an unsatisfactory result when a table does not have sufficient contents to fill the page. I'd like to somehow remove the blank space in the middle: Example Table

I'm looking to turn that into something closer to this (courtesy of MS Paint):Desired Result

Here is the code used to generate the above document:

\documentclass[12pt]{article}

\usepackage[table]{xcolor}
\usepackage{multicol}
\usepackage{longtable}

\begin{document}
    \rowcolors{2}{white}{gray!15}
    \def\arraystretch{1.1}
    \setlength\tabcolsep{10pt}
    \setlength\LTleft{0pt}
    \setlength\LTright{0pt}
    \begin{longtable}{@{\extracolsep{\fill}}|r|l|@{}}
        \caption{Example Table \label{et}}\\ \hline
        \rowcolor{gray!50}
        \multicolumn{1}{|c|}{\textbf{Example}} & \multicolumn{1}{|c|}{\textbf{Table}} \\ \hline
        \endfirsthead
        \caption{Example Table (Continued)}\\ \hline
        \rowcolor{gray!50}
        \textbf{Example} & \textbf{Table} \\ \hline
        \endhead
        \hline
        \multicolumn{2}{|c|}{Continued on next page...}\\ \hline
        \endfoot
        \hline
        \endlastfoot
        Example & Table \\
        Example & Table \\
        Example & Table \\
    \end{longtable}
\end{document}

Thoughts? Ideas? Criticism? Insults?

  • And this one? However, that you can combine longtable with tabularx or tabulary do not mean that fit to the text width is always a good idea. When the natural width of the table is munch less that the text width, as in this case, the best result is simply \begin{longtable}{rl}, no matter of how wide are the others tables. – Fran Sep 10 '18 at 5:22
  • @Fran, this could be just a MWE and possibly the real text is longer... But this way the OP shows the problem much easier/more intense. The actual problem is that the code doesn't compile!!! (Please fix your code to reproduce the output and run without errors) – koleygr Sep 10 '18 at 6:35
  • Stretching a small tabular to textwidth looks ugly. Drop the idea of consistency. – Ulrike Fischer Sep 10 '18 at 6:37
  • @koleygr, Code fixed. I somehow turned a "{" into a " " when I pasted everything in and added the indentation. – Cool BreeZe Sep 10 '18 at 11:14
  • @Fran, as @.koleygr stated, this is just a MWE. In the actual document, I have quite a few of these tables, all separated by a minimal amount of text. They're all roughly the same size, within maybe a cm of each other in width, and all fairly wide. The document looks sloppy as it is, and I'm trying to fix that. If it looked good the way it is, I wouldn't be looking to change it. Thank you for pointing to that other answer though, I hadn't seen it yet. – Cool BreeZe Sep 10 '18 at 11:19
2

When I want to unify the layout of tabulars I normally calculate the optimal widths and then use p-columns:

\documentclass[12pt]{article}

\usepackage[table]{xcolor}
\usepackage{multicol}
\usepackage{longtable,array}

\begin{document}
    \rowcolors{2}{white}{gray!15}
    \def\arraystretch{1.1}
    \setlength\tabcolsep{10pt}
    \newlength\colA
    \settowidth\colA{\textbf{Example}}
    \newlength\colB
    \setlength\colB{\dimexpr\textwidth-\colA-4\tabcolsep-3\arrayrulewidth}


    \begin{longtable}{@{}|>{\centering}p{\colA}|p{\colB}|@{}}
        \caption{Example Table \label{et}}\\ \hline
        \rowcolor{gray!50}
        \textbf{Example} & \centering \textbf{Table} \tabularnewline \hline
        \endfirsthead
        \caption{Example Table (Continued)}\\ \hline
        \rowcolor{gray!50}
        \textbf{Example} & \textbf{Table} \\ \hline
        \endhead
        \hline
        \multicolumn{2}{|c|}{Continued on next page...}\\ \hline
        \endfoot
        \hline
        \endlastfoot
        Example & Table \\
        Example & Table \\
        Example & Table \\
    \end{longtable}
\end{document}

enter image description here

  • For noonbs: Just add the bigger line of first column (or of first columns) in the place of "Example" at the command \settowidth\colA{\textbf{Example}} (+1) – koleygr Sep 10 '18 at 12:14
  • Meh... Close enough, I guess. – Cool BreeZe Sep 12 '18 at 1:19
1

This use xltabular, yet another package to use a longtable like tabularx.

The MWE show columns of different widths and alignements and some cells with long text, so you can see what is the result in each case in a very wide table to remark that the result is not beautiful in this way if there are little text).

MWE, page 1:

page1

MWE, page 2:

page2

\documentclass[12pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{lipsum,booktabs,array}
\usepackage[table]{xcolor}
\usepackage{xltabular}
\rowcolors{2}{white}{red!10}
\begin{document}
\lipsum[1-5]
\begin{xltabular}{\linewidth}{>{\centering\arraybackslash}X
!{{\color{green}\vrule}} % awful, only to show where the cell width
p{4cm}}
\caption{Some caption\label{aaa}}\\\midrule
\bfseries Example & \bfseries\hfil Table \\\midrule\endfirsthead
\caption*{Table \ref{aaa} (continued)}\\\toprule
\textbf{Example} & \textbf{Table} \\ \midrule
\endhead
Example & Table \\
Example & Table \\
Example long cell long cell long cell long cell long cell long cell &
Table long cell long cell long cell long cell long cell long cell \\
\raggedright Example long cell long cell long cell long cell long cell long cell &
{\centering Table long cell long cell long cell long cell long cell long cell\par} \\
Example & Table \\
Example & Table \\
Example & Table \\
Example & Table \\
Example & Table \\
Example & Table \\\bottomrule
\end{xltabular}
\lipsum[5-6]
\end{document}

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.