我有个问题。我有一个列表,其中每个项目都包含相同的文本。
例如:
1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. SAMETEXT
2. Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi. SAMETEXT
3. Quisque vehicula consequat elit in mattis. SAMETEXT
每个项目都包含“SAMETEXT”。有什么方法可以自动显示此文本吗?
答案1
一种方法patching
,使用专门的enumerate
环境,防止修补\item
命令的“污染”。
\documentclass{article}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{xpatch}
\newcommand{\TextToAppend}{\textbf{Don't do this at home}}
\newlist{dontdothislist}{enumerate}{1}
\setlist[dontdothislist,1]{label={\arabic*.}}
\xapptocmd{\dontdothislist}{%
\xpretocmd{\enddontdothislist}{\ifnumgreater{\value{dontdothislisti}}{0}{\TextToAppend}{}}{\typeout{Yes}}{\typeout{Nope}}
\xpretocmd{\item}{\ifnumgreater{\value{dontdothislisti}}{0}{\TextToAppend}{}}{\typeout{Yes}}{\typeout{Nope}}
}{}{}
\begin{document}
\begin{multicols}{2}
\begin{enumerate}
\item Bla
\item Blo
\item Blu
\item Ble
\item Bli
\end{enumerate}
\begin{dontdothislist}
\item Bla
\item Blo
\item Blu
\item Ble
\item Bli Blu Blo Bla Ble
\end{dontdothislist}
\end{multicols}
\end{document}
答案2
除非你只是想做一些非常简单的事情,而不是修补每个列表
\documentclass{article}
\newcommand{\sametext}{The world is your oyster}
\begin{document}
\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit. \sametext
\item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi. \sametext
\item Quisque vehicula consequat elit in mattis. \sametext
\end{enumerate}
\renewcommand{\sametext}{The world is your kitchen}
\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit. \sametext
\item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi. \sametext
\item Quisque vehicula consequat elit in mattis. \sametext
\end{enumerate}
\end{document}
答案3
在这里,我定义了环境renumerate
,它将重复的文本作为强制参数。在我的 MWE 中,我调用来enumerate
展示的重新定义如何\item
不超出renumerate
环境范围。无需任何包!
\documentclass{article}
\let\svitem\item
\newcommand\newitem{\savetext\svitem}%
\newenvironment{renumerate}[1]
{%
\def\savetext{#1}%
\renewcommand\item{\let\item\newitem\svitem}%
\begin{enumerate}%
}
{\savetext\end{enumerate}}
\begin{document}
\begin{renumerate}{My repeated text}
\item doe.
\item reh.
\item mi.
\end{renumerate}
\begin{enumerate}
\item doe.
\item reh.
\item mi.
\end{enumerate}
\end{document}
嵌套renumerate
有效但可能不会达到预期的效果:
\documentclass{article}
\let\svitem\item
\newcommand\newitem{\savetext\svitem}
\let\savetext\relax
\newenvironment{renumerate}[1]
{%
\def\savetext{#1}%
\renewcommand\item{\let\item\newitem\svitem}%
\begin{enumerate}%
}
{\savetext\end{enumerate}}
\begin{document}
\begin{renumerate}{My repeated text}
\item doe.
\item reh.
\begin{renumerate}{\textbf{Now for something different!}}
\item X.
\item Y.
\item Z.
\end{renumerate}
\item mi.
\end{renumerate}
\end{document}
答案4
也可以\item
手动修补宏。在这种情况下,我分隔了修补后的\item
宏以收集所有项目,同时每次将所需的文本(由声明\everyitem
)插入到令牌寄存器中,然后使用普通enumerate
环境打印它:
\documentclass{article}
\makeatletter
\let\ltx@item\item
\newtoks\EI@toks
\newtoks\EI@items
\def\EI@item{\begingroup\catcode`\^^M=12 \EI@item@}
\bgroup\catcode`\^^M=12 %
\gdef\EI@item@#1^^M{%
\global\EI@items=\expandafter{\the\EI@items\ltx@item#1 \the\EI@toks}%
\endgroup}%
\egroup
\newcommand{\everyitem}[1]{\global\EI@toks{#1}}
\newenvironment{sametextlist}{%
\let\item\EI@item
}{%
\begin{enumerate}
\the\EI@items
\end{enumerate}
}
\makeatother
\parindent0em
\everyitem{SAMETEXT}
\begin{document}
The \verb+sametextlist+ environment will print the text declared in the \verb+\everyitem+ macro after every use of the \verb+\item+ macro:
\begin{sametextlist}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi.
\item Quisque vehicula consequat elit in mattis.
\end{sametextlist}
The usage of the \verb+\item+ macro isn't affected in the other environments:
\begin{enumerate}
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
\item Fusce blandit, orci vel cursus mollis, elit dui tristique purus, eget dapibus leo neque id mi.
\item Quisque vehicula consequat elit in mattis.
\end{enumerate}
\end{document}