如何使用节点样式在 TikZ 节点内部绘图?

如何使用节点样式在 TikZ 节点内部绘图?

我想定义一个节点样式,使节点看起来像 LaTeX 符号\oplus,即一个从北到南、从东到西有线条的圆圈。(我心中还有更复杂的例子。)

execute at end node我认为我应该在、 或参数中画线after mode path,但我尝试过的方法似乎都不起作用。我想到的代码是这样的:

\tikzstyle{foo}=[circle,draw,execute at end node={\draw ??????}]

重新表述这个问题:如何在节点内进行任意绘制,并将其定义为样式?具体来说,如何获取样式中当前节点的坐标?

答案1

所有 TikZ 节点形状均使用低级 PGF 代码定义。这在pgfmanual第 75.5 节中进行了描述声明新形状,在v2.1手册第625页。

您可以使用圆形的现有代码作为基础,并向其中添加线条。代码可以在文件中找到${TEXMF}/tex/generic/pgf/modules/pgfmoduleshapes.code.tex

以下是我的做法:

形状声明:

\pgfdeclareshape{oplus}
%
% Shaped like '\oplus' math symbol. Based on 'circle' shape
%
{%
  % All anchors are taken from the 'circle' shape:
  \inheritsavedanchors[from={circle}]%
  \inheritanchor[from={circle}]{center}%
  \inheritanchor[from={circle}]{mid}%
  \inheritanchor[from={circle}]{base}%
  \inheritanchor[from={circle}]{north}%
  \inheritanchor[from={circle}]{south}%
  \inheritanchor[from={circle}]{west}%
  \inheritanchor[from={circle}]{east}%
  \inheritanchor[from={circle}]{mid west}%
  \inheritanchor[from={circle}]{mid east}%
  \inheritanchor[from={circle}]{base west}%
  \inheritanchor[from={circle}]{base east}%
  \inheritanchor[from={circle}]{north west}%
  \inheritanchor[from={circle}]{south west}%
  \inheritanchor[from={circle}]{north east}%
  \inheritanchor[from={circle}]{south east}%
  \inheritanchorborder[from={circle}]%
  %
  % Only the background path is different
  %
  \backgroundpath{%
    % First the existing 'circle' code:
    \pgfutil@tempdima=\radius%
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%
    \ifdim\pgf@xb<\pgf@yb%
      \advance\pgfutil@tempdima by-\pgf@yb%
    \else%
      \advance\pgfutil@tempdima by-\pgf@xb%
    \fi%
    \pgfpathcircle{\centerpoint}{\pgfutil@tempdima}%
    %
    % Now the | and -- lines:
    \pgfmoveto{\pgfpointadd{\centerpoint}{\pgfpoint{0pt}{\pgfutil@tempdima}}}%
    \pgflineto{\pgfpointadd{\centerpoint}{\pgfpoint{0pt}{-\pgfutil@tempdima}}}%
    \pgfmoveto{\pgfpointadd{\centerpoint}{\pgfpoint{\pgfutil@tempdima}{0pt}}}%
    \pgflineto{\pgfpointadd{\centerpoint}{\pgfpoint{-\pgfutil@tempdima}{0pt}}}%
  }%
}

使用示例:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfshape_oplus}

\begin{document}
\begin{tikzpicture}
  \node [draw=blue,shape=oplus] {Test};
\end{tikzpicture}
\end{document}

结果:

结果

您可能还想在中绘制其他线条\foregroundpath。为此,请参阅forbidden sign文件中的形状pgflibraryshapes.symbols.code.tex就是一个很好的例子。

答案2

一个潜在可能 :

 \begin{tikzpicture}
   \tikzset{oplus/.style={path picture={% 
      \draw[black]
       (path picture bounding box.south) -- (path picture bounding box.north) 
       (path picture bounding box.west) -- (path picture bounding box.east);
      }}} 
    \node[oplus,fill=blue!10,draw=blue,thick,circle] {};
    \end{tikzpicture}

奥普洛斯

可以在背景中发送十字架并改变颜色

\begin{tikzpicture}
 \tikzset{oplus/.style={path picture={%
 \begin{pgfonlayer}{background} 
 \draw[red,opacity=.5]
  (path picture bounding box.south) -- (path picture bounding box.north) 
  (path picture bounding box.west) -- (path picture bounding box.east);
\end{pgfonlayer}}}
}
\node[oplus,draw=blue,thick,circle](a) {TEST}; 
\end{tikzpicture}

oplus 2

现在你的代码execute at end node在这种情况下是不好的。此代码修改了节点的内容。你可以尝试下一个代码:

 \begin{tikzpicture}
    \tikzset{foo/.style ={circle,draw, execute at end node={\ #1}}} 
    \node[foo=bad](A) at (2,3){good};
 \end{tikzpicture}

有趣的execute at begin node=$execute at end node=$

现在,如果我的答案不是您想要的,您可以像shape =oplusMartin 的另一个答案中那样定义新的形状。

相关内容