# importing an external image on top of a node using tikz

How can I generate this nodes and edge structure using tikz in latex?

I created the following figure using tikz, now I'd like to put some figures on the top of each node (e.g. a heart with heart-beat in one and a cheese figure on the other node)?

The reason I want to create the heart figure using tikz is that the quality of my external image reduces because I cropped heart-rate from one image put it on the top of heart figure and resizing it constantly decreases its quality.

I also would like to put one of the characters from this package \usepackage{tikzlings} as a figure on top of the other node and it doesn't work (for instance \thing[cheese]) or even not use this package but either another package or just create a cheese figure inside the node).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,cal,positioning}
\usepackage{tikzlings}

\begin{document}
\begin{tikzpicture}
%nodes for latent state
\node[scale=0.07,circle, fill=red!40!brown!65] (P1) at (0,7) {\includegraphics[]{}};
\node[scale=0.07,circle, fill=red!40!brown!65] (P2) at (2.5,7) {\includegraphics[]{}};
\node[align=center] at (0,6.2){\tiny\textbf{\textsc{Low}}};
\node[align=center] at (2.5,6.2){\tiny\textbf{\textsc{High}}};
\path[every node/.style={font=\sffamily\small}]
(P2) edge [bend right] coordinate [pos=0.2] (top) (P1)
(P1) edge [bend right] (P2)
;
\path (P1) edge [loop left] node {} (P1);
\path (P2) edge [loop right] node {} (P2);
%%%%%%%%%%
%%% RECTANGLES %%%
\node[draw, thick, dotted, rounded corners, inner xsep=3.5em, inner ysep=1em, fit=(P1) (P2)] (box) {};
\node[fill=white] at (box.south) {\tiny\textbf{\textsc{States}}};
\end{tikzpicture}
\end{document}


• Hello ! I am afraid I do not fully understand your question. The first image can be obtained using some bend left,bend right and loop left,loop right. But I can't figure the link with the heart. Can you provide a MWE so we have something to start from ? Nov 4 '20 at 18:40
• @BambOo I managed to make my total graph structure with nodes and links but have problems to put images on the nodes. Nov 4 '20 at 18:48
• Sorry but your example is not complete. It lacks \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. Nov 4 '20 at 18:52
• @BambOo I updated my question and its code. Nov 4 '20 at 19:00
• the code needs to load some libraries!!! fit command!! Nov 4 '20 at 19:05

At the moment, based on your code, I obtain the following picture:

Based on your intent to use an image clipped by a path, I propose you an alternative based on the path picture syntax presented in section §15.6 of the pgfmanual. This alternative gives the figure at the bottom of this answer.

\documentclass[tikz]{standalone}
\usetikzlibrary{fit}
\usepackage{tikzlings}
\tikzset{
img/.style={circle, fill=red!40!brown!65,inner sep=0pt},
}
\begin{document}
\begin{tikzpicture}
%nodes for latent state
% Current code
%\node[img] (P1) at (0,7) {\includegraphics[width=1cm]{example-image-a}};
%\node[img] (P2) at (2.5,7) {\includegraphics[width=1cm]{example-image-b}};
% Proposal
\path[img] (0,7) circle (5mm) [path picture={\node (P1) at (path picture bounding box.center) {\includegraphics[width=1cm]{example-image-a}};}] ;
\path[img] (2.5,7) circle (5mm) [path picture={\node (P2) at (path picture bounding box.center) {\includegraphics[width=1cm]{example-image-b}};}] ;
\node[align=center] at (0,6.2){\tiny\textbf{\textsc{Low}}};
\node[align=center] at (2.5,6.2){\tiny\textbf{\textsc{High}}};
\path[every node/.style={font=\sffamily\small}]
(P2) edge [bend right] coordinate [pos=0.2] (top) (P1)
(P1) edge [bend right] (P2)
;
\path (P1) edge [loop left] node {} (P1);
\path (P2) edge [loop right] node {} (P2);
%%%%%%%%%%
%%% RECTANGLES %%%
\node[draw, thick, dotted, rounded corners, inner xsep=3.5em, inner ysep=1em, fit=(P1) (P2)] (box) {};
\node[fill=white] at (box.south) {\tiny\textbf{\textsc{States}}};
\end{tikzpicture}
\end{document}



EDIT

Here is working version including an improved definition of the "Low" and "High" node position with the positioning library and the inclusion of the \tikzlings in the nodes.

