Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only some items from a list in LaTeX

Tags:

list

select

latex

I have a LaTeX document which is basically one big enumerate environment, with a few hundreds of items. I want to be able to issue a command like

\printitems{2,5,12,45-48}

which will output only the requested items.

A similar command \onlyslides is a part of slides.cls, but I cannot figure out what goes on there and adapt it to my needs.

I can replace the list of item's with a list of environments, like

\begin{myitem}
...
\end{myitem}

\begin{myitem}
...
\end{myitem}

with a \newcounter etc. if it helps to achieve my purpose — being able to print only some items with given numbers without cut-and-pasting. I can have the items in one file, and the \printitems command in another, if needed.

I can not put the numbers in the file — the file is constantly changing, and I need the automatic enumeration.

like image 252
AVB Avatar asked Mar 05 '10 18:03

AVB


2 Answers

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.

like image 182
Will Robertson Avatar answered Nov 15 '22 20:11

Will Robertson


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

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}}
like image 24
Christopher Oezbek Avatar answered Nov 15 '22 20:11

Christopher Oezbek