tags:

views:

153

answers:

3
+1  A: 

The difficult (or rather, non-trivial) part of doing this is parsing the comma-separated input. Several packages have implemented this; perhaps lipsum's implementation is simple enough to extract for your purposes.

After that, it's just a matter of stepping a counter every time you hit an item and either display the contents or not depending on how the comma-list is parsed.

Will Robertson
The difficult part got discussed recently here: http://stackoverflow.com/questions/2402354/split-comma-separated-parameters-in-latex
Christopher Oezbek
The looping itself is easy; see my latter answer for an example of the range-parsing that is a bit of a pain to write from scratch.
Will Robertson
+1  A: 

First use the foreach function to split a comma separated list given in

http://stackoverflow.com/questions/2402354/split-comma-separated-parameters-in-latex

You can simplify it by removing the constant parameter #2 used.

Then define your items as following (this is the way to build a hash in TeX):

\@namedef{item_1}{This is item 1}
\@namedef{item_2}{This is item 2}
\@namedef{item_3}{This is item 3}

Define a function to be used in foreach:

\def\printSingleItem#1{%
    \@ifundefined{item_#1}{%
         \PackageWarning{MyPackage}{Undefined Item #1}%
    }{%
         \@nameuse{item_#1}% This can be more fancy here, using formatting etc.
    }%
}

Last define printitems

\def\printitems#1{\foreach{\printSingleItem}{#1}}
Christopher Oezbek
Ah, I did not see that you need dynamic numbers. I guess this answer is thus not really helping you. Sorry.
Christopher Oezbek
Almost there. Two things: 1) can I have a few hundreds of these namedefs? I know that some resources in TeX are rather limited. 2) Can I build them from a counter, like \def\myitem#1{\namedef{item_\themycounter}{#1}} ? (Because I must have automatic numbering.)
AVB
I cannot answer the second question (but I think that should work), but the first one definitely works (I have 5500 of those for indexing emails in a mailing-list)
Christopher Oezbek
+2  A: 

Alright, then, here we go.

As you can see below, the main part of the coding is parsing the comma separated range input. After that, it's easy to check what number you're up to in the enumerate environment (or whatever) and conditionally display the item.

You can copy and paste from here on into an empty .tex document and it should just work:


%% First of all, I'm using the expl3 package to do most of this coding. Makes some things easier.

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn

%% Here's the function to loop over comma-list range input like -2,4-6,8,10-:

\prg_new_conditional:Nnn \i_in_range:nn {TF,T,F} {
  \bool_set_false:N \l_tmpa_bool
  \clist_map_inline:nn {#2} {
    \parse_range:w ##1 - \q_marker - \q_nil #1 \q_nil
  }
  \bool_if:NTF \l_tmpa_bool \prg_return_true: \prg_return_false:
}

%% And the auxiliary function to return whether the input argument is contained within the range:

\cs_set:Npn \parse_range:w #1 - #2 - #3 \q_nil #4 \q_nil {
  \tl_if_eq:nnTF {\q_marker}{#2}{
    \intexpr_compare:nT {#4=#1} {\bool_set_true:N \l_tmpa_bool}
  }{
    \tl_if_empty:nTF {#2}{
      \intexpr_compare:nT {#4>=#1} {\bool_set_true:N \l_tmpa_bool}
    }{

%% Sorry about this break here; Stackoverflow's display engine has a bug:

      \tl_if_empty:nTF {#1}{
        \intexpr_compare:nT {#4<=#2} {\bool_set_true:N \l_tmpa_bool}
      }{
        \intexpr_compare:nT {#4>=#1} {
          \intexpr_compare:nT {#4<=#2}
            {\bool_set_true:N \l_tmpa_bool}
        }
      }
    }
  }
}
\cs_generate_variant:Nn \i_in_range:nnTF {nV}

%% This is the command to input each item of your list:

\newcommand\numitem[1]{
  \i_in_range:nVTF {\value{enumi}+1}{\l_item_range_tl}{
    \item #1
  }{
    \stepcounter{enumi}
  }
}

%% And the enumerate environment with a range argument:

\newenvironment{someitems}[1]{
  \tl_set:Nn \l_item_range_tl {#1}
  \begin{enumerate}
}{
  \end{enumerate}
}
\ExplSyntaxOff

%% Finally, an example:

\begin{document}
\begin{someitems}{-2,4-6,8,10-}
\numitem{one}\numitem{two}\numitem{three}
\numitem{four}\numitem{five}\numitem{six}
\numitem{seven}\numitem{eight}\numitem{nine}
\numitem{ten}\numitem{eleven}
\end{someitems}
\end{document}
Will Robertson
After installing the expl3 package - it works!
AVB
Glad to hear it :)
Will Robertson