3

Given the mwe:

\documentclass{article}
\usepackage{etoolbox}

\begin{document}

\listadd\terms{First item of the list} \listadd\terms{Second item which has punctuation such as, commas, potentially a period and maybe even other things!} \listadd\terms{And the last one}

\begin{enumerate}% \forlistloop{\item}{\terms}% \end{enumerate}%

\end{document}

What is the simplest way to achieve something like the following?

\begin{enumerate}%
\selectiveforlistloop{\item}{\terms}{1,3}%
\end{enumerate}%

Where the goal is to show only select elements, in this case, elements number 1 and 3.

2 Answers2

4
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\newcommand\selectiveforlistloop[4][]{%
\edef\iaux{0}%
\renewcommand*{\do}[1]{\edef\iaux{\the\numexpr\iaux+1}%
\expanded{\noexpand\in@{,\iaux,}{,#4,}}%
\ifin@
#2 ##1
\fi}%
\dolistloop{#3}%
}
\makeatother
\begin{document}

\listadd\terms{First item of the list}
\listadd\terms{Second item which has punctuation such as, commas, potentially a period and maybe even other things!}
\listadd\terms{And the last one}

\begin{enumerate}%
\selectiveforlistloop{\item}{\terms}{1,3}%
\end{enumerate}%
\end{document}

enter image description here

4

I can suggest a fairly general method based on expl3.

\documentclass{article}
\usepackage{amsthm} % for `\@addpunct

\ExplSyntaxOn

% borrow @addpunct \cs_new_eq:Nc \addpunct { @addpunct }

\NewDocumentCommand{\listadd}{mm} { % create the sequence where storing the items if not existent \seq_if_exist:cF { l_johnchris_list_#1_seq } { \seq_new:c { l_johnchris_list_#1_seq } } % store the items \seq_put_right:cn { l_johnchris_list_#1_seq } { #2 } }

\NewDocumentCommand{\listextract}{mmo} { % define the function to use according to the template \cs_set_protected:Nn __johnchris_list_extract:n { #2 } \IfNoValueTF { #3 } {% no optional argument, use the whole sequence \seq_map_function:cN { l_johnchris_list_#1_seq } __johnchris_list_extract:n } {% the optional argument is a list of item numbers; extract them in a temporary sequence \seq_clear:N \l__johnchris_list_temp_seq \clist_map_inline:nn { #3 } { \seq_put_right:Nx \l__johnchris_list_temp_seq { \seq_item:cn { l_johnchris_list_#1_seq } {##1} } } % do the mapping on the temporary sequence \seq_map_function:NN \l__johnchris_list_temp_seq __johnchris_list_extract:n } }

\ExplSyntaxOff

\begin{document}

\listadd{terms}{First item of the list} \listadd{terms}{Second item which has punctuation such as, commas, potentially a period and maybe even other things!} \listadd{terms}{And the last one}

\begin{enumerate} \listextract{terms}{\item #1\addpunct{.}} \end{enumerate}

\begin{enumerate} \listextract{terms}{\item \textit{#1}}[1,3] \end{enumerate}

\end{document}

The second argument to \listextract is a template for how to use the stored items, where #1 stands for the current item in the loop.

enter image description here

\addpunct is just to make an example of how the template can be used.

egreg
  • 1,121,712