获取已赋予 TikZ 键的值

获取已赋予 TikZ 键的值

我正在尝试编写一个基于 TikZ 的包,这是我目前遇到的问题。

让我们假设,我有一个密钥,/tikz/circuit symbol unit它可以执行一些代码。

我想知道是否可以获取传递给此键的值以便在代码中的其他地方使用它?

我想使用传递给/tikz/circuit symbol unit键的值在我制作的形状的前景中绘制适当半径的圆弧。

因此,\pgfpatharc{50}{130}{6pt}我会得到类似这样的结果\pgfpatharc{50}{130}{[some function to get the value passed to circuit symbol unit key]}

这是形状的代码 - 您可以跳到代码的最末尾的“绘制弧”部分:

   % The measurement gate shape - rectangle with an arc inside
   \pgfdeclareshape{meter}
   {%

     % All anchors are taken from the 'rectangle' shape:
     \inheritsavedanchors[from={rectangle}]%
     \inheritanchor[from={rectangle}]{center}%
     \inheritanchor[from={rectangle}]{mid}%
     \inheritanchor[from={rectangle}]{base}%
     \inheritanchor[from={rectangle}]{north}%
     \inheritanchor[from={rectangle}]{south}%
     \inheritanchor[from={rectangle}]{west}%
     \inheritanchor[from={rectangle}]{east}%
     \inheritanchor[from={rectangle}]{mid west}%
     \inheritanchor[from={rectangle}]{mid east}%
     \inheritanchor[from={rectangle}]{base west}%
     \inheritanchor[from={rectangle}]{base east}%
     \inheritanchor[from={rectangle}]{north west}%
     \inheritanchor[from={rectangle}]{south west}%
     \inheritanchor[from={rectangle}]{north east}%
     \inheritanchor[from={rectangle}]{south east}%
     \inheritanchorborder[from={rectangle}]%

     % Drawing a white background
     \backgroundpath{
     \pgfpathrectanglecorners{\southwest}{\northeast}
     \pgfsetfillopacity{1}
         \pgfsetfillcolor{white}
         \pgfusepath{fill}
     }       

     \foregroundpath
     {
       % Drawing the rectangle

    \pgfpathrectanglecorners
     {\pgfpointadd{\southwest}
     {\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}
     {\pgfkeysvalueof{/pgf/outer ysep}}}}
     {\pgfpointadd{\northeast}{\pgfpointscale{-1}
     {\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}
     {\pgfkeysvalueof{/pgf/outer ysep}}}}}





    \northeast
    \pgf@xa=\pgf@x
    \pgf@ya=-\pgf@y

   %
   %    
   % Drawing the arc

   \def\arcstart{\pgfpoint{.8\pgf@xa}{.7\pgf@ya}}
   \pgfpathmoveto{\arcstart}    

   %
   %
   % This is the line i'm interested in - how do i put in the value passed on to
   % /tikz/circuit symbol unit key instead of 6pt. It should be something like
   % \pgfpatharc{50}{130}{some function to get the value passed to circuit symbol unit}
   \pgfpatharc{50}{130}{6pt}    

    }

答案1

您的代码已经使用检索了键的值,\pgfkeysvalueof{}所以也许我对您的问题的理解过于简单。

但是,您当然可以使用该方法:

    \pgfpatharc{50}{130}{\pgfkeysvalueof{/tikz/circuit/symbol/unit}}%

/tikz/circuit/symbol/unit由于您没有提供完整的示例,而且我不知道您实际上在做什么,所以我创建的虚拟密钥在哪里。

\tikzset{%
  circuit/symbol/unit/.initial=10pt,
}

然后,例如,

\begin{tikzpicture}
  \node [meter, draw] {};
  \node [xshift=15pt, circuit/symbol/unit=4pt, meter, draw] {};
\end{tikzpicture}

生产

可变维度的东西

完整代码:

\documentclass[border=10pt,multi,tikz]{standalone}
\makeatletter
\pgfdeclareshape{meter}
{%
  % All anchors are taken from the 'rectangle' shape:
  \inheritsavedanchors[from={rectangle}]%
  \inheritanchor[from={rectangle}]{center}%
  \inheritanchor[from={rectangle}]{mid}%
  \inheritanchor[from={rectangle}]{base}%
  \inheritanchor[from={rectangle}]{north}%
  \inheritanchor[from={rectangle}]{south}%
  \inheritanchor[from={rectangle}]{west}%
  \inheritanchor[from={rectangle}]{east}%
  \inheritanchor[from={rectangle}]{mid west}%
  \inheritanchor[from={rectangle}]{mid east}%
  \inheritanchor[from={rectangle}]{base west}%
  \inheritanchor[from={rectangle}]{base east}%
  \inheritanchor[from={rectangle}]{north west}%
  \inheritanchor[from={rectangle}]{south west}%
  \inheritanchor[from={rectangle}]{north east}%
  \inheritanchor[from={rectangle}]{south east}%
  \inheritanchorborder[from={rectangle}]%
  \backgroundpath{%
    \pgfpathrectanglecorners{\southwest}{\northeast}%
    \pgfsetfillopacity{1}%
    \pgfsetfillcolor{white}%
    \pgfusepath{fill}%
  }%
  \foregroundpath
  {%
    \pgfpathrectanglecorners
    {\pgfpointadd{\southwest}%
      {\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}%
        {\pgfkeysvalueof{/pgf/outer ysep}}}}%
    {\pgfpointadd{\northeast}{\pgfpointscale{-1}%
        {\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}%
          {\pgfkeysvalueof{/pgf/outer ysep}}}}}%
    \northeast
    \pgf@xa=\pgf@x
    \pgf@ya=-\pgf@y
    \def\arcstart{\pgfpoint{.8\pgf@xa}{.7\pgf@ya}}%
    \pgfpathmoveto{\arcstart}%
    \pgfpatharc{50}{130}{\pgfkeysvalueof{/tikz/circuit/symbol/unit}}%
  }%
}
\makeatother
\tikzset{%
  circuit/symbol/unit/.initial=10pt,
}
\begin{document}
\begin{tikzpicture}
  \node [meter, draw] {};
  \node [xshift=15pt, circuit/symbol/unit=4pt, meter, draw] {};
\end{tikzpicture}
\end{document}

相关内容