如何定义一个枚举环境,当项目编号仅为 1 时省略标签,当项目编号多于 1 时照常显示?
答案1
您可以在框内排版环境以获取项目的数量,然后重新排版它:
\usepackage{environ}
\makeatletter
\NewEnviron{fenumerate}
{\setbox0=\vbox{\enumerate\BODY\endenumerate\expandafter}%
\expandafter\def\expandafter\@tempfenum\expandafter{\the\value{\@enumctr}}%
\begin{enumerate}
\ifnum\@tempfenum=\@ne\expandafter\def\csname label\@enumctr\endcsname{}\fi
\BODY
\end{enumerate}}
\makeatother
首先,环境收集环境的文本;然后在临时框中排版,最后将相关计数器(enumi
、enumii
或)的值存储在中enumiii
,用于“真正”排版。enumiv
\@tempfenum
用法:
\begin{fenumerate}
\item this won't get a number
\end{fenumerate}
\begin{fenumerate}
\item this will have a number
\item since it's followed by another item
\end{fenumerate}