我想在节点的边框上添加一个图标。我可以轻松地为一个节点完成此操作:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw] (A) {content};
\fill(A.west) ++(0.1,0)--++(-0.1,-0.1)--++(-0.1,0.1)--++(0.1,0.1)--++(0.1,-0.1)--cycle;
\end{tikzpicture}
但为了方便起见,我想设计一种可以完成这个工作的样式。比如
\documentclass{article}
\usepackage{tikz}
\tikzset{symbol/.style={...}}
\begin{document}
\begin{tikzpicture}
\node[draw,symbol] (A) {content};
\end{tikzpicture}
答案1
我建议pic
使用[pic actions]
。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[diamond/.pic={
\path[pic actions] (.1,0)--(0,.1)--(-.1,0)--(0,-.1)--cycle;
}]
\path
(0,0) node[draw] (A) {content A}
(A.west) pic[fill=red]{diamond};
\path
(4,0) node[draw] (B) {content B}
(B.east) pic[fill=blue!50,draw=magenta]{diamond};
\path
(2,-1) node[draw] (C) {content C}
(C.north west) pic[fill=white,draw]{diamond};
\end{tikzpicture}
\end{document}
答案2
可以使用append after command
键来执行此类操作(请参阅第 151 页的 pgfmanual v3.1.4),但由于您想填充额外的内容而不是节点,因此您还需要一个路径图片,因此考虑到结果,这有点复杂。这定义了一种diamond node
可以像
\draw[fill] node[diamond node] (A) {content};
完整示例:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[diamond node/.style={draw,fill,
path picture={
\fill[white] (path picture bounding box.south west)
rectangle (path picture bounding box.north east);
\fill (path picture bounding box.west) ++(0.1,0)--++(-0.1,-0.1)--++(-0.1,0.1)--++(0.1,0.1)--++(0.1,-0.1)--cycle;},
append after command={
(\tikzlastnode.west)
++(0.1,0)--++(-0.1,-0.1)--++(-0.1,0.1)--++(0.1,0.1)--++(0.1,-0.1)--cycle}}]
\draw[fill] node[diamond node] (A) {content};
\end{tikzpicture}
\end{document}