Controlling vertical padding in tabularx for itemize

itemizetabularx

I am having a small challenge in controlling the vertical spacing in tabularx. As you can see below, the top and bottom padding appear to be more only when using itemize (perhaps with other \begin{} \end{} also). I have removed all vertical separators in the itemize by using nosep as can be seen in the sample outside of tabularx below. So my guess is this is something happening within tabularx.

I tried using makecell, and also \extrarowheight (as suggested by some other threads), but the vertical spaces remain unchanged.

Would appreciate any help!

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

enter image description here

Best Answer

The enumitem package provides the options before=... and after=... to control what comes before and after the list itself. One can use these options to encase the list in a minipage; this is useful because, by default, any whitespace padding above and below a minipage is eliminated automatically.

As the example below shows, it's actually preferable to use the minipage with the [t] ("top") positinioning specifier, as that will assure that the first line in the first list item will be aligned with lines elsewhere in the table (here: Col0).

enter image description here

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