# Typeset Only First n Items or Items after n from a List

I would like to have macro that given splits the text that occurs before a given item count and after a given item count. That is \TypesetPartialList[2]{\MyList} should typese only the first two items in the list, and the starred variant \TypesetPartialList*[2]{\MyList} should typeset items except the first two items.

The desired output from the MWE below is

### Notes:

• You can assume that there is only one list in the parameter to \TypesetPartialList.
• If the text before and after the list introduces complexities, please assume that the content passed to \TypesetPartialList will stricly be a single list.

### Code:

\documentclass{article}

\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{xparse}

\newcommand{\MyList}{%
\textcolor{magenta}{This is the text before the list.}
\begin{itemize}
\item
This is the \textcolor{red}{first} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{red}{second} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{blue}{third} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{blue}{fourth} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{blue}{fifth} item of the list.
There is some additional text so that it occupies at least two lines.
\end{itemize}%
\textcolor{magenta}{This is the text after the list.}%
}%

\NewDocumentCommand{\TypesetPartialList}{%
s%   #1 = star means to typeset items AFTER item number in #2
O{-1}% #2 = item number to split the list at
%%          Ex. \TypesetPartialList{-1}{<list>} typsets ALL items
%%              \TypesetPartialList{2}{<list>} typsets ONLY items 1 and 2
%%              \TypesetPartialList*{2}{<list>} typsets ONLY items 3 and after
m%   #3 = list to split
}{%
#3%
}%

\begin{document}
\noindent\textbf{First Two Items:}\par
\TypesetPartialList[2]{\MyList}

\medskip\par
\noindent\textbf{Skipping First Two Items:}\par
\TypesetPartialList*[2]{\MyList}
\end{document}

• Note: Something is messed up with the image uploading. That is not the image that I included, nor is it somehting from my drive. Will report on meta. – Peter Grill Jan 19 at 19:33
• Image problem reported: Image Uploader Using Random Image. – Peter Grill Jan 19 at 19:49

It would be much simpler if you remove the text before and after and also the \begin{itemize} and \end{itemize} tags which you can reinsert when you want to typeset and don't really belong to the list.

I'm not sure what's the purpose of the texts before and after and when they should be output, so I propose a key-value interface for printing them.

\documentclass{article}

\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\definelist}{ m O{} +m }
{% #1 = list name
% #2 = key-value list
% #3 = list
\grill_lists_define:nnn { #1 } { #2 } { #3 }
}

\NewDocumentCommand{\printlist}{ s O{} m O{2} }
{
\IfBooleanTF { #1 }
{
\grill_lists_print:nnnn {#4+1} {\seq_count:c{ l_grill_lists_#3_seq }} { #3 } { #2 }
}
{
\grill_lists_print:nnnn {1} {#4} { #3 } { #2 }
}
}

\cs_generate_variant:Nn \seq_set_split:Nnn { c }
\cs_generate_variant:Nn \seq_pop_left:NN { c }

\keys_define:nn { grill/lists }
{
pre  .bool_set:N = \l__grill_lists_pre_bool,
post .bool_set:N = \l__grill_lists_post_bool,
pre  .default:n = true,
post .default:n = true,
}

\cs_new_protected:Nn \grill_lists_define:nnn
{
\prop_clear_new:c { l_grill_lists_#1_prop }
\prop_set_from_keyval:cn { l_grill_lists_#1_prop } { type=itemize, color={.}, #2 }
\seq_clear_new:c { l_grill_lists_#1_seq }
\seq_set_split:cnn { l_grill_lists_#1_seq } { \item } { #3 }
\seq_pop_left:cN { l_grill_lists_#1_seq } \l_tmpa_tl % discard the empty first item
}

\cs_new_protected:Nn \grill_lists_print:nnnn
{
\group_begin:
\keys_set:nn { grill/lists } { #4 }
\bool_if:NT \l__grill_lists_pre_bool
{
\textcolor{ \prop_item:cn { l_grill_lists_#3_prop } { color } }
{
\prop_item:cn { l_grill_lists_#3_prop } { pre }
}
}
\begin{ \prop_item:cn { l_grill_lists_#3_prop } { type } } % itemize or ...
\int_step_inline:nnn { #1 } { #2 } { \item \seq_item:cn { l_grill_lists_#3_seq } { ##1 } }
\end{ \prop_item:cn { l_grill_lists_#3_prop } { type } } % itemize or ...
\bool_if:NT \l__grill_lists_post_bool
{
\textcolor{ \prop_item:cn { l_grill_lists_#3_prop } { color } }
{
\prop_item:cn { l_grill_lists_#3_prop } { post }
}
}
\group_end:
}

\ExplSyntaxOff

\definelist{MyList}[
color=magenta,
pre={This is the text before the list.},
post={This is the text after the list.},
]{
\item
This is the \textcolor{red}{first} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{red}{second} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{blue}{third} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{blue}{fourth} item of the list.
There is some additional text so that it occupies at least two lines.
\item
This is the \textcolor{blue}{fifth} item of the list.
There is some additional text so that it occupies at least two lines.
}

\begin{document}

\noindent\textbf{First Two Items:}\par
\printlist{MyList}

\medskip

\noindent\textbf{Skipping First Two Items:}\par
\printlist*[pre,post]{MyList}

\medskip

\noindent\textbf{First Three Items:}\par
\printlist[pre]{MyList}[3]

\medskip

\noindent\textbf{Skipping First Three Items:}\par
\printlist*[post]{MyList}[3]

\end{document}


The options are stored in a property list to be used at print time. The list is split at \item which is then inserted back at print time.

• How about adding an option to change the list format between itemize (the default) to (say) enumerate by adding another key (listformat=itemize)? – Werner Jan 19 at 21:37
• @Werner There actually is, but it's not clear in this case what the numbering should be, I I didn't advertise it: type=enumerate. – egreg Jan 19 at 22:02