我有以下 MWE:
\documentclass{article}
\usepackage{pgf, tikz}
\setlength{\parskip}{0.5cm}
\begin{document}
\textbf{Comprehensive knowledge storage follows the evolutionary sequence given by}
\foreach \x in {letter, word, phrase, paragraph, page, book, library} {\x, }
\textbf{while the access to it follows the reverse order.}
\textbf{Keywords:}
\foreach \x in {letter, word, phrase, paragraph, page, book, library} {1.~\x. }
\end{document}
在最后一行,不应该出现每个变量的数字1
,而应该是一个随着\foreach
循环而迭代的索引,就像这样:
所以我的问题是如何获取迭代索引?
谢谢你!
答案1
使用count=\n
:
\documentclass{article}
\usepackage{pgf, tikz}
\begin{document}
\textbf{Comprehensive knowledge storage follows the evolutionary sequence given by}
\foreach \x in {letter, word, phrase, paragraph, page, book, library} {\x, }% <-----
\textbf{while the access to it follows the reverse order.}
\bigskip
\textbf{Keywords:}
\foreach \x [count=\n] in {letter, word, phrase, paragraph, page, book, library} {\n.~\x. }
\end{document}
我没有尝试修复过满的顶行,但我删除了“library”后的双倍空格。
一个不同的解决方案expl3
:第二个参数\uselist
是一个模板,其中#1
引用索引和#2
当前项目。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\uselist}{mm}
{
\brasil_uselist:nn { #1 } { #2 }
}
\seq_new:N \l__brasil_uselist_seq
\cs_new:Nn \__brasil_uselist_do:nn { } % initialize
\cs_new_protected:Nn \brasil_uselist:nn
{
\cs_set:Nn \__brasil_uselist_do:nn { #2 }
\seq_set_from_clist:Nn \l__brasil_uselist_seq { #1 }
\seq_indexed_map_function:NN \l__brasil_uselist_seq \__brasil_uselist_do:nn
}
\ExplSyntaxOff
\begin{document}
\textbf{Comprehensive knowledge storage follows the evolutionary sequence given by}
\uselist{letter, word, phrase, paragraph, page, book, library}{#2, }%
\textbf{while the access to it follows the reverse order.}
\bigskip
\textbf{Keywords:}
\uselist{letter, word, phrase, paragraph, page, book, library}{#1.~#2. }
\end{document}