[Tex/LaTex] Spacing after itemize (without parskip package)

enumitemline-spacinglistsspacing

This piece of code seemed to be working fine for a long time, until I try to remove the parskip package. Without this package, it seems that I need to include two hacks:

  1. Add a \vspace{-\baselineskip} before and after the itemize environment.
  2. Need a \strut to be added.

So without parskip, and with these two hacks I obtain the desired results:

enter image description here

But without the hacks, which is obtained by ensuring the following are un-commented:

\renewcommand{\HackVSpace}{}%
\renewcommand{\HackStrut}{}%

this yields:

enter image description here

Surely there is a less hackish solution to this. And, what is special about parskip that I was getting the desired behavior.

Notes:

  • In actual usage I am only really using this for a single item and itemize provides the desired formatting, so I could do without itemize. But am still interested to know how to control itemize.

Code:

%\def\UseParskipPackage{}% Need to work with this commented.

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{showframe}

\ifdefined\UseParskipPackage
    \usepackage{parskip}
    \newcommand{\HackVSpace}{}%
    \newcommand{\HackStrut}{}%
\else
    \newcommand{\HackVSpace}{\vspace{-\baselineskip}}%
    \newcommand{\HackStrut}{\strut}%
\fi


%% Remove the hack - comment these out and things work fine
%% with or without the parskip package
    \renewcommand{\HackVSpace}{}%
    \renewcommand{\HackStrut}{}%


\begin{document}

\noindent
\colorbox{blue!20}{%
    \parbox[t]{\dimexpr\linewidth-2\fboxsep\relax}{%
            \HackVSpace%
            \begin{itemize}[label={\small \texttt{X:}}]%
                \item \HackStrut\texttt{some text fff yyy}%
            \end{itemize}%
            \HackVSpace%
            }}%
%
\newline
\colorbox{blue!20}{%
    \parbox[t]{\dimexpr\linewidth-2\fboxsep\relax}{%
            \HackVSpace%
            \begin{itemize}[label={\small \texttt{Y:}}]%
                \item \HackStrut\texttt{some more text fff yyy}%
            \end{itemize}%
            \HackVSpace%
            }%
}%
\end{document}

Best Answer

The parskip package changes the definition of \@listi, \@listii and \@listiii to use different values for the parameters \topsep, \itemsep and \parsep; it sets the first two to zero and the third to \parskip.

This explains why you don't get additional space in the box when parskip is loaded. When it isn't, the normal spacings are applied.

Solution, independent on parskip: add nosep to the options passed to itemize.

\documentclass{article}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{showframe}

\begin{document}

\noindent
\colorbox{blue!20}{%
  \parbox[t]{\dimexpr\linewidth-2\fboxsep\relax}{
    \begin{itemize}[label={\small \texttt{X:}},nosep]
    \item \texttt{some text fff yyy}
    \end{itemize}
  }%
}
\newline
\colorbox{blue!20}{%
  \parbox[t]{\dimexpr\linewidth-2\fboxsep\relax}{
    \begin{itemize}[label={\small \texttt{Y:}},nosep]
    \item \texttt{some more text fff yyy}
    \end{itemize}
  }%
}
\end{document}

enter image description here