# How can I apply actions to specific items or after specific items in a sequence in LaTeX3?

Let's say I have a sequence of items. I would like to allow for the user to provide a key (here named "mainitems") that equals a number (default 1 as there is always at least 1 main item). Special formatting is applied to as many items as assigned to the mainitems key. The remaining items have other formatting applied.

# Code

The following code has been offered by egreg here: https://tex.stackexchange.com/a/231077/13552

There is an action function to apply actions to every item. What my question addresses is applying an action to a specified number of items (making items 1-2 bold and throwing in an \hfill before items 3-6, for example).

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\listset}{O{default}m}
{
\seq_set_from_clist:cn { l_macmadness_list_#1_seq } { #2 }
}
\NewDocumentCommand{\listappend}{O{default}m}
{
\clist_map_inline:nn { #2 }
{
\seq_put_right:cn { l_macmadness_list_#1_seq } { ##1 }
}
}
\NewDocumentCommand{\listprint}{+O{}}
{
\group_begin:
\keys_set:nn { macmadness/lists } { #1 }
\group_end:
}

{
name .initial:n      = default,
action .code:n       = \cs_set_eq:NN \__macmadness_action:n #1,
action .initial:n    = \use:n,
separator .initial:n = { ,~ },
}

{
{
$\langle$ \textit{empty~list} $\rangle$
}
{
% split off the last item
% print the items followed by the separator
\seq_map_inline:cn
{
\__macmadness_action:n { ##1 } % the item
}
% print the last item
}
}

\ExplSyntaxOff

\newcommand{\addquotes}[1]{#1'}
\newcommand{\inputlanguagefile}[1]{%
\input{UserManual_#1}%
}

\listset[languages]{de,da,en}

\begin{document}

\title{\listprint[name=languages]}

\maketitle

\section{Examples of list management}

\listset{apples}
\listappend{oranges,strawberries}

\listprint

\listprint[
separator={ $|$ },
]

\listprint[
name=languages,
]

\listset[empty]{}

\listprint[name=empty]

\section{The manuals}

\listprint[
name=languages,
separator=\par\bigskip,
action=\inputlanguagefile,
]

\end{document}

• Isn't it a bit vague? And are you sure this is the best approach? – egreg May 17 '15 at 15:45
• @egreg hmm. well I tried to be specific. I just wanted to know how it would be done. It seems like a reasonable thing to be able to do (access items and manipulate them based on counts). – Jonathan Komar May 17 '15 at 17:29
• @egreg Could you maybe help me to understand why you think it is vague? I think you are likely the most qualified person to answer this question. I really like the flexibility of keys, and I think they could have many other useful applications. I am thinking that the solution might include a pgf foreach loop or makedo loop. (of course that is based on my limited understanding of the LaTeX 3 syntax. – Jonathan Komar May 17 '15 at 20:48
• @macmadness86 I asked something similar some time ago, like \seq_set_item:Nnn \l_tmpa_seq { 3 } { <whatever> } and it seems not to be possible (nor planned to add). So may be a seq is not the perfect tool here (although, I think \seq_set_item:Nnn would be pretty nice). – Manuel May 17 '15 at 21:38
• @Manuel +1 That is not good news. It seems the only way to do it with a sequence is to create a loop that generates variables to store each item, and then another loop that "pops" each item into those variables. Then one or more loops to handle actions for the other items. Quite a bit of work for such a simple task. – Jonathan Komar May 18 '15 at 6:28