![使用 style=nextline 时,描述术语和项目列表之间出现空白行](https://linux22.com/image/279878/%E4%BD%BF%E7%94%A8%20style%3Dnextline%20%E6%97%B6%EF%BC%8C%E6%8F%8F%E8%BF%B0%E6%9C%AF%E8%AF%AD%E5%92%8C%E9%A1%B9%E7%9B%AE%E5%88%97%E8%A1%A8%E4%B9%8B%E9%97%B4%E5%87%BA%E7%8E%B0%E7%A9%BA%E7%99%BD%E8%A1%8C.png)
在我的 Sphinx 项目中,我有以段落或列表开头的描述列表。由于我希望描述术语单独成行,因此我使用了enumitem
with {style=nextline}
。问题是,如果后面跟着逐项列表,则会导致术语后面出现一行额外的空格。但由于是 Sphinx 自动生成的,因此我只能在序言中设置选项来修复它。是否存在可以防止出现额外空格的修复方法?
\documentclass{article}
\usepackage{enumitem}
\setlist[description]{style=nextline}
\begin{document}
\begin{description}
\item[{This is a term.}] \leavevmode
This is a description of the term, usually spanning multiple lines,
which should start on its own line.
\end{description}
\begin{description}
\item[{This is a term containing an itemize list.}] \leavevmode
\begin{itemize}
\item {}
This is an item with too much space between it and the term above.
\end{itemize}
\end{description}
\end{document}
更新:为了解决手头的问题,我让 Makefile 在整个文件中应用 karlkoeller 的修复。然而,这篇关于根据环境改变行为帮助我想出了这个几乎可行的解决方案。
\documentclass{article}
\makeatletter
\def\ifenv#1{
\def\@tempa{#1}%
\ifx\@tempa\@currenvir
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\usepackage{enumitem}
\setlist[description]{style=nextline}
\setlist[itemize]{before=\ifenv{description}{\vspace*{-1.5\baselineskip}}{\ldots}}
\begin{document}
\begin{description}
\item[{This is a term.}] \leavevmode
This is a description of the term, usually spanning multiple lines, which should start on its own line.
\end{description}
\begin{description}
\item[{This is a term containing an itemize list.}] \leavevmode
\begin{itemize}
\item {}
This is an item with too much space between it and the term above.
\end{itemize}
\end{description}
Text in between
\begin{itemize}
\item {}
This is a bare itemize.
\end{itemize}
\end{document}
应该发生的是,传递给的代码before
在itemize
列表开始确定它是否在 内时运行description
,并在必要时应用修复。但是,它\ldots
每次都返回错误条件 ( ),因为代码是在序言中而不是文档本身中进行评估的。我想这expandafter
就是答案,但我无法让它工作。
答案1
目前没有更好的想法...
你可以加
\setlist[itemize]{before=\vspace*{-1.5\baselineskip}}
在序言中。
请注意这将影响所有itemize
环境......
完整代码:
\documentclass{article}
\usepackage{enumitem}
\setlist[description]{style=nextline}
\setlist[itemize]{before=\vspace*{-1.5\baselineskip}}
\begin{document}
\begin{description}
\item[{This is a term.}] \leavevmode
This is a description of the term, usually spanning multiple lines,
which should start on its own line.
\end{description}
\begin{description}
\item[{This is a term containing an itemize list.}] \leavevmode
\begin{itemize}
\item {}
This is an item with too much space between it and the term above.
\end{itemize}
\end{description}
\end{document}
输出:
答案2
我按照 Dan 的建议,通过编程解决了这个问题,即让描述设置一个宏来\indesc
指示它处于活动状态,并itemize
在其为真时应用修复。然后 itemize 会\indesc
全局重置,以防止修复应用于描述中的后续列表。我能看到的唯一警告是描述文本中的文本是否出现在列表之前。
\documentclass{article}
\def\indesc{FALSE}
\makeatletter
\def\ifdesc{
\def\@istrue{TRUE}%
\ifx\indesc\@istrue
\vspace*{-1.0\baselineskip}
\gdef\indesc{FALSE}
\fi
}
\makeatother
\usepackage{enumitem}
\setlist[description]{style=nextline,before=\def\indesc{TRUE},after=\def\indesc{FALSE}}
\setlist[enumerate]{before=\ifdesc}
\setlist[itemize]{before=\ifdesc}
\begin{document}
\begin{description}
\item[{This is a term.}] \leavevmode
This is a description of the term, usually spanning multiple lines,
which should start on its own line.
\end{description}
\begin{description}
\item[{This is a term containing two itemize lists.}] \leavevmode
\begin{itemize}
\item {}
This is an itemize list with the extra space above removed.
\end{itemize}
Some text in between.
\begin{itemize}
\item {}
This is an itemize list left untouched.
\end{itemize}
\end{description}
Some text after the description.
\begin{itemize}
\item {}
This is another itemize list left untouched.
\end{itemize}
\end{document}