\foreach 包含两个或更多变量

\foreach 包含两个或更多变量

我经常发现自己想要使用一些功能,使我能够循环遍历多个集合,其中每个列表中的第 n 个项目与其他列表中的第 n 个项目配对在一起。

\begin{tikzpicture}
\draw [->] (0,0) -- (7,0) node[right] {$x$};
\draw [->] (0,0) -- (0,6) node[above] {$y$};
% Let's say I have several values to label on the graph
\foreach \x in {1,5,6,2,1,6,3,1}
\foreach \y in {0,3,4,0.5,2,3,1.5,4}
\foreach \point in {A,B,C,D,E,F,G,H}
\draw [fill = black] (\x,\y) circle (1 mm) node[left] {\point};
% This draws every combination from the three lists which is a huge mess!

% What I want it to draw is
\draw [fill = black] (4,2)      circle (1 mm) node[left] {A};
\draw [fill = black] (5,3)      circle (1 mm) node[left] {B};
\draw [fill = black] (6,4)      circle (1 mm) node[left] {C};
\draw [fill = black] (2,0.5)    circle (1 mm) node[left] {D};
\draw [fill = black] (1,2)      circle (1 mm) node[left] {E};
\draw [fill = black] (6,3)      circle (1 mm) node[left] {F};
\draw [fill = black] (3,1.5)    circle (1 mm) node[left] {G};
\draw [fill = black] (1,4)      circle (1 mm) node[left] {H};
\end{tikzpicture}

有没有一个干净、简单的方法来做到这一点?

答案1

像这样?

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \foreach\x/\y/\z in {4/2/A,5/3/B,6/4/C,2/.5/D,1/2/E,6/3/F,3/1.5/G,1/4/H}
  \draw [fill = black] (\x,\y)circle (1 mm) node[left] {\z};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

还有pgfplots

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=left,xmin=0,ymin=0, xmax=7.5, ymax=7.5]
\addplot+[only marks, nodes near coords=\csname @Alph\endcsname{\numexpr\coordindex+1}] 
     coordinates {(4,2)(5,3)(6,4)(2,0.5)(1,2)(6,3)(3,1.5)(1,4)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容