定义“可选”描述列表环境

定义“可选”描述列表环境

我正在尝试创建description环境的一个变体,强制将任何现有描述放在下一行。然而,当没有术语描述时,我似乎无法正确设置垂直间距。

如果列表中的项目没有任何正文,我该如何放弃其相应的 vskip?

平均能量损失

\documentclass{article}
\usepackage{mwe}
\usepackage{enumitem}
\newlist{classlist}{description}{1}
\setlist[classlist]{
  before     = \itshape\small,
  font       = \normalfont,
  leftmargin = 1em,
  labelsep   = 1em,
  itemsep    = 0pt,
  style      = nextline,
  labelwidth = 0pt % always bump to next line
}

\def\descr{Class description.  More class description.  There's so
  much description here.  It's very descriptive.  This class will be
  the most descriptive.  It'll be great.}

\begin{document}
\lipsum[1]
\begin{classlist}
  \item[Class Name]
  \item[Class Name] \descr
  \item[Class Name]
  \item[Class Name]
\end{classlist}

% This version displays correctly, but...
\begin{description}[                    %
  font=\normalfont,                  %
  leftmargin=1em,                 %
  labelsep=1em,                %
  before=\itshape\small,    %
  itemsep=0pt            %
]                     %
\item[Class Name]  % i'd like to avoid this manual ~\newline cruft
\item[Class Name] ~\newline \descr
\item[Class Name]
\item[Class Name]
\end{description}
\end{document}

输出

答案1

扩大标签宽度,使标签占据整个第一行:

\documentclass{article}
\usepackage{mwe}
\usepackage{enumitem}
\newlist{classlist}{description}{1}
\setlist[classlist]{
  before     = \itshape\small,
  font       = \normalfont,
  leftmargin = 1em,
  labelsep   = 1em,
  itemsep    = 0pt,
  labelwidth = \dimexpr\linewidth-1em % always bump to next line
}

\def\descr{Class description.  More class description.  There's so
  much description here.  It's very descriptive.  This class will be
  the most descriptive.  It'll be great.}

\begin{document}
\lipsum[1]
\begin{classlist}
  \item[Class Name]
  \item[Class Name] \descr
  \item[Class Name]
  \item[Class Name]
\end{classlist}

\end{document}

在此处输入图片描述

相关内容