有没有办法忘记 TikZ 图片之间的节点名称?

有没有办法忘记 TikZ 图片之间的节点名称?

我有一个包含多张 TikZ 图片的文档。我通常对节点使用简单的名称,例如:

\node (A) at ..... 
\node (B) at .....

等等。因此,我在不同的图片之间发生了名称冲突。这通常不是问题。但是,我发现自己可能在给定的图片中定义了某个节点,也可能没有定义。因此,我使用以下代码片段来测试该节点是否已定义:

\makeatletter
\long\def\ifnodedefined#1#2#3{%%
  \@ifundefined{pgf@sh@ns@#1}{#3}{#2}}
\makeatother

不幸的是,如果我已经在另一张图片中创建了同名的节点,则此操作会失败。

有没有办法确定节点定义的范围,或者有没有办法在不再需要节点时取消定义节点?

附言

我知道我可以将每张图片嵌入到standalone文件中并使用 导入它\includegraphics。不幸的是,这种方法不适用于我正在创建的文档。

平均能量损失

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\long\def\ifnodedefined#1#2#3{%%
  \@ifundefined{pgf@sh@ns@#1}{#3}{#2}}
\makeatother

\setlength{\parindent}{0pt}
\pagestyle{empty}
\begin{document}


There's no node \textbf{D} here, so I draw a triangle:\par
\begin{tikzpicture}

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (1,1);
%  \coordinate (D) at (0,1);

  \ifnodedefined{D}
    {  \draw (A) foreach \myn in {B,C,D} {  -- (\myn) } --cycle;  }
    {  \draw (A) foreach \myn in {B,C}   {  -- (\myn) } --cycle;  }

\end{tikzpicture}




There is a node \textbf{D} here, so I draw a square:\par
\begin{tikzpicture}

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (1,1);
  \coordinate (D) at (0,1);

  \ifnodedefined{D}
    {  \draw (A) foreach \myn in {B,C,D} {  -- (\myn) } --cycle;  }
    {  \draw (A) foreach \myn in {B,C}   {  -- (\myn) } --cycle;  }

\end{tikzpicture}





There isn't supposed to be a node \textbf{D} here.  I'm expecting a
triangle, but get a square.\par
\begin{tikzpicture}

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (1,1);
%  \coordinate (D) at (0,1);

  \ifnodedefined{D}
    {  \draw (A) foreach \myn in {B,C,D} {  -- (\myn) } --cycle;  }
    {  \draw (A) foreach \myn in {B,C}   {  -- (\myn) } --cycle;  }

\end{tikzpicture}

\end{document}

生成:

在此处输入图片描述

答案1

我似乎通过提问的方式实际上回答了我的问题:

\makeatletter

\newcommand\aeundefinenode[1]{%%
  \expandafter\ifx\csname pgf@sh@ns@#1\endcsname\relax
  \else
    \typeout{===>Undefining node "#1"}%%
    \global\expandafter\let\csname pgf@sh@ns@#1\endcsname\relax
  \fi
}

这是工作示例:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter

\long\def\ifnodedefined#1#2#3{%%
  \@ifundefined{pgf@sh@ns@#1}{#3}{#2}}

\newcommand\aeundefinenode[1]{%%
  \expandafter\ifx\csname pgf@sh@ns@#1\endcsname\relax
  \else
    \typeout{===>Undefining node "#1"}%%
    \global\expandafter\let\csname pgf@sh@ns@#1\endcsname\relax
  \fi
}

\newcommand\aeundefinethesenodes[1]{%%
  \foreach \myn  in {#1}
    {%%
      \expandafter\aeundefinenode\expandafter{\myn}%%
    }%%
}

\makeatother

\setlength{\parindent}{0pt}
\pagestyle{empty}
\begin{document}

%-@-(1)---------------------------------------------------------------------
There's no node \textbf{D} here, so I draw a triangle:\par
\begin{tikzpicture}

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (1,1);
%  \coordinate (D) at (0,1);

  \ifnodedefined{D}
    {  \draw (A) foreach \myn in {B,C,D} {  -- (\myn) } --cycle;  }
    {  \draw (A) foreach \myn in {B,C}   {  -- (\myn) } --cycle;  }

\end{tikzpicture}

%-@-(2)---------------------------------------------------------------------
There is a node \textbf{D} here, so I draw a square:\par
\begin{tikzpicture}

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (1,1);
  \coordinate (D) at (0,1);

  \ifnodedefined{D}
    {  \draw (A) foreach \myn in {B,C,D} {  -- (\myn) } --cycle;  }
    {  \draw (A) foreach \myn in {B,C}   {  -- (\myn) } --cycle;  }

  \aeundefinethesenodes{A,B,C,D}
\end{tikzpicture}

%-@-(3)---------------------------------------------------------------------
There isn't supposed to be a node \textbf{D} here.  I'm expecting a
triangle, and get a triangle.\par
\begin{tikzpicture}

  \coordinate (A) at (0,0);
  \coordinate (B) at (1,0);
  \coordinate (C) at (1,1);
%  \coordinate (D) at (0,1);

  \ifnodedefined{D}
    {  \draw (A) foreach \myn in {B,C,D} {  -- (\myn) } --cycle;  }
    {  \draw (A) foreach \myn in {B,C}   {  -- (\myn) } --cycle;  }

\end{tikzpicture}

\end{document}

\newcommand\aeundefinethesenodes[1]{%%
  \foreach \myn  in {#1}
    {%%
      \expandafter\aeundefinenode\expandafter{\myn}%%
    }%%
}

\makeatother

在此处输入图片描述

相关内容