如何实现表格宽度相同但列数不同

如何实现表格宽度相同但列数不同

我必须打印几个具有不同列数的表格。我使用 lingtable,因为有时这些表格每个包含超过 300 行。这就是问题所在。

即使定义固定的宽度(以厘米为单位),当列数不同时,表格的总宽度也会不同。

为什么?(我想我显然没有意识到一些填充或边距)

我如何控制列的“实际”宽度?

这是我现在使用的代码

\documentclass{report}
\usepackage{longtable}
\usepackage{array}
\begin{document}
    \begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
            \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline \hline
    \end{longtable}

    \begin{longtable}{|p{3cm}|p{3cm}|p{1cm}|p{1cm}|}
            \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline \hline
    \end{longtable}

    \begin{longtable}{|p{3cm}|p{3cm}|p{2cm}|}
            \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline \hline
    \end{longtable}
\end{document}

这是结果(但列宽总和是相同的!):

结果

答案1

\documentclass{report}
\usepackage{longtable}
\usepackage{array}
\newlength\mylength
\begin{document}

\setlength{\mylength}{\dimexpr 8cm -8\tabcolsep- 5\arrayrulewidth}

    \begin{longtable}{|*4{p{\dimexpr\mylength / 4\relax}|}}
            \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline \hline
    \end{longtable}

    \begin{longtable}{|*2{p{\dimexpr 3\mylength/8}|}
                       *2{p{\dimexpr   \mylength/8}|}}
            \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline \hline
    \end{longtable}

\setlength{\mylength}{\dimexpr 8cm -6\tabcolsep- 4\arrayrulewidth}
    \begin{longtable}{|*2{p{\dimexpr 3\mylength/8}|}
                          p{\dimexpr 2\mylength/8}|}
            \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline \hline
    \end{longtable}
\end{document}

答案2

您可以使用扩展包之一,它允许您将 的功能tabularx与 的功能相结合longtabletabularx允许您指定表格的宽度。它本身与 不兼容,longtable但有多个包允许您将其语法用于长表。例如:

\documentclass{report}
\usepackage{ltablex}
  \keepXColumns
\usepackage{array}
\begin{document}
    \begin{tabularx}{110mm}{|*{4}{X|}}
            \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline \hline
    \end{tabularx}

    \begin{tabularx}{110mm}{|*{2}{>{\hsize=1.5\hsize}X|}*{2}{>{\hsize=.5\hsize}X|}}
            \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline \hline
    \end{tabularx}

    \begin{tabularx}{110mm}{|*{2}{>{\hsize=1.125\hsize}X|}>{\hsize=.75\hsize}X|}
            \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline \hline
    \end{tabularx}
\end{document}

ltablex 固定宽度的表格

相关内容