![如何使用节点样式在 TikZ 节点内部绘图?](https://linux22.com/image/234787/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E8%8A%82%E7%82%B9%E6%A0%B7%E5%BC%8F%E5%9C%A8%20TikZ%20%E8%8A%82%E7%82%B9%E5%86%85%E9%83%A8%E7%BB%98%E5%9B%BE%EF%BC%9F.png)
我想定义一个节点样式,使节点看起来像 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}
现在你的代码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 =oplus
Martin 的另一个答案中那样定义新的形状。