我有以下代码
\draw (3, 3) circle (0.25) node[anchor=south, above=0.2cm]{$\vec{B}$};
\filldraw (3, 3) circle (0.05);
\draw (-3, 3) circle (0.25) node[anchor=south, above=0.2cm]{$\vec{B}$};
\filldraw (-3, 3) circle (0.05);
\draw (-3, -3) circle (0.25) node[anchor=south, above=0.2cm]{$\vec{B}$};
\filldraw (-3, -3) circle (0.05);
\draw (3, -3) circle (0.25) node[anchor=south, above=0.2cm]{$\vec{B}$};
\filldraw (3, -3) circle (0.05);
编写此代码会变得非常重复,最好通过 for 循环一次性处理它;但是,我不明白循环(和变量?)在 LaTeX 中如何工作以使其正常工作。
编辑:根据@user237299 的回答,我得出了以下结论,尽管我不确定这是否是最有效的解决方法:
\foreach \x in {-3, 3}{
\foreach \y in {-3, 3}{
\draw (\x, \y) circle (0.25) node[anchor=south, above=0.2cm]{$\vec{B}$};
\filldraw (\x, \y) circle (0.05);
}
};
答案1
您可以嵌套foreach
循环。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\path foreach \x in {-3,3} {foreach \y in {-3,3}
{(\x,\y) node[circle,fill,inner sep=0pt,minimum size=1mm,
label={above:{$\vec{B}$}}]{}}};
\end{tikzpicture}
\end{document}