\pgfdeclareshape 中圆形路径与圆形节点的尺寸?

\pgfdeclareshape 中圆形路径与圆形节点的尺寸?

我正在尝试将矩形节点“重载”为自定义形状,使其西南角有一个圆圈。在下面的 MWE 中,我用来\circsize设置圆圈的大小 - 出于测试目的,我既绘制了一条圆形路径,又绘制了一个圆形节点。但是,我得到的输出是这样的:

测试22.png

...也就是说 - 即使我试图\circsize同时应用于圆形路径和圆形节点 - 它们也会以不同的大小输出!(请注意,不幸的是我甚至无法进行\typeout{\circsize}调试)。

为什么会发生这种情况 - 以及如何使节点和路径具有与指定相同的大小\circsize

编辑:澄清问题:当我使用圆路径图元\pgfpathcircle{\southwest}{\circsize}与圆节点图元\pgfnode{circle}{center}{}{\tikz@fig@name-A}{\pgfusepath{draw}}(使用来\pgfkeys{/pgf/minimum size=\circsize}指定大小)时,为什么会得到不同大小的圆?如何通过使用\circsize作为两者的参数来使这些圆具有相同的大小?

妇女权利委员会:

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

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\pgfkeys{/tikz/mycircsize/.initial = 0.3cm}
\makeatletter
\pgfdeclareshape{testshape}{
  \inheritsavedanchors[from={rectangle}]
  \inheritbackgroundpath[from={rectangle}]
  \inheritanchorborder[from={rectangle}]
  \foreach \x in {center,north east,north west,north,south,south east,south west,east,west}{
    \inheritanchor[from={rectangle}]{\x}
  }
  \saveddimen\circsize{\pgf@x=\pgfkeysvalueof{/tikz/mycircsize}}
  %\typeout{\the\circsize} % ! Undefined control sequence.
  \foregroundpath{
    \southwest
    % just draw a circle path (and only it, a bit thicker)
    \begin{pgfscope}
      {
      \pgfsetlinewidth{2pt} % no effect here ...
      \pgfpathcircle{\southwest}{\circsize}
      \pgfusepath{stroke}   % ... unless 'usepath is used!
      }
    \end{pgfscope}
    % draw a circular node
    {
    \color{red}
    \pgfkeys{/pgf/minimum size=\circsize}
    \pgfset{minimum size=\circsize}
    \pgfkeys{/tikz/radius=0.5\circsize}
    %\pgfset{inner sep=0pt}
    \pgftransformshift{\southwest}
    \pgfnode{circle}{center}{}{\tikz@fig@name-A}{\pgfusepath{draw}}
    %\pgfusepath{stroke} % no need for this?
    }
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[testshape,draw] (n1) at (1,0) {Testing};
\end{tikzpicture}
\end{document}

答案1

一个小小的更正(如果我正确理解了这个问题?)。

  %### Correction here ###%
  \saveddimen\circsize{\pgfkeysvalueof{/tikz/mycircsize}}

编辑

你甚至可以尝试:

 \pgfkeys{/tikz/.cd,
 mycirsize/.store in=\circsize,
 mycirsize= 0.3cm
 }

在此处输入图片描述

答案2

从头开始:(这是您正在寻找的吗?)

在此处输入图片描述

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}

\pgfkeys{/tikz/.cd,
 mycircsize/.store in=\circsize,
 mycircsize= 0.8cm
 }
\tikzset {
    Mycirc/.style 2 args={%
        append after command={
            \pgfextra{
            \pgfinterruptpath

    \path[#2] ($(\tikzlastnode.south west)+(0,0.5*\circsize)$)
        arc (90:360:0.5*\circsize)
        -- (\tikzlastnode.south east)
        -- (\tikzlastnode.north east)
        -- (\tikzlastnode.north west)
        -- cycle ;

    \path (\tikzlastnode.south west)
        node[inner sep=2pt,anchor=center,shape=circle,%
        minimum size=\circsize]{#1};

            \endpgfinterruptpath
            }
        }
    }
}


\begin{document}
\begin{tikzpicture}

\node[Mycirc={a}] at (0,0) {testing} ;

\node[Mycirc={a}{draw}] at (0,-1) {testing} ;

\node[Mycirc={a}{draw,fill=blue!20}] at (0,-2) {testing} ;

\tikzset{mycircsize=.3cm}
\node[Mycirc={a}{draw,fill=blue!20}] at (0,-3) {testing} ;

\end{tikzpicture}
\end{document}

答案3

好的,非常感谢@Tarass 的回答,我终于明白了 - 事情是这样的:

  • \pgfpathcircle{\southwest}{\circsize}\circsize需要是半径
  • \pgfkeys{/pgf/minimum size=\circsize}(对于\pgfnode{circle}{center}{}{\tikz@fig@name-A}{\pgfusepath{draw}})中,\circsize需要是直径

所以答案是:应该使用\pgfpathcircle{\southwest}{0.5*\circsize}- 最后绘制两个相同大小的圆形:

测试25.png

这是 MWE:

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

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

%\pgfkeys{/tikz/mycircsize/.initial = 0.3cm}
\pgfkeys{/tikz/.cd,
mycircsize/.store in=\circsize,
mycircsize=0.3cm
}
\makeatletter
\pgfdeclareshape{testshape}{
  \inheritsavedanchors[from={rectangle}]
  \inheritbackgroundpath[from={rectangle}]
  \inheritanchorborder[from={rectangle}]
  \foreach \x in {center,north east,north west,north,south,south east,south west,east,west}{
    \inheritanchor[from={rectangle}]{\x}
  }
  %\saveddimen\circsize{\pgfkeysvalueof{/tikz/mycircsize}}
  %\typeout{\the\circsize} % ! Undefined control sequence.
  \foregroundpath{
    \southwest
    % just draw a circle path (a bit thicker)
    \begin{pgfscope}
      {
      \pgfsetlinewidth{2pt} % no effect here ...
      \pgfpathcircle{\southwest}{0.5*\circsize}
      \pgfusepath{stroke}   % unless usepath is used!
      }
    \end{pgfscope}
    % draw a circular node
    {
    \color{red}
    %\pgftransformresetnontranslations % not really needed
    \pgfkeys{/pgf/minimum size=\circsize}
    \pgfset{minimum size=\circsize}
    %\pgfkeys{/tikz/radius=10\circsize} % has no effect here!
    \pgftransformshift{\southwest}
    \pgfset{inner sep=0pt,outer sep=0pt}
    \pgfnode{circle}{center}{}{\tikz@fig@name-A}{\pgfusepath{draw}}
    %\pgfusepath{stroke} % no need for this?
    }
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[testshape,draw] (n1) at (1,0) {Testing};
\end{tikzpicture}
\end{document}

相关内容