3

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_clear_new:c { l_macmadness_list_#1_seq }
  \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 }
  \macmadness_print_list:V \l__macmadness_list_name_tl
  \group_end:
 }

\seq_new:N \l_macmadness_list_default_seq
\tl_new:N \l__macmadness_list_lastitem_tl

\keys_define:nn { macmadness/lists }
 {
  name .tl_set:N       = \l__macmadness_list_name_tl,
  name .initial:n      = default,
  action .code:n       = \cs_set_eq:NN \__macmadness_action:n #1,
  action .initial:n    = \use:n,
  separator .tl_set:N  = \l__macmadness_list_separator_tl,
  separator .initial:n = { ,~ },
 }

\cs_new_protected:Npn \macmadness_print_list:n #1
 {
  \seq_if_empty:cTF { l_macmadness_list_#1_seq }
   {
    $\langle$ \textit{empty~list} $\rangle$
   }
   {
    % split off the last item
    \seq_pop_right:cN { l_macmadness_list_#1_seq } \l__macmadness_list_lastitem_tl
    % print the items followed by the separator
    \seq_map_inline:cn
     { l_macmadness_list_#1_seq }
     {
      \__macmadness_action:n { ##1 } % the item
      \l__macmadness_list_separator_tl % the separator
     }
    % print the last item
    \__macmadness_action:n { \l__macmadness_list_lastitem_tl }
   }
 }
\cs_generate_variant:Nn \macmadness_print_list:n { V }

\ExplSyntaxOff

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

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


\begin{document}

\title{\listprint[name=languages]}
\author{macmadness86}

\maketitle

\section{Examples of list management}

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

\listprint

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

\listprint[
  name=languages,
  action=\addquotes,
]

\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
  • 2
    @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
  • 2
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.