我想处理这样的一对列表:
{{a1,b1},{a2,b2},{a3,b3}}
其中每对中的第一个项都是不同的。
因此,对于每一对{ai,bi}
我都需要以下数据:
{a1,a2,a3,...,an} % does not contain ai
ai
bi
然后我用这些调用一个宏
\foo{ai}{a1 \\ a2 \\ ... \\ an}{bi}
理想情况下,它应该不仅仅适用于成对的情况,即{{a1,b1,c1,d1},...}
以下是我迄今为止尝试过的:
\documentclass[preview,border=4mm,multi=true]{standalone}
\usepackage{pgf,pgffor}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\createlist}{mm}
{
\seq_clear_new:c { g_data_#1_seq }
\seq_gset_from_clist:cn { g_data_#1_seq } { #2 }
}
\NewDocumentCommand{\addtolist}{mm}
{
\seq_gput_right:cn { g_data_#1_seq } { #2 }
}
\NewDocumentCommand{\removefromlist}{mm}
{
\seq_gremove_all:cx { g_data_#1_seq } { #2 }
}
\NewDocumentCommand{\purgelist}{m}
{
\seq_gremove_duplicates:c { g_data_#1_seq }
}
\NewDocumentCommand{\printlist}{m}
{
\seq_use:cnnn { g_data_#1_seq } { ~\\ } { ~\\ } { ~\\ }
}
\cs_generate_variant:Nn \seq_use:Nnnn { c }
\cs_generate_variant:Nn \seq_gremove_all:cn {cx}
\ExplSyntaxOff
我有一个将使用这些数据组合的宏:
\newcommand{\playcardfamily}[2]{{
\foreach \thiscard in {#2}{
\createlist{relatives}{#2}
\removefromlist{relatives}{\thiscard}
% Needs to process to get the title in \thistitle and picture in
% \thispicture. relatives should contain only the titles.
\playcard{#1}{\thistitle}{\printlist{relatives}}{\thispicture}
}
}}
在文档中我有
\begin{document}
\playcardfamily{Family}{{Member1,picture1.png},{Member2,picture2.png},{Member3,picture3.png}}
\end{document}
为了澄清起见,我想从\playcardfamily{...}
这里得到的是:
\playcard{Family}{Member1}{Member2 \\ Member3}{picture1.png}
\playcard{Family}{Member2}{Member1 \\ Member3}{picture2.png}
\playcard{Family}{Member3}{Member1 \\ Member2}{picture3.png}
我愿意接受任何关于如何解决这个问题的提示和想法。
解决方案
通过对 egreg 的解决方案进行一些修改,我成功地制作出了我需要的卡片
答案1
我建议为每个家庭定义一个包含其成员和属性列表的序列。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\createfamily}{ m m }
{
\magnus_createfamily:nn { #1 } { #2 }
}
\NewDocumentCommand{\playcardfamily}{ m }
{
\magnus_playcardfamily:n { #1 }
}
\seq_new:N \l_magnus_tempa_seq
\seq_new:N \l_magnus_tempb_seq
\cs_new_protected:Npn \magnus_createfamily:nn #1 #2
{
\seq_new:c { g_magnus_family_ \tl_to_str:n { #1 } _seq }
\prop_new:c { g_magnus_family_ \tl_to_str:n { #1 } _prop }
\clist_map_inline:nn { #2 } { \magnus_populate:nn { #1 } { ##1 } }
}
\cs_new_protected:Npn \magnus_populate:nn #1 #2
{
\seq_set_split:Nnn \l_magnus_tempa_seq { , } { #2 }
\seq_gput_right:cx
{ g_magnus_family_ \tl_to_str:n { #1 } _seq }
{ \seq_item:Nn \l_magnus_tempa_seq { 1 } }
\prop_gput:cxx
{ g_magnus_family_ \tl_to_str:n { #1 } _prop }
{ \seq_item:Nn \l_magnus_tempa_seq { 1 } }
{ \seq_item:Nn \l_magnus_tempa_seq { 2 } }
}
\cs_new_protected:Npn \magnus_playcardfamily:n #1
{
\seq_set_eq:Nc \l_magnus_tempa_seq { g_magnus_family_ \tl_to_str:n { #1 } _seq }
\seq_map_inline:cn { g_magnus_family_ \tl_to_str:n { #1 } _seq }
{
\seq_set_eq:NN \l_magnus_tempb_seq \l_magnus_tempa_seq
\seq_remove_all:Nn \l_magnus_tempb_seq { ##1 }
\magnus_playcard:nnxx
{ #1 }
{ ##1 }
{ \seq_use:Nn \l_magnus_tempb_seq { \\ } }
{ \prop_get:cn { g_magnus_family_ \tl_to_str:n { #1 } _prop } { ##1 } }
}
}
% This should do something more useful, probably ;-)
\cs_new_protected:Npn \magnus_playcard:nnnn #1 #2 #3 #4
{
\tl_show:n { \#1~is~#1,~\#2~is~#2,~\#3~is~#3,~\#4~is~#4 }
}
\cs_generate_variant:Nn \prop_gput:Nnn { cxx }
\cs_generate_variant:Nn \magnus_playcard:nnnn { nnxx }
\ExplSyntaxOff
\createfamily{Family}
{
{Member1,picture1.png},
{Member2,picture2.png},
{Member3,picture3.png}
}
\playcardfamily{Family}
\stop
我在终端上得到的输出是
> \#1 is Family, \#2 is Member1, \#3 is Member2\\Member3, \#4 is picture1.png.
<recently read> }
l.71 \playcardfamily{Family}
?
> \#1 is Family, \#2 is Member2, \#3 is Member1\\Member3, \#4 is picture2.png.
<recently read> }
l.71 \playcardfamily{Family}
?
> \#1 is Family, \#2 is Member3, \#3 is Member1\\Member2, \#4 is picture3.png.
<recently read> }
l.71 \playcardfamily{Family}
?