\documentclass[tikz]{standalone}
\usetikzlibrary{fit,positioning}
\usepackage{tikzlings}
\tikzset{
img/.style={circle, fill=red!40!brown!65,inner sep=0pt,outer sep=0pt},
}
\begin{document}
\begin{tikzpicture}
%nodes for latent state
\path[img] (0,7) circle (5mm) [path picture={\node (P1) at (path picture bounding box.center) {\tikz[scale=0.5]{\marmot}};}] ;
\path[img] (2.5,7) circle (5mm) [path picture={\node (P2) at (path picture bounding box.center) {\tikz[scale=0.5]{\cat}};}] ;
\node[below=5mm of P1.center] (P1l) {\tiny\textbf{\textsc{Low}}};
\node[below=5mm of P2.center] (P2l) {\tiny\textbf{\textsc{High}}};
\path[every node/.style={font=\sffamily\small}]
(P2) edge [bend right] coordinate [pos=0.2] (top) (P1)
(P1) edge [bend right] (P2)
;
\path (P1) edge [loop left] node {} (P1);
\path (P2) edge [loop right] node {} (P2);
%%%%%%%%%%
%%% RECTANGLES %%%
\node[draw, thick, dotted, rounded corners, inner xsep=3.5em, inner ysep=1em, fit=(P1) (P2)] (box) {};
\node[fill=white] at (box.south) {\tiny\textbf{\textsc{States}}};
\end{tikzpicture}
\end{document}


EDIT n°2 : inclusion of heartbeat shape and \resizebox

\documentclass[tikz]{standalone}
\usetikzlibrary{fit,positioning}
\usepackage{tikzlings}
\tikzset{
img/.style={circle, fill=red!40!brown!65,inner sep=0pt,outer sep=0pt},
}
\usepackage{fontawesome}
\begin{document}
\begin{tikzpicture}
%nodes for latent state
\path[img] (0,7) circle (5mm) [path picture={\node[text width=1cm,align=center,anchor=center,inner sep=0pt, outer sep=0pt] (P1) at (path picture bounding box.center) {\resizebox{1cm}{!}{\faHeartbeat}};}] ;
\path[img] (2.5,7) circle (5mm) [path picture={\node[text width=1cm,align=center,anchor=center,inner sep=0pt, outer sep=0pt] (P2) at (path picture bounding box.center) {\resizebox{1cm}{!}{\tikz{\cat}}};}] ;
\node[below=5mm of P1.center] (P1l) {\tiny\textbf{\textsc{Low}}};
\node[below=5mm of P2.center] (P2l) {\tiny\textbf{\textsc{High}}};
\path[every node/.style={font=\sffamily\small}]
(P2) edge [bend right] coordinate [pos=0.2] (top) (P1)
(P1) edge [bend right] (P2)
;
\path (P1) edge [loop left] node {} (P1);
\path (P2) edge [loop right] node {} (P2);
%%%%%%%%%%
%%% RECTANGLES %%%
\node[draw, thick, dotted, rounded corners, inner xsep=3.5em, inner ysep=1em, fit=(P1) (P2)] (box) {};
\node[fill=white] at (box.south) {\tiny\textbf{\textsc{States}}};
\end{tikzpicture}
\end{document}



• well it is my code I don't see you added anything to it! I already made this. Nov 4 '20 at 19:23
• @Dalek Well, before stating that someone trying to help has just copy-pasted your code I suggest you should first read carefully THEN make an advised comment Nov 4 '20 at 19:27
• It is slightly different and I didn't say you copy-pasted my code, I said I was already able to make to this point and my question is not this. I have a problem using figures from this \usepackage{tikzlings} package inside the node. Nov 4 '20 at 19:31
• Dalek, I updated my code, hope this help. Nov 4 '20 at 19:47
• Thanks it looks awesome. Would you kindly provide an answer about how I can make this heart shape with heart-rate inside as a symbol which I can also call inside nodes of my graph? I will appreciate in advance. Nov 4 '20 at 19:56

A small variation.mostly off-topic of nice @ BambOo answer (+1), for fun and exercise. Main differences are in defined styles, which enable to write shorter and more clear code:

\documentclass[margin=3mm]{standalone}
\usepackage{newtxtext} % provide also bold small caps fonts
\usepackage{tikzlings}
\usetikzlibrary{arrows.meta,
fit}

\begin{document}
\begin{tikzpicture}[
every edge/.style = {draw,-Straight Barb},
every label/.append style = {font=\scriptsize\bfseries\scshape, % work with newtxtext
fill=white,inner ysep=1pt},
FIT/.style = {draw, densely dotted, rounded corners,
inner ysep=1em, fit=#1},
img/.style = {fill=yellow!50},
state/.style 2 args = {path picture={\node[inner sep=0pt] (#1) at (\ppbb.center)
{\tikz[scale=0.6] {#2}};}}
]
\def\ppbb{path picture bounding box}
% states
\fill[img] (0,0) circle (6mm) [state={P1}{\penguin[body=teal]}] ;
\node (p1) [label=below:Low] at (P1.south) {};
\fill[img] (3,0) circle (6mm) [state={P2}{\penguin[body=red]}] ;
\node (p2) [label=below:High] at (P2.south) {};