4

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

3
  • In my opinion, it would be better to add vertical padding to all cells. LateX tables often look quite tight. This can be done with the cellspace package.
    – Bernard
    Commented Feb 22, 2022 at 9:40
  • Yes, the cells are too tight by default. When I used the makecell package and \setcellgapes{<some-value>pt}, all cells got that padding, however the one in question above had more padding than the rest.
    – fishfin
    Commented Feb 22, 2022 at 12:41
  • You can define a \compress command as meaning \@minipagetrue and use >{\compress\arraybackslash}X for the column containing the list.
    – Bernard
    Commented Feb 23, 2022 at 10:22

1 Answer 1

4

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}
1
  • Thank you @Mico, it worked. However (and it has got nothing to do with your solution), when using minipage, I realized that I could have the table span across pages. I am also using parcol - dual columns throughout the document, and the table above I am discussing is in the right column (I am actually playing with a resume template that I am fully customizing, and in turn learning TeX). So now I need longtable, tabularx and parcol, and longtable is not playing well with parcol at all. I may need to give up some of my requirements in order not to make it too complex.
    – fishfin
    Commented Feb 22, 2022 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .