我想要一个宏,给定拆分出现的文本前给定项目数量和后给定项目数。也就是说,它\TypesetPartialList[2]{\MyList}
应该只排版列表中的前两个项目,而带星号的变体\TypesetPartialList*[2]{\MyList}
应该排版除前两个项目之外的项目。
下面的 MWE 的期望输出是
笔记:
- 你可以假设只有一参数中的列表
\TypesetPartialList
。 - 如果文本前和后该列表引入了复杂性,请假设传递给的内容
\TypesetPartialList
严格是单身的列表。
代码:
\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}
答案1
如果您删除前后的文本以及当您想要排版且不真正属于列表时可以重新插入的\begin{itemize}
和标签,那就简单多了。\end{itemize}
我不确定前后文本的用途是什么以及何时输出它们,所以我建议使用一个键值接口来打印它们。
\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}
这些选项存储在属性列表中,供打印时使用。列表被拆分,\item
然后在打印时重新插入。