![如何在 TikZ 中的节点内使用计数器?](https://linux22.com/image/275499/%E5%A6%82%E4%BD%95%E5%9C%A8%20TikZ%20%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9%E5%86%85%E4%BD%BF%E7%94%A8%E8%AE%A1%E6%95%B0%E5%99%A8%EF%BC%9F.png)
我正在尝试在预定义坐标列表上逐个绘制节点。
\documentclass{beamer}
\usepackage{caption}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,shapes,calc,intersections,through,backgrounds}
\begin{document}
\begin{figure}[b]
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\coordinate (a) at (1,4);
\coordinate (b) at (1,5);
\coordinate (c) at (1,6);
\coordinate (d) at (1,7);
\newcounter{count}
\setcounter{count}{1}
\foreach \name in {a,b,c,d}
{
\node<\the\value{count}->[circle,inner sep=0pt] at (\name) {$$};
\stepcounter{count}
}
\end{tikzpicture}
\end{figure}
\end{document}
我有上述 MWE,这是我在查看 TeX.SE 上一些其他答案后发现的,但我对 LaTeX 生成的错误消息感到困惑:
! LaTeX Error: Command \c@count already defined.
Or name \end... illegal, see p.192 of the manual.
任何正确的帮助都将不胜感激!
答案1
在 foreach 循环中,您可以使用有意的count
键来计算列表中的项目:无需定义其他计数器。
\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{frame}{}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\coordinate (a) at (1,4);
\coordinate (b) at (1,5);
\coordinate (c) at (1,6);
\coordinate (d) at (1,7);
\foreach \name[count=\mycount from 1] in {a,b,c,d}
{
\node<\mycount->[draw,circle,inner sep=0pt] at (\name) {$ $};
}
\end{tikzpicture}
\end{frame}
\end{document}
该count
键从 1 开始计数,但您可以借助 自定义初始值from=<initial value>
。此外,您还可以避免首先定义所有坐标:
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1cm,y=1cm]
\foreach \pos[count=\mycount] in {{(1,4)},{(1,5)},{(1,6)},{(1,7)}}
{
\node<\mycount->[draw,circle,inner sep=0pt] at \pos {$ $};
}
\end{tikzpicture}