我想使用 LaTeX 设计一个隐马尔可夫模型。我编写了以下代码,但在正确绘制路径时遇到问题。我遇到了错误Package Pgf error: No shape named lds1lds1L1 is known
。我该如何解决?
\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usetikzlibrary{positioning}
\usepackage{color}
\usepackage{caption}
\pgfmathsetseed{5}
\begin{document}
\begin{tikzpicture}[ lds/.pic={
\tikzstyle{main}=[circle, minimum size = .2cm, thick, draw =black!80, node distance = 3mm]
\tikzstyle{connect}=[-latex, thick]
\tikzstyle{box}=[rectangle, draw=black!100]
\node[box,draw=white!100] (Latent) {};
\node[main,minimum size=.2cm] (L1) [right=of Latent] {$x_1$};
\node[main,minimum size=.2cm] (L2) [right=of L1] {$x_2}$};
\node[main,minimum size=.2cm] (Lt) [right=of L2] {$x_T}$};
\node[box,draw=white!100] (Observed) [below=of Latent] {};
\node[main,fill=black,text=white,minimum size=.2cm] (O1) [right=of Observed,below=of L1] {$y_1$};
\node[main,fill=black,text=white,minimum size=.2cm] (O2) [right=of O1,below=of L2] {$y_2$};
\node[main,fill=black,text=white,minimum size=.2cm] (Ot) [right=of O2,below=of Lt] {$y_T$};
\path (L2) -- node[auto=false]{\ldots} (Lt);
\path (L1) edge [connect] (L2)
(L2) -- node[auto=false]{\ldots} (Lt);
(O2) -- node[auto=false]{\ldots} (Ot);
\path (L1) edge [connect] (O1);
\path (L2) edge [connect] (O2);
\path (Latent) edge [connect] (O1);
\draw[dashed] [below=of L1,above=of O1];
} ]
\pic(lds1) at (0,16){lds}; % <---the problem is here!!
\end{tikzpicture}
\end{document}
答案1
这基本上是如何给 \pic 命名。我仍然觉得这是 pic 节点锚点访问的一个错误。目前您可以执行以下操作
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,calc,positioning}
\usepackage{caption}
\pgfmathsetseed{5}
\tikzset{main/.style={circle, minimum size = .2cm, thick, draw =black!80, node distance = 3mm},
connect/.style={-latex, thick},
box/.style={rectangle, draw=black}
}
\begin{document}
\begin{tikzpicture}[ lds/.pic={
\node[box,draw=white] (-Latent) {};
\node[main,minimum size=.2cm, right=of -Latent] (-L1) {$x_1^{(j)}$};
\node[main,minimum size=.2cm] (-L2) [right=of -L1] {$x_2^{(j)}$};
\node[main,minimum size=.2cm] (-Lt) [right=of -L2] {$x_T^{(j)}$};
\node[box,draw=white!100] (-Observed) [below=of -Latent] {};
\node[main,fill=black,text=white,minimum size=.2cm] (-O1) [right=of -Observed,below=of -L1] {$y_1^{(j)}$};
\node[main,fill=black,text=white,minimum size=.2cm] (-O2) [right=of -O1,below=of -L2] {$y_2^{(j)}$};
\node[main,fill=black,text=white,minimum size=.2cm] (-Ot) [right=of -O2,below=of -Lt] {$y_T^{(j)}$};
\draw (-L2) -- node[auto=false]{\ldots} (-Lt);
\draw (-L1.east) edge [connect] (-L2)
(-L2) -- node[auto=false]{\ldots} (-Lt)
(-O2) -- node[auto=false]{\ldots} (-Ot);
\path (-L1.south) edge [connect] (-O1);
\path (-L2.south) edge [connect] (-O2);
\path (-Latent.south) edge [connect] (-O1);
%\draw[dashed] [below=of -L1,above=of -O1]; % No path here
}]
\pic (lds1) at (0,16) {lds}; % <---the problem is here!!
\end{tikzpicture}
\end{document}
问题出在路径的开始处,通过加倍前缀来访问锚点,但我没有时间去深入研究它。
另外,我删除了图片之外的样式和一些多余的包声明。