5

Context: I want to treat lists (itemize, enumerate, and description) just like regular text for what regards vertical spacing. Namely:

  1. If the list starts a paragraph, insert a vertical \parskip beforehand — no vertical spacing otherwise;
  2. No vertical spacing between items of the list;
  3. If the list ends a paragraph, insert a vertical \parskip afterwards — no vertical spacing otherwise.

I have notably used information found in \topsep, \itemsep, \partopsep and \parsep - what does each of them mean.

Requirement #1 is solved with \setlist{topsep=-\parskip,partopsep=\parskip}.

Requirement #2 is solved with \setlist{noitemsep}.

Problem: There remain two issues with Requirement #3:

  • A vertical space is added after the list if the latter starts a paragraph, even if this list is immediately followed by text. (I.e. there is no such thing as an idependent parbottomsep length).
  • If a new paragraph starts after the list, this paragraph is not preceded with a \parskip.

Question: How to comply with Requirement #3?
(I currently use manual patches — see MWE below — but it is of course not satisfactory.)

enter image description here


MWE

\documentclass[parskip=half]{scrartcl}
    \usepackage{enumitem}
    \setlist{%
        topsep=-\parskip,
        partopsep=\parskip,
        noitemsep,
    }
\begin{document}
    This sentence is a paragraph on its own; there is thus a vertical parskip prior next paragraph.

    Following list is \emph{within} a paragraph, with preceding and appended text.
    \begin{itemize}
        \item One,
        \item Two,
        \begin{itemize}
            \item Two and a half;
            \item Almost three.
        \end{itemize}
        \item Three.
    \end{itemize}
    This text is appended to the previous list.
    However, following list starts a new paragraph on its own.

    \begin{enumerate}
        \item Did you notice the vertical spacing preceding this list?
        \item Two,
        \begin{enumerate}
            \item Two and a half;
            \item Almost three.
        \end{enumerate}
        \item Three.
    \end{enumerate}
%   \vspace{-\parskip}                                  %quick and dirty solution
    \textbf{There shouldn't be a vertical spacing here.}
    This text is appended to the previous list too.

    And finally, a list with preceding text only.
    \begin{itemize}
        \item One,
        \item Two,
        \begin{itemize}
            \item Two and a half;
            \item Almost three.
        \end{itemize}
        \item Three.
    \end{itemize}

%   \null\par                                            %quick and dirty solution
    \textbf{There should be a vertical spacing here.}
    This is a new paragraph.
    It should thus be preceded with parskip.
\end{document}
Bernard
  • 271,350
ebosi
  • 11,692

2 Answers2

3

This is hardly less dirty, but it uses the tools provided by enumitem, playing with the after key:

\documentclass[parskip=half]{scrartcl}
\usepackage{enumitem}
\setlist{%
    topsep=-\parskip,
    partopsep=\parskip,
    noitemsep,
}

\begin{document}

This sentence is a paragraph on its own; there is thus a vertical parskip prior next paragraph.

Following list is \emph{within} a paragraph, with preceding and appended text.
\begin{itemize}
    \item One,
    \item Two,
    \begin{itemize}
        \item Two and a half;
        \item Almost three.
    \end{itemize}
    \item Three.
\end{itemize}
This text is appended to the previous list.
However, following list starts a new paragraph on its own.

\begin{enumerate}[after =\vspace*{-\partopsep}]
    \item Did you notice the vertical spacing preceding this list?
    \item Two,
    \begin{enumerate}
        \item Two and a half;
        \item Almost three.
    \end{enumerate}
    \item Three.
\end{enumerate}
\textbf{There shouldn't be a vertical spacing here.}
This text is appended to the previous list too.

And finally, a list with preceding text only.
\begin{itemize}[after = \vspace*{\partopsep}]
    \item One,
    \item Two,
    \begin{itemize}
        \item Two and a half;
        \item Almost three.
    \end{itemize}
    \item Three.
\end{itemize}


\textbf{There should be a vertical spacing here.}
This is a new paragraph.
It should thus be preceded with parskip.

\end{document} 

enter image description here

Bernard
  • 271,350
1

Here is a possible solution using lua to get the next line in the source and check its emptiness. Of course, it only works with lualatex. It may need a bit of refining for quasi-empty lines (only spaces ?).

\documentclass{article}

\usepackage{luacode} \begin{luacode} function manageNextLine(s) luatexbase.remove_from_callback("process_input_buffer", "manageNextLine") if s == "" then return "\leavevmode\par" else return s end end \end{luacode}

\parskip=10ex % to see it better

\usepackage{enumitem} \setlist{noitemsep, topsep=-\parskip, partopsep=\parskip, after=\luadirect{luatexbase.add_to_callback("process_input_buffer", manageNextLine , "manageNextLine")} }

\begin{document} Para

Para2 \begin{itemize} \item 1 \item 2 \end{itemize} % Continuing the same para.

Para3 \begin{itemize} \item A. \item B. \end{itemize}

Suite with vertical spacing.

\end{document}

ysalmon
  • 1,160