3

Does anyone know what is the command “not in the set” in TikZ? What I want is, let A be a set and B be a subset of A. What is the command in TikZ to tell: “for each i in A but not in B?

I need something like:

foreach i in A
  if i is in the set B do THIS, else do THAT
2
  • 1
    Welcome to TeX.SE!
    – Mensch
    Apr 23 at 17:28
  • 4
    Could you provide some code that shows how you intend the approach to look like? Also, what is the reason for this? There might be other ways to solve your issue ... Apr 23 at 17:36

2 Answers 2

4

Brute force approach.

\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}
3

We can represent (ordered) sets as comma separated lists.

\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}

The third and fourth arguments are templates where the current item is denoted by #1.

More work is needed if you need to nest such loops.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .