坐标数组和字符串数组

坐标数组和字符串数组

我想使用一个坐标数组和一个名称数组,这两个数组都被声明为 Latex 宏。下面的代码可以工作,但我不明白为什么第一个数组\Tpos用单括号 {} 声明,而第二个数组必须用双括号 {{}} 声明。另一个奇怪的问题是循环索引范围从 1 到 3m(参见命令\draw (pt1) -- (pt2) -- (pt3)),而第二个数组的索引范围从 0 到 2(参见命令\Tname[\i-1])。哦,好吧……

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\Tpos{( 0.5, 0.0), ( 2.0, 0.0), (-1.0,-1.0)}
\newcommand\Tname{{"A", "B", "C"}}
\begin{document}
\begin{tikzpicture}
  \foreach \pos [count=\i] in \Tpos {
    \node [draw=black,circle,inner sep=2pt,minimum size=4mm] (pt\i) [at=\pos] {\pgfmathparse{\Tname[\i-1]}\pgfmathresult};
  }
  \draw (pt1) -- (pt2) -- (pt3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

欢迎!由于您正在比较两种截然不同的结构,即循环和数组,因此可能无法以您想象的方式回答问题。foreach在数组中,计数从零开始,而在默认情况下,count数组foreach从 1 开始,但您可以更改这一点:

\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{calc} % not needed here
\newcommand\Tpos{( 0.5, 0.0), ( 2.0, 0.0), (-1.0,-1.0)}
\newcommand\Tname{"A", "B", "C"}
\begin{document}
\begin{tikzpicture}
  \foreach \pos [count=\i starting from 0] in \Tpos {
    \node [draw=black,circle,inner sep=2pt,minimum size=4mm] (pt\i) 
    [at=\pos] {\pgfmathparse{{\Tname}[\i]}\pgfmathresult};
  }
  \draw (pt0) -- (pt1) -- (pt2);
\end{tikzpicture}
\end{document}

现在计数从 0 开始。至于双括号,这是因为一个来自\newcommand(or \def) 语法,另一个来自arraypgf 函数。在上面的例子中,我将其包裹起来\Tname\pgfmathparse{{\Tname}[\i]}但简短的回答是:是的,无论如何您都需要“双”括号。

附录:至于评论中的附加问题:您可能只使用数组来接近预期用途。如果您使用

\newcommand\Tarray{"( 0.5, 0.0)","( 2.0, 0.0)","(-1.0,-1.0)"}

然后可以添加一个坐标

  \path 
   node[draw=red,circle,inner sep=2pt,minimum size=4mm]
   [/utils/exec=\pgfmathparse{{\Tarray}[\myindex]},at=\pgfmathresult ]
   {\pgfmathparse{{\Tname}[\myindex]}\pgfmathresult};

完整示例:

\documentclass{article}
\usepackage{tikz}
\newcommand\Tarray{"( 0.5, 0.0)","( 2.0, 0.0)","(-1.0,-1.0)"}
\newcommand\Tname{"A", "B", "C"}
\begin{document}
\begin{tikzpicture}
   \foreach \i in {0,1,2} {
     \node [draw=black,circle,inner sep=2pt,minimum size=4mm] (pt\i) 
     [/utils/exec=\pgfmathparse{{\Tarray}[\i]},at=\pgfmathresult]
  {\pgfmathparse{{\Tname}[\i]}\pgfmathresult};
   }
  \draw (pt0) -- (pt1) -- (pt2);
  \pgfmathtruncatemacro{\myindex}{1}
  \path 
   node[draw=red,circle,inner sep=2pt,minimum size=4mm]
   [/utils/exec=\pgfmathparse{{\Tarray}[\myindex]},at=\pgfmathresult ]
   {\pgfmathparse{{\Tname}[\myindex]}\pgfmathresult};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

谢谢。我还有一个问题是如何通过 Tpos 数组访问其坐标来一次绘制一个点。不幸的是,到目前为止我的所有尝试都失败了:

\coordinate [at= {\pgfmathparse{{\Tpos}[8]}\pgfmathresult}, name=pt8];

结果是错误:

! Package tikz Error: Cannot parse this coordinate.

如果我删除封闭的 {},也会出现同样的错误。更明显的代码是:

\node [draw=red,rond] (pt8) at {\pgfmathparse{{\Tpos}[8]}\pgfmathresult} {H};

产生完全相同的错误。我在另一篇文章中读到 \coordinate () at () 命令无法解析宏,因此我尝试改用 \coordinate [at=]。它实际上适用于宏,但不适用于数组访问宏。

理想情况下,我想写类似这样的内容:

\node [draw=red,rond] (pt8) at {\pgfmathparse{{\Tpos}[8]}\pgfmathresult} {\Tname[8]};

以便将 pt8 点设为红色。我这里做错了什么?Alain

答案3

仅供参考!(我认为这几乎与 LaTeX/TikZ 中的坐标数组和字符串数组有关)

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
% array of numbers a[i], i=0,1,2,3
\def\a{{6,8,11,12}}
% array of strings b[i], i=0,1,2,3
\def\b{{"blue","red","orange","teal"}}
% array of points (pairs of coordinates) c[i], i=0,1,2,3
\def\c{{"(1,2)","(1.5,1.5)","(3,2.5)","(5,3)"}}
\begin{tikzpicture}
\draw (0,.5)rectangle (6.5,3.5);
\foreach \i in {0,...,3}{
\pgfmathsetmacro{\ii}{int(\i+1)}
\pgfmathsetmacro{\tmpa}{\a[\i]}
\pgfmathsetmacro{\tmpb}{\b[\i]}
\pgfmathsetmacro{\tmpc}{\c[\i]}
\path (0,-\i) node[right,\tmpb,align=left]
{The \ii-th element is $\tmpa{}$ with \tmpb{} color\\
associated with the point $\tmpc$};
\fill[\tmpb] \tmpc node[above]{\tmpa} circle(2pt);
}
\end{tikzpicture}
\end{document}

相关内容