具有唯一第一行的自定义枚举项列表

具有唯一第一行的自定义枚举项列表

我正在尝试创建一个“枚举注释”列表类型,如下所示:

在此处输入图片描述

这是我当前的方法,它有效,但也存在一些问题:

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{xparse}

\ExplSyntaxOn

\makeatletter
\NewDocumentCommand \labelstyle{m} {
  \int_compare:nNnT {\c@notesi} < {2} {Note:~ }
  (\emph{#1})
}
\makeatother

\ExplSyntaxOff

\newlist{notes}{enumerate}{2}
\setlist[notes]{
  label = {\labelstyle{\alph*}},
  leftmargin = *,
  align = right
}

\begin{document}

\lipsum[1]

\begin{notes}[widest = c]
\item The product of two even permutations is even.
\item The product of two odd permutations is even.
\item The product of an odd and an even permutation is odd.
\end{notes}

\end{document}

第一行比其他行长,但包含第一个枚举标签;其余行仅包含枚举标签。

除了是否有更好的方法来解决这个问题这个一般性问题之外,我还想特别解决两个问题:

  • labelstyle命令中,我尝试使用\value{notesi}但失败并出现错误! Missing = inserted for \ifnum.

  • 我无法找到leftmarginlabelwidth等的正确组合以避免使用键widest。(我希望标签 (a)、(b) 等出现在彼此下方,但希望“注释:”与周围的文本对齐。)

答案1

稍有不同但与 Ulrike 的版本相似。

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{etoolbox}
\newcommand{\Note}{\ifnumequal{\value{notesi}}{1}{Note:\quad}{\phantom{Note:\quad}}}
\newlist{notes}{enumerate}{2}
\setlist[notes]{
leftmargin=*,
label=(\emph{\alph*}),
format=\Note,
align=left}
\begin{document}
\lipsum[1]
\begin{notes}
\item This is the first item.
\item This is the second item.
\item This is the third and longest item.
\end{notes}
\lipsum[1]
\end{document}

代码输出

答案2

不要在标签命令中放置格式化指令。请改用格式键。

\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum}
\usepackage{xparse}

\ExplSyntaxOn

\makeatletter

\NewDocumentCommand \labelstyle{m} {
  \int_compare:nNnTF {\value{notesi}} < {2} 
   {\makebox[\labelwidth][l]{Note:\hfill (\emph{#1})}}
   {(\emph{#1})}
}
\makeatother

\ExplSyntaxOff

\newlist{notes}{enumerate}{2}
\setlist[notes]{
  label = {\alph*},
  format= \labelstyle,
  leftmargin = 2.5cm,
  labelwidth=2cm,
  labelsep=0.5cm,
  align = right
}

\begin{document}

\lipsum[1]

\begin{notes}%[widest = c]
\item The product of two even permutations is even. 
\item The product of two odd permutations is even.

\item The product of an odd and an even permutation is odd. \lipsum[1]
\end{notes}

\end{document}

在此处输入图片描述

答案3

方法略有不同。将“注释:”放在一个框中,\myPrelistBox然后先写框,然后写一个\linewidth-\wd\myPrelistBox包含列表的宽度的缩略图。

\documentclass{article}
\usepackage{lipsum}
\parskip=1em
\parindent=0pt
\usepackage{calc}
\usepackage{enumitem}
\newsavebox\myPrelistBox
\sbox\myPrelistBox{Note: }
\newenvironment{notes}{%
  \usebox\myPrelistBox%
  \begin{minipage}[t]{\linewidth-\wd\myPrelistBox}
    \begin{enumerate}[label=(\emph{\alph*}),align=right]
    }{%
    \end{enumerate}
  \end{minipage}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\lipsum[1]
\begin{notes}
\item The product of two even permutations is even.
\item \lipsum[2]
\item The product of two odd permutations is even.
\item The product of an odd and an even permutation is odd.
\end{notes}

\lipsum[3]
\end{document}

在此处输入图片描述

相关内容