如何将列表视为常规文本(关于垂直间距)?

如何将列表视为常规文本(关于垂直间距)?

语境: 我想将列表(逐项列举、枚举和说明)视为与垂直间距有关的常规文本。即:

  1. 如果列表开始一个段落,则\parskip预先插入垂直间距 — 否则无垂直间距;
  2. 列表项之间没有垂直间距;
  3. 如果列表结束一个段落,则\parskip在后面插入一个垂直间距 — 否则没有垂直间距。

我特别使用了在\topsep、\itemsep、\partopsep 和 \parsep - 它们各自是什么意思

需求 #1 已通过 解决\setlist{topsep=-\parskip,partopsep=\parskip}

需求 #2 已通过 解决\setlist{noitemsep}

问题: 要求 3 仍存在两个问题:

  • 添加垂直空间如果后者开始一个段落,则列表,即使这个列表是立即地后面跟着文本。(也就是说,不存在独立parbottomsep长度之类的东西)。
  • 如果一个新的段落在列表之后开始,本段不是前面带有\parskip

问题: 如何满足要求3?
(我目前使用手动补丁 - 参见下面的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}

答案1

这几乎不那么肮脏,但是它使用了提供的工具enumitem,玩弄了after钥匙:

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

在此处输入图片描述

答案2

这是一个可能的解决方案,使用 lua 获取源代码中的下一行并检查其是否为空。当然,它只适用于 lualatex。对于准空行(只有空格?),可能需要进行一些改进。

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

相关内容