有誰知道Ti中「不在集合中」的命令是什麼kZ?我想要的是,讓A是一個集合並且乙是一個子集A。 Ti中的命令是什麼kZ 告訴:「對於每個我在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
。
如果需要嵌套此類循環,則需要做更多工作。