将一系列值分配给tikz中的变量

Assigning a range of values to a variable in tikz

我想将一系列值分配给 LaTeX 中的变量,以便在 tikzpicture 环境中的循环中使用。

在下面的代码中,我想替换这些行

\begin{tikzpicture}
\foreach \x in {1, 3, 5, 7}
    \foreach \y in {2, ..., 5}{

类似

first_range = {1, 3, 5, 7}
second_range = {2, ..., 5}
\begin{tikzpicture}
\foreach \x in first_range
    \foreach \y in second_range{

完整的可运行代码部分如下:

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}

\begin{document}

\begin{tikzpicture}
\foreach \x in {1, 3, 5, 7}
    \foreach \y in {2, ..., 5}{
        \ifthenelse{\(\x=1 \OR \x=7\) \AND \y = 3}{
            %\filldraw[fill=white] (\x, \y) circle (0.2);
            \node[] at (\x, \y) {\vdots};
        }{
            \filldraw[fill=red] (\x, \y) circle (0.2);      
        }   
    }
\end{tikzpicture}

\end{document}

您可以将列表存储在宏中:

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}

\def\first{1, 3, 5, 7}
\def\second{2, ..., 5} 

\begin{document}

\begin{tikzpicture}
\foreach \x in \first
    \foreach \y in \second {
        \ifthenelse{\(\x=1 \OR \x=7\) \AND \y = 3}{
            %\filldraw[fill=white] (\x, \y) circle (0.2);
            \node[] at (\x, \y) {\vdots};
        }{
            \filldraw[fill=red] (\x, \y) circle (0.2);      
        }   
    }
\end{tikzpicture}

\end{document}