Select only some items from a list in LaTeX
latex, list, select
Solution
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}
}{
\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}
Problem
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.