表格单元格中逐项列出之前的 vspace,表格与长表

表格单元格中逐项列出之前的 vspace,表格与长表

tabular在以下 MWE 中,我们可以看到和之间的差异longtable,就单元格内项目化之前的垂直空间而言:

\documentclass[12pt]{article}
\usepackage{longtable,enumitem}
\begin{document}
\begin{longtable}{| p{3cm} |p{6cm}|}
\hline%
text &  
\begin{itemize}[topsep=0ex,partopsep=0ex]
\item Produkt und Quotient
\item Produkt und Quotient
\end{itemize}
\\ \hline 
\end{longtable}

\begin{tabular}{| p{3cm} |p{6cm}|}
\hline%
text &  
\begin{itemize}[topsep=0ex,partopsep=0ex]
\item Produkt und Quotient
\item Produkt und Quotient
\end{itemize}
\\ \hline 
\end{tabular}

\end{document}

我希望 longtable 中的 vspace 与 tabular 中的相同。顺便说一句,我将和都设置topseppartopsep0,只是因为我无知,我真的不知道应该使用哪一个……

答案1

哦!抱歉,我认为这是一个未报告的错误longtable

Longtable 插入一个支柱,使文本和任意一行\hline或前一行之间保持最小间隔。

它使用以下代码实现此目的

\def\LT@startpbox#1{%
  \bgroup
    \let\@footnotetext\LT@p@ftntext
    \setlength\hsize{#1}%
    \@arrayparboxrestore
    \vrule \@height \ht\@arstrutbox \@width \z@}

如果你查看array包中的等效代码,你会发现

\def\@startpbox#1{\bgroup
%    \end{macrocode}
%    The argument is the width of the box. This information has to be
%    assigned to =\hsize=.
%    Then we assain default values to several parameters used in a
%    \textsf{parbox}.
% \changes{v2.3k}{1998/05/12}{Use \cs{setlength} to set \cs{hsize},
%      so that the calc package can be applied here (pr/2793)}
%    \begin{macrocode}
  \setlength\hsize{#1}\@arrayparboxrestore
%    \end{macrocode}
%    Our main problem is to obtain the same distance between succeeding
%    lines of the \textsf{parbox}.
%    We have to remember that the distance between two \textsf{parboxes}
%    should be defined by =\@arstrut=. That means that it can be
%    greater than the distance in a \textsf{parbox}.
%    Therefore it is not enough to set a =\@arstrut= at the
%    beginning and at the end of the \textsf{parbox}. This would
%    dimension the distance
%    between first and second line and the distance between the two
%    last lines of the \textsf{parbox} wrongly.
%    To prevent this we set an invisible rule of height
%    =\@arstrutbox=
%    at the beginning of the \textsf{parbox}. This has no effect on the
%    depth of the first line. At the end of the \textsf{parbox} we set
%    analogously another invisible rule which only affects the depth
%    of the last line. It is necessary to wait inserting this strut
%    until the paragraph actually starts to allow for things like
%    =\parindent= changes via =>{...}=.
% \changes{v2.1c}{1992/12/14}{Use `everypar to insert strut}
%    \begin{macrocode}
   \everypar{%
      \vrule \@height \ht\@arstrutbox \@width \z@
      \everypar{}}%
   }
%    \end{macrocode}
% \end{macro}

重要的是最后几行通过使用来延迟添加支柱 \everypar

我觉得应该采取同样的措施,longtable但这只是晚了 20 年。

\def\LT@startpbox#1{%
  \bgroup
  \color@begingroup% Omit line if package date older than 2014/10/28
    \let\@footnotetext\LT@p@ftntext
    \setlength\hsize{#1}%
    \@arrayparboxrestore
   \everypar{%
      \vrule \@height \ht\@arstrutbox \@width \z@
      \everypar{}}%
%    \vrule \@height \ht\@arstrutbox \@width \z@
}

这样就使得两个表具有相同的垂直间距。

如果你这样做你能改变吗

\ProvidesPackage{longtable}
          [2004/02/01 v4.11 Multi-page Table package (DPC)]

\ProvidesPackage{longtable}
          [2004/02/01 v4.11 Multi-page Table package (DPC) + strut fix]

我怀疑经过这么长时间我无法改变longtable默认间距,但我应该添加一个选项来修复它。

答案2

为什么不为此使用专用的列类型(最初由 Donald Arseneau 在 ctt 上发布),否则间距始终是一个问题。

\documentclass[12pt]{article}
\usepackage{longtable,array,enumitem}
\makeatletter
%----  Enumerated columns
\newcolumntype{e}[1]{%
    >{\minipage[t]{\linewidth}\let\\\tabularnewline
      \enumerate
      \addtolength{\rightskip}{0pt plus 50pt}% for raggedright
      \setlength{\itemsep}{-\parsep}}%
    p{#1}%
    <{\@finalstrut\@arstrutbox\endenumerate\endminipage}}

%---- Itemized columns
\newcolumntype{i}[1]{%
    >{\minipage[t]{\linewidth}\let\\\tabularnewline
      \itemize
      \addtolength{\rightskip}{0pt plus 50pt}% for raggedright
      \setlength{\itemsep}{-\parsep}}%
    p{#1}%
    <{\@finalstrut\@arstrutbox\enditemize\endminipage}}
\makeatother

\begin{document}
\begin{longtable}{| p{3cm} |p{6cm}|}
    \hline%
    text &
    \begin{itemize}[topsep=0ex,partopsep=0ex]
    \item Produkt und Quotient
    \item Produkt und Quotient
    \end{itemize}
    \\ \hline
    text &
    \multicolumn{1}{i{6cm}|}{
    \item Produkt und Quotient
    \item Produkt und Quotient
    }\\ \hline
\end{longtable}

\begin{center}
\begin{tabular}{| p{3cm} |p{6cm}|}
    \hline%
    text &
    \begin{itemize}[topsep=0ex,partopsep=0ex]
    \item Produkt und Quotient
    \item Produkt und Quotient
    \end{itemize}
    \\ \hline
    text &
    \multicolumn{1}{i{6cm}|}{
    \item Produkt und Quotient
    \item Produkt und Quotient
    }\\ \hline
\end{tabular}
\end{center}

\end{document}

在此处输入图片描述

相关内容