扩展长表以使用阴影行填充页面宽度

扩展长表以使用阴影行填充页面宽度

我正在处理一个包含多个长表格的文档。为了保持一致性,我希望所有这些表格的宽度都相同。我假设实现此目的的最简单方法是简单地将表格扩展到页面的宽度。我希望每个表格第一行的内容居中。我还希望行交替显示颜色。

我遵循了几个我找到的答案(具体来说:这个这个, 和这个。)但当表格内容不足以填满整个页面时,它们似乎都无法给出令人满意的结果。我想以某种方式删除中间的空白:示例表

我希望把它变成更接近这样的东西(由 MS Paint 提供):期望结果

以下是用于生成上述文档的代码:

\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}

想法?观点?批评?侮辱?

答案1

当我想统一表格的布局时,我通常会计算最佳宽度,然后使用 p 列:

\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}

在此处输入图片描述

答案2

这个用途xltabular,还有另一个使用 longtable 的包tabularx

MWE 显示不同宽度和对齐方式的列以及一些带有长文本的单元格,因此您可以在非常宽的表格中看到每种情况下的结果,请注意,如果文本很少,结果就不会很漂亮。

MWE,第 1 页:

第1页

MWE,第 2 页:

第2页

\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}

相关内容