控制 tabularx 中 itemize 的垂直填充

控制 tabularx 中 itemize 的垂直填充

我在控制 tabularx 中的垂直间距时遇到了一个小挑战。如下所示,只有在使用 itemize 时(也许还有其他情况\begin{} \end{}),顶部和底部的填充才会显得更多。我已使用 itemize 删除了所有垂直分隔符,nosep如下面 tabularx 外部的示例中所示。所以我猜这是 tabularx 中发生的事情。

我尝试使用makecell,以及\extrarowheight(正如其他一些线程所建议的那样),但垂直空间保持不变。

非常感谢您的帮助!

\documentclass[12pt,a4paper]{article}

\usepackage{enumitem}
\usepackage{tabularx}
\usepackage{makecell}

\begin{document}
  %{\setlength{\extrarowheight}{0pt}             % doesn't work
  %\setcellgapes{0pt}                            % doesn't work either
  %\makegapedcells                               % ditto
  \begin{tabularx}{\linewidth}{|X|X|}
    \hline
    Col 1 & Col 2\\
    \hline
    \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{Note the vertical space above and below this cell.}\\
    \hline
    \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax\linewidth=\hsize}X|}{
    \begin{itemize}[nosep,align=left,leftmargin=*]
      \item The first item in itemize, note the vertical space above this. A bit more to show.
      \item The second item in itemize, note the vertical space below this.
    \end{itemize}}\\
    \hline
  \end{tabularx}
  \hrule
  \begin{itemize}[nosep,align=left,leftmargin=*]
    \item The first item in itemize, note the vertical space above this. A bit more to show.
    \item The second item in itemize, note the vertical space below this.
  \end{itemize}
  \hrule
\end{document}

在此处输入图片描述

答案1

enumitem包提供了选项before=...after=...来控制列表本身之前和之后的内容。可以使用这些选项将列表包含在 中minipage;这很有用,因为默认情况下, a 上方和下方的任何空格填充minipage都会自动消除。

如下例所示,实际上最好将(“top”)定位说明符minipage与它一起使用[t],因为这将确保第一个列表项的第一行与表中其他地方的行对齐(此处为:Col0)。

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\usepackage{tabularx}

\begin{document}
\noindent % <-- important
\begin{tabularx}{\linewidth}{|l|X|X|}
    \hline
    Col 0 & Col 1 & Col 2\\
    \hline
    abcxyz & 
    \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\relax}X|}{%
    Note the vertical space above and below this cell.}\\
    \hline
    abcxyz & 
    \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\linewidth=\hsize}X|}{%
    \begin{itemize}[nosep,align=left,leftmargin=*,
                    before={\begin{minipage}{\hsize}}, % <-- new
                    after={\end{minipage}}             % <-- new
                   ]
      \item The first item in itemize, note the vertical space above this. 
            A bit more to show.
      \item The second item in itemize, note the vertical space below this.
    \end{itemize}}\\
    \hline
    abcxyz & 
    \multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep+\arrayrulewidth\linewidth=\hsize}X|}{%
    \begin{itemize}[nosep,align=left,leftmargin=*,
                    before={\begin{minipage}[t]{\hsize}}, % <-- with "[t]"
                    after={\end{minipage}}%               % <-- new
                   ]%
      \item The first item in itemize, note the vertical space above this. 
            A bit more to show.
      \item The second item in itemize, note the vertical space below this.
    \end{itemize}}\\
    \hline
  \end{tabularx}
  \hrule
  \begin{itemize}[nosep,align=left,leftmargin=*]
    \item The first item in itemize, note the vertical space above this. A bit more to show.
    \item The second item in itemize, note the vertical space below this.
  \end{itemize}
  \hrule
  \begin{itemize}[nosep,align=left,leftmargin=*] % note use of "\strut" directives
    \item \strut The first item in itemize, note the vertical space above this. A bit more to show.
    \item The second item in itemize, note the vertical space below this.\strut
  \end{itemize}
  \hrule

\end{document}

相关内容