![在每个列表项末尾添加代码](https://linux22.com/image/285832/%E5%9C%A8%E6%AF%8F%E4%B8%AA%E5%88%97%E8%A1%A8%E9%A1%B9%E6%9C%AB%E5%B0%BE%E6%B7%BB%E5%8A%A0%E4%BB%A3%E7%A0%81.png)
我的问题实际上是对这种情况的概括:我正在使用xhfill
包用水平填充规则填充给定列表(通过包设置enumitem
)中每个项目对应的最后一行(排版很糟糕,但某些法律文件需要这样做)。我想知道是否有办法自动在每个列表项的文本后添加规则命令,而不必手动添加。我的观点实际上是是否有办法做到这一点,以便可以在该点添加任何命令或宏。
答案1
下次,请提供 Johannes_B 提到的最小工作示例。这样可以更轻松地提供帮助,并且我的答案更有可能真正有用(我不确定)。
这是您要找的东西吗?此图显示了两个“常规”枚举之间的“合法”枚举。
该代码使用了提供的before*
和选项:after*
enumitem
\documentclass{article}
\usepackage{enumitem}
\usepackage{xhfill}
\newcommand{\xfill}[2][1ex]{{%
\dimen0=#2\advance\dimen0 by #1
\leaders\hrule height \dimen0 depth -#1\hfill%
}}
\newlist{mylegal}{enumerate}{1}
\newif\iffirstlegal\firstlegalfalse% We need a toggle to track whether the item is first in the list or not
\setlist[mylegal]{%
label=\arabic*.,
before*={% This saves the standard definition of \item and then redefines it to add the fill if the item is not first on the list; if it is the first item, it omits the fill but toggles the firstlegal switch so the next item will trigger it
\let\olditem\item% save the standard definition of \item in a macro, \olditem
\firstlegaltrue% set the toggle for first item in the list to true
\def\item{\iffirstlegal\olditem\firstlegalfalse\else \xfill{.5pt}\mbox{}\olditem\fi}% new, temporary defition of \item
},
after*={% This takes care of adding the fill for the final item on the list and just makes sure that \item is reset to its standard definition
\xfill{.5pt}\mbox{}% fill for final item in list
\let\item\olditem% restore standard definition of \item
}%
}
\begin{document}
% Get a baseline so we know what the regular enumerated list looks like...
A regular enumeration:
\begin{enumerate}
\item An item
\item Another item
\end{enumerate}
% Demonstrate the effect of the new list environment
A legal enumeration:
\begin{mylegal}
\item An item
\item Another item
\end{mylegal}
% Make sure that \item is correctly reset by comparing the output here with the baseline
A regular enumeration:
\begin{enumerate}
\item An item
\item Another item
\end{enumerate}
\end{document}
请注意,其工作方式涉及重新定义\item
以添加填充第一的。这就是为什么有必要排除第一个列表项(我们不想在开始之前有一行)并特殊处理最后一个列表项(因为否则它根本不会得到一行)。