4

I would like to have a loop which avoids elements in a specified list, ideally something like this:

\def\list{{1,3,6}}
\foreach \i in {1,...,7} {
  if "\i not in \list" do {...} else do {... }
 }

which should do something for \i=2,4,5,7 but not for \i=1,3,6.

I am very new to TikZ, but still hope that it's clear what I'm asking ...

3
  • Can you provide a complete example? And don't \def\list !
    – cfr
    Apr 16, 2016 at 23:16
  • why should I not \def\list ? what are the disadvantages, and what would be better? Apr 16, 2016 at 23:23
  • grep '\\list' $(kpsewhich -var TEXMFDIST)/tex/latex/base/*. (Or whatever the equivalent command would be on Windows or for MikTeX.)
    – cfr
    Apr 16, 2016 at 23:29

1 Answer 1

5

Something like this?

\newif\ifoneofthese
\oneofthesefalse
\foreach \i in {1,...,7}%
  {%
    \oneofthesefalse
    \foreach \j in {1,3,6}%
    {%
      \ifx\i\j \global\oneofthesetrue\fi
    }%
    \ifoneofthese\relax\else \i{} is not in the list. \fi
  }

conditional loops

EDIT

This works fine with a macro for the exclusion list:

\newcommand*\mylist{1,3,6}
\foreach \i in {1,...,7}%
  {%
    \oneofthesefalse
    \foreach \j in \mylist
    {%
      \ifx\i\j \global\oneofthesetrue\fi
    }%
    \ifoneofthese\relax\else \i{} is not in the list. \fi
  }

produces identical output to the original version:

macro version

Complete Code

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\newif\ifoneofthese
\oneofthesefalse
\foreach \i in {1,...,7}%
  {%
    \oneofthesefalse
    \foreach \j in {1,3,6}%
    {%
      \ifx\i\j \global\oneofthesetrue\fi
    }%
    \ifoneofthese\relax\else \i{} is not in the list. \fi
  }
\newcommand*\mylist{1,3,6}
\foreach \i in {1,...,7}%
  {%
    \oneofthesefalse
    \foreach \j in \mylist
    {%
      \ifx\i\j \global\oneofthesetrue\fi
    }%
    \ifoneofthese\relax\else \i{} is not in the list. \fi
  }
\end{document}
10
  • Thanks, that works nicely. But how can I replace {1,3,6} by a list variable? Writing \def\list{1,3,6} and then \foreach \i in \list seems not to work. You mentioned earlier that \def\list{1,3,6} should be avoided ... But since I would like to have the code run on arbitrary platforms, your grep replacement is not a good solution. Is there another way of doing it? Apr 17, 2016 at 9:53
  • @GandalfLechner \def redefines any command that happens to have the same name. In other words, it doesn't check if the command exists or not. So you might inadvertently overwrite an existing Tex command. \newcommand is the overlapping command that does the check and works in a similar way.
    – Alenanno
    Apr 17, 2016 at 15:25
  • If I remember correctly, \def after \begin{document} shouldn't be a problem, but I'm not sure at the moment.
    – Alenanno
    Apr 17, 2016 at 15:25
  • @GandalfLechner The grep is not an alternative to \def\list. It is an explanation for why you ought not do \def\list. \newcommand would indeed be preferable because compilation would fail with an error. And, yes, that really would be better here.
    – cfr
    Apr 17, 2016 at 15:33
  • @Alenanno ???? Why on Earth would \def be any better after \begin{document}? If you are overwriting a kernel command or TeX primitive or command from another package, you are still overwriting it. \newcommand would indeed be better here because compilation will fail: it will tell you \list is a bad choice.
    – cfr
    Apr 17, 2016 at 15:37

You must log in to answer this question.

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