如何在 Latex 中使用 tikz 生成此节点和边缘结构?
我使用 创建了下图tikz
,现在我想在每个节点的顶部放置一些图形(例如,一个节点上放一颗心跳的心脏,另一个节点上放一个奶酪图形)?
我想要使用它来创建心形图形的原因tikz
是我的外部图像的质量降低了,因为我从一张图像中裁剪了心率并将其放在心形图形的顶部,并且不断调整其大小会降低其质量。
我还想将这个包中的一个角色\usepackage{tikzlings}
作为一个图形放在另一个节点的顶部,但它不起作用(例如\thing[cheese]
)甚至不使用此包,而是使用另一个包或仅在节点内创建一个奶酪图形)。
\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}}};
%links
\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}
答案1
目前,根据您的代码,我得到了以下图片:
根据您使用路径剪辑的图像的意图,我建议您使用基于path picture
第 15.6 节中介绍的语法的替代方法pgfmanual
。此替代方案给出了此答案底部的图。
\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}}};
%links
\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}
编辑
这是工作版本,其中包括对库中“低”和“高”节点位置的改进定义以及在节点中positioning
的包含。\tikzlings
\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}}};
%links
\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}
编辑 2:加入心跳形状和\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}}};
%links
\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}
答案2
一个小的变化。大部分都是离题的@BambOo 回答(+1),为了好玩和练习。主要的区别在于定义的样式,这样可以编写更短更清晰的代码:
\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) {};
% links
\path (P2) edge [bend right] (P1) % , coordinate [pos=0.2] (top)
(P1) edge [bend right] (P2)
(P1) edge [loop left] coordinate (L) (P1)
(P2) edge [loop right] coordinate (R) (P2);
%%%%%%%%%%
% automatom border
\node[FIT=(L) (P1) (p2) (R),
label={[anchor=center]below:States}] {};
\end{tikzpicture}
\end{document}