2

I would like to add an icon on the border of a node. I can do it easily for one node:

\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}

But for convinience, I would like to make a style doing the job. Something like

\documentclass{article}
\usepackage{tikz}
\tikzset{symbol/.style={...}}
\begin{document}

\begin{tikzpicture}
        \node[draw,symbol] (A) {content};
\end{tikzpicture}
  • The problem is that the \fill isn't part of the \node, but rather both are separate \paths. You could make a shape which includes the icon, but that uses \pgfdeclareshape (page 1126). – John Kormylo Sep 18 at 12:54
  • I guess the \pgfdeclareshape is a good choice, it seems convenient to use. Sadly my knowlege in pgf, is not sufficient now. But from the answers below, it seems to be the best choice. – raphchar Sep 20 at 8:41
2

I suggest using pic with [pic actions].

enter image description here

\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}
1

It is possible to do such things with the append after command key (see the pgfmanual v3.1.4 on p. 151) but since you want to fill the extra thingy but not the node, you also need a path picture, so it is a bit complicated given the outcome. This defines a style diamond node that can be used like

\draw[fill] node[diamond node] (A) {content};

Full example:

\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}

enter image description here

  • The seemingly simpler option of using \pgfextra inside the append after command is a trap... – Schrödinger's cat Sep 18 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.