编辑

编辑

我想要一个循环来避免指定列表中的元素,理想情况下是这样的:

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

它应该对 \i=2,4,5,7 执行某些操作,但对 \i=1,3,6 不执行任何操作。

我对 TikZ 很陌生,但仍然希望我问的问题很清楚......

答案1

像这样吗?

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

产生与原始版本相同的输出:

宏版本

完整代码

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

相关内容