我需要使用令牌列表吗?
为了消除混淆,假设有两个分隔符:
- 输入分隔符(LaTeX 读取的输入)
- 输出分隔符(纸上所见的内容)
我想循环并从包含文件列表的文件中获取项目:
apples.tex
bananas.tex
strawberries.tex
例子
我曾尝试使用基于的 LaTeX3 代码如何创建一个可以附加并最终排版的变量列表?,但却令人紧张。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\listset}{O{default}m}
{
\seq_clear_new:c { l_macmadness_list_#1_seq }
\seq_set_from_clist:cn { l_macmadness_list_#1_seq } { #2 } % input delimiter? (due to clist? "comma list"?)
}
\NewDocumentCommand{\listappend}{O{default}m}
{
\clist_map_inline:nn { #2 }% input delimiter? (due to clist? "comma list"?)
{
\seq_put_right:cn { l_macmadness_list_#1_seq } { ##1 }
}
}
\NewDocumentCommand{\listprint}{+O{}}
{
\group_begin:
\keys_set:nn { macmadness/lists } { #1 }
\macmadness_print_list:V \l__macmadness_list_name_tl
\group_end:
}
\seq_new:N \l_macmadness_list_default_seq
\tl_new:N \l__macmadness_list_lastitem_tl
\keys_define:nn { macmadness/lists }
{
name .tl_set:N = \l__macmadness_list_name_tl,
name .initial:n = default,
action .code:n = \cs_set_eq:NN \__macmadness_action:n #1,
action .initial:n = \use:n,
separator .tl_set:N = \l__macmadness_list_separator_tl, % output delimiter
separator .initial:n = { ,~ }, % output delimiter
}
\cs_new_protected:Npn \macmadness_print_list:n #1
{
\seq_if_empty:cTF { l_macmadness_list_#1_seq }
{
$\langle$ \textit{empty~list} $\rangle$
}
{
% split off the last item
\seq_pop_right:cN { l_macmadness_list_#1_seq } \l__macmadness_list_lastitem_tl % print the items followed by the delimiter
\seq_map_inline:cn
\seq_map_inline:cn
{ l_macmadness_list_#1_seq }
{
\__macmadness_action:n { ##1 } % the item
\l__macmadness_list_separator_tl % output delimiter
}
% print the last item
\__macmadness_action:n { \l__macmadness_list_lastitem_tl }
}
}
\cs_generate_variant:Nn \macmadness_print_list:n { V }
\ExplSyntaxOff
\begin{document}
\listprint[%
separator={ , } % output delimiter
]%
\end{document}
答案1
如果您的输入在文件中,则需要抓取该文件:
\begin{filecontents*}{\jobname-list.tex}
apples.tex
bananas.tex
strawberries.tex
\end{filecontents*}
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\listset}{O{default}m}
{
\macmadness_listset:nn { #1 } { #2 }
}
\NewDocumentCommand{\listprint}{+O{}}
{
\group_begin:
\keys_set:nn { macmadness/lists } { #1 }
\macmadness_print_list:V \l__macmadness_list_name_tl
\group_end:
}
\seq_new:N \l__macmadness_list_separator_tl
\cs_new_protected:Nn \macmadness_listset:nn
{
\seq_clear_new:c { l_macmadness_list_#1_seq }
\tl_set_from_file:Nnn \l_macmadness_file_contents_tl {} {#2}
\seq_set_split:cnV { l_macmadness_list_#1_seq } { ~ } \l_macmadness_file_contents_tl
\seq_pop_right:cN { l_macmadness_list_#1_seq } \l_tmpa_tl
}
\cs_generate_variant:Nn \seq_set_split:Nnn {cnV}
\keys_define:nn { macmadness/lists }
{
name .tl_set:N = \l__macmadness_list_name_tl,
name .initial:n = default,
action .code:n = \cs_set_eq:NN \__macmadness_action:n #1,
action .initial:n = \use:n,
separator .tl_set:N = \l__macmadness_list_separator_tl, % output delimiter
separator .initial:n = { ,~ }, % output delimiter
}
\cs_new_protected:Npn \macmadness_print_list:n #1
{
\seq_if_empty:cTF { l_macmadness_list_#1_seq }
{
$\langle$ \textit{empty~list} $\rangle$
}
{
\seq_clear:N \l_macmadness_output_seq
\seq_map_inline:cn { l_macmadness_list_#1_seq }
{
\seq_put_right:Nn \l_macmadness_output_seq { \__macmadness_action:n { ##1 } }
}
\seq_use:NV \l_macmadness_output_seq \l__macmadness_list_separator_tl
}
}
\cs_generate_variant:Nn \macmadness_print_list:n { V }
\cs_generate_variant:Nn \seq_use:Nn { NV }
\ExplSyntaxOff
\begin{document}
\listset{\jobname-list}
\listprint
\listprint[separator={; }]
\end{document}