我想给我的节点添加样式,在节点内部绘制额外的路径。
例如(没有正确放置节点)
\node[draw,circle,lVert] {};
\node[draw,circle,lHor] {};
\node[draw,circle,lVert,lHor] {};
将产生以下节点:
关键词
删除线节点、节点内线、删除线样式、十字、路径作为样式
答案1
调整以下问题的解决方案(感谢 Claudio 的评论):创建无缝 XOR 符号作为节点
我最终得到了这个可行且极其简单的代码:
\begin{tikzpicture}[
every node/.style={draw,circle},
hVert/.style={append after command={(\tikzlastnode.north) edge (\tikzlastnode.south)}},
hHorz/.style={append after command={(\tikzlastnode.west) edge (\tikzlastnode.east)}}
]
\node[hVert] (a) {};
\node[right=of a,hVert,hHorz] (f) {};
\node[right=of f,hHorz] (b) {};
\end{tikzpicture}
答案2
另一种选择:声明三种新形状。
最好只声明一个新形状并使用一些参数来绘制水平线、垂直线或两条线,但我不知道该怎么做。
\documentclass[tikz, border=2mm]{standalone}
\makeatletter
\pgfdeclareshape{hVert}
{
\inheritsavedanchors[from=circle] % this is nearly a circle
\inheritanchorborder[from=circle]
\inheritanchor[from=circle]{north}
\inheritanchor[from=circle]{north west}
\inheritanchor[from=circle]{north east}
\inheritanchor[from=circle]{center}
\inheritanchor[from=circle]{west}
\inheritanchor[from=circle]{east}
\inheritanchor[from=circle]{mid}
\inheritanchor[from=circle]{mid west}
\inheritanchor[from=circle]{mid east}
\inheritanchor[from=circle]{base}
\inheritanchor[from=circle]{base west}
\inheritanchor[from=circle]{base east}
\inheritanchor[from=circle]{south}
\inheritanchor[from=circle]{south west}
\inheritanchor[from=circle]{south east}
\inheritbackgroundpath[from=circle]
\foregroundpath{
\centerpoint%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
\pgfutil@tempdima=\radius%
\advance\pgfutil@tempdima by -.5\pgflinewidth%
\pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0pt}{\pgfutil@tempdima}}}
\pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0pt}{-\pgfutil@tempdima}}}
\pgfsetarrowsstart{}
\pgfsetarrowsend{}
}
}
\pgfdeclareshape{hHorz}
{
\inheritsavedanchors[from=circle] % this is nearly a circle
\inheritanchorborder[from=circle]
\inheritanchor[from=circle]{north}
\inheritanchor[from=circle]{north west}
\inheritanchor[from=circle]{north east}
\inheritanchor[from=circle]{center}
\inheritanchor[from=circle]{west}
\inheritanchor[from=circle]{east}
\inheritanchor[from=circle]{mid}
\inheritanchor[from=circle]{mid west}
\inheritanchor[from=circle]{mid east}
\inheritanchor[from=circle]{base}
\inheritanchor[from=circle]{base west}
\inheritanchor[from=circle]{base east}
\inheritanchor[from=circle]{south}
\inheritanchor[from=circle]{south west}
\inheritanchor[from=circle]{south east}
\inheritbackgroundpath[from=circle]
\foregroundpath{
\centerpoint%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
\pgfutil@tempdima=\radius%
\advance\pgfutil@tempdima by -.5\pgflinewidth%
\pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{\pgfutil@tempdima}{0pt}}}
\pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-\pgfutil@tempdima}{0pt}}}
\pgfsetarrowsstart{}
\pgfsetarrowsend{}
}
}
\pgfdeclareshape{hVertHorz}
{
\inheritsavedanchors[from=circle] % this is nearly a circle
\inheritanchorborder[from=circle]
\inheritanchor[from=circle]{north}
\inheritanchor[from=circle]{north west}
\inheritanchor[from=circle]{north east}
\inheritanchor[from=circle]{center}
\inheritanchor[from=circle]{west}
\inheritanchor[from=circle]{east}
\inheritanchor[from=circle]{mid}
\inheritanchor[from=circle]{mid west}
\inheritanchor[from=circle]{mid east}
\inheritanchor[from=circle]{base}
\inheritanchor[from=circle]{base west}
\inheritanchor[from=circle]{base east}
\inheritanchor[from=circle]{south}
\inheritanchor[from=circle]{south west}
\inheritanchor[from=circle]{south east}
\inheritbackgroundpath[from=circle]
\foregroundpath{
\centerpoint%
\pgf@xc=\pgf@x%
\pgf@yc=\pgf@y%
\pgfutil@tempdima=\radius%
\advance\pgfutil@tempdima by -.5\pgflinewidth%
\pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0pt}{\pgfutil@tempdima}}}
\pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0pt}{-\pgfutil@tempdima}}}
\pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{\pgfutil@tempdima}{0pt}}}
\pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-\pgfutil@tempdima}{0pt}}}
\pgfsetarrowsstart{}
\pgfsetarrowsend{}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[hHorz,draw, thick, blue, fill=red!30] (A) at (0,0) {};
\node[hVert,draw, red, fill=blue!30, minimum size=.75cm] (B) at (1,1) {};
\node[hVertHorz,draw, thick,fill=red!30!blue] (C) at (2,0) {};
\draw (A)--(B)--(C);
\end{tikzpicture}
\end{document}