有人知道 Ti 中的“不在集合中”命令是什么吗钾Z?我想要的是,让A成为一组乙是……的子集A. Ti 中的命令是什么钾Z 告诉:“对于每一个我在A但不是在乙?
我需要类似的东西:
foreach i in A
if i is in the set B do THIS, else do THAT
答案1
蛮力手段。
\documentclass{article}
\usepackage{tikz}
\def\setA{1,2,3,4,5}
\def\setB{2,4}
\newif\ifmatch
\begin{document}
\let\setC=\empty
\foreach \x in \setA {\matchfalse
\foreach \y in \setB {\ifnum\x=\y\relax \global\matchtrue \fi}%
\ifmatch\else
\ifx\empty\setC\relax
\xdef\setC{\x}%
\else
\xdef\setC{\setC,\x}%
\fi
\fi}
\setC% should contain 1,3,5
\end{document}
答案2
我们可以将(有序)集合表示为逗号分隔的列表。
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\foreachnot}{mm+m+m}
{% #1 = main list, #2 = exclusion list,
% #3 = to do if item is in main list but not in the exclusion list
% #4 = to do if item is in the main list and in the exclusion list
\erdos_forachnot:nnnn { #1 } { #2 } { #3 } { #4 }
}
\NewDocumentCommand{\definelist}{mm}
{
\clist_clear_new:c { l__erdos_list_#1_clist }
\clist_set:cn { l__erdos_list_#1_clist } { #2 }
}
\cs_new_protected:Nn \erdos_forachnot:nnnn
{
\cs_set_protected:Nn \__erdos_foreachnot_true:n { #3 }
\cs_set_protected:Nn \__erdos_foreachnot_false:n { #4 }
\clist_map_inline:cn { l__erdos_list_#1_clist }
{
\clist_if_in:cnTF { l__erdos_list_#2_clist } { ##1 }
{% item is in main list and in the exclusion list
\__erdos_foreachnot_false:n { ##1 }
}
{% item is in main list but not in the exclusion list
\__erdos_foreachnot_true:n { ##1 }
}
}
}
% initialize the two scratch functions
\cs_new_protected:Nn \__erdos_foreachnot_true:n {}
\cs_new_protected:Nn \__erdos_foreachnot_false:n {}
\ExplSyntaxOff
\begin{document}
\definelist{A}{1,2,3,4,5}
\definelist{B}{2,4}
\foreachnot{A}{B}{Item #1 is in A but not in B\par}{Item #1 is in A and in B\par}
\end{document}
第三和第四个参数是模板,其中当前项用 表示#1
。
如果需要嵌套这样的循环,则需要做更多的工作。