4

I have the following 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}

Which outputs this: enter image description here

In the last line, instead of appearing the number 1 for each variable, it should be an index that iterates along with the \foreach loop, just like this:

enter image description here

So, my question is HOW TO GET THE INDEX OF ITERATION?

Thank you!

1 Answer 1

8

Use 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}

enter image description here

I didn't try and fix the overfull top line, but I removed the double space after “library”.

A different solution with expl3: the second argument to \uselist is a template where #1 refers to the index, and #2 to the current item.

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .