
情境: 我想像對待垂直間距的常規文本一樣對待清單(逐項列出、列舉和描述)。即:
- 如果清單開始一個段落,請
\parskip
預先插入垂直線 - 否則沒有垂直間距;- 列表項之間沒有垂直間距;
- 如果清單以段落結尾,則
\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}