我正在制作一个 TikZ 图片,其中有一个节点名称和坐标的列表,我想遍历列表,将第 i 个节点名称放在第 i 个坐标处。我的 MWE:
\documentclass{beamer}
\usepackage{tikz}
\tikzstyle{arrow} = [thick,->]
\tikzstyle{aAllele} = [text opacity=0, circle,text centered, fill=blue, text=white, minimum size = 0.5cm]
\tikzstyle{bAllele} = [text opacity=0, circle, text centered, fill=red, text=white, minimum size = 0.5cm]
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\def\names{{24,23,33,22,32,11,21,31,41,00,10,20,30,40}}
\def\coords{(2,4),(2,3),(3,3),(2,2),(3,2),(1,1),(2,1),(3,1),(4,1),(0,0),(1,0),(2,0),(3,0),(4,0)}
\foreach \x in {0,1,2,3,4}{%
\foreach \y in {0,1,2,3,4}{%
\fill[red] (\x,\y) circle (0.25cm);%
}
}
\foreach \i in {0,...,14}{%
\node (\names[\i]) [aAllele] at \coords[\i] {};%
}
\end{tikzpicture}
\end{frame}
\end{document}
在此图中,给出的圆圈coords
应为蓝色。我使用节点是因为我最终想用线连接它们;我已经通过迭代坐标使其工作,但是我需要命名它们,以便我可以使用线连接它们。将第 18-20 行替换为以下内容:
\foreach \coord in \coords{%
\node (\coord) [aAllele] at \coord {};
}
编译正确,但不是预期的结果,因为我无法轻松地通过名称连接节点(我认为),因为节点被赋予了用于生成它们的坐标的“名称”。
谢谢
答案1
我将使用节点来表示红色圆圈并为其命名:
\documentclass{beamer}
\usepackage{tikz}
\tikzstyle{arrow} = [thick,->]
\tikzstyle{aAllele} = [text opacity=0, circle,text centered, fill=blue, text=white, minimum size = 0.5cm]
\tikzstyle{bAllele} = [text opacity=0, circle, text centered, fill=red, text=white, minimum size = 0.5cm]
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\def\names{24,23,33,22,32,11,21,31,41,00,10,20,30,40}
%\def\coords{(2,4),(2,3),(3,3),(2,2),(3,2),(1,1),(2,1),(3,1),(4,1),(0,0),(1,0),(2,0),(3,0),(4,0)}
\foreach \x in {0,1,2,3,4}{%
\foreach \y in {0,1,2,3,4}{%
\node[bAllele](\x\y)at (\x,\y){};%
}
}
\foreach \i in \names{%
\node[aAllele] at (\i) {};%
}
\path[green,thick](22)
edge(23)
edge(32);
\end{tikzpicture}
\end{frame}
\end{document}
结果: