Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Would adding the nosep option to itemize help? –  egreg Jun 10 '13 at 22:18
    
@egreg: Well, it seems to do a lot more than help -- it seems to fix the problem completely!!! :-) Thanks. Testing in actual use case now... –  Peter Grill Jun 10 '13 at 22:27
add comment

2 Answers

up vote 4 down vote accepted

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

share|improve this answer
add comment

space is discarded at the top of pages, but not at the top of boxes. LaTeX emulates this behaviour, which is the main difference between minipage and \parbox

enter image description here

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

\bigskip


\noindent
\colorbox{blue!20}{%
    \begin{minipage}[t]{\dimexpr\linewidth-2\fboxsep\relax}%
            \HackVSpace%
            \begin{itemize}[label={\small \texttt{X:}}]%
                \item \HackStrut\texttt{some text fff yyy}%
            \end{itemize}%
            \HackVSpace%
            \end{minipage}}%
%
\newline
\colorbox{blue!20}{%
    \begin{minipage}[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{minipage}%
}%

\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.