[Tex/LaTex] Package Pgf error: No shape named lds1lds1L1 is known

tikz-pgftikz-pic

I want to design a hidden Markov model using LaTeX. I write the below code, but I have a problem in drawing paths correctly. I encountered the error Package Pgf error: No shape named lds1lds1L1 is known. How can I solve it?

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

Best Answer

This is essentially a duplicate of How to give a name to \pic. I still feel that this is a bug in terms of pic node anchor access. For now you can do the following

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

The problem is somehow at the start of the path to access the anchors by doubling the prefixes but I didn't have time to go into it.

Also, I've removed the styles outside the pic and some redundant package declarations.