0

I was wondering if it is possible to replicate the following figure of an HMM in latex. If so, which alteration to my code below should be used? Any help would be much appreciated.

HMM_Paper

Note that the following code

\begin{tikzpicture}[
shorten >=1pt,
node distance=2cm,
auto,
state/.style={rectangle, draw=black!60, very thick, minimum size=5mm
},
]
\node[state] (l_1) {$\ell_1$};
\node[state] (l_2) [right of=l_1] {$\ell_2$};
\node[state] (l_3) [right of=l_2] {$\ell_3$};
\node[state] (l_i) [above of=l_3] {$\ell_i$};
\node[state] (l_4) [right of=l_3] {$\ell_4$};
\node[state] (l_5) [right of=l_4] {$\ell_5$};
\node[state] (l_6) [right of=l_5] {$\ell_6$};
\node[state] (end) [right of=l_6] {$\ell_e$};
\path[->] (l_1) edge node [swap] {1} (l_2)
(l_2) edge node [swap] {1} (l_3)
(l_3) edge node {3/5} (l_i)
edge node [swap] {2/5} (l_4)
(l_i) edge node {3/5} (l_4)
edge [loop above] node {2/5} ()
(l_4) edge node [swap] {1} (l_5)
(l_5) edge node [swap] {1} (l_6)
(l_6) edge node [swap] {1} (end)
(end) edge [loop right] node {1} ();
\end{tikzpicture}

outputs the figure (the transition probabilities here are the exponential of the probabilities in the first figure) outputFig

So, how should I best include multiline latex within the nodes?

6
  • 1
    Do you have any code so people have a look at it, if not tex.stackexchange.com/questions/498919/… should give a good starting point.
    – Paul A
    Commented Sep 5, 2022 at 9:37
  • @PaulA Thanks for your comment. Refer to the edited question for my code so far.
    – UNOwen
    Commented Sep 5, 2022 at 11:04
  • Hi and welcome to TeX.SE. You are expected to add a minimum working example so others can copy/paste and compile to assess the problem. You will get help much sooner. Currently, you have posted a snippet.
    – Celdor
    Commented Sep 5, 2022 at 11:37
  • @Celdor Thank you. I assume you mean that I should include necessary preamble and \begin{document} etc. so that the code can compile on its own?
    – UNOwen
    Commented Sep 5, 2022 at 11:42
  • 1
    @Celdor Okay, thank you very much. I'll bear this in mind for any future questions I might have.
    – UNOwen
    Commented Sep 5, 2022 at 11:50

2 Answers 2

2

Add align=center or align=left to your node's options, e.g. as shown individually, or via style for many. Use \\ for line breaks.

example

\documentclass[10pt, border=20pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
shorten >=1pt,
node distance=2cm,
auto,
state/.style={rectangle, draw=black!60, very thick, minimum size=5mm
},
]
\node[state, align=left] (l_1) {$\ell_1$\\A: $+1.16$\\T: $-0.22$};
\node[state] (l_2) [right of=l_1] {$\ell_2$};
\node[state] (l_3) [right of=l_2] {$\ell_3$};
\node[state] (l_i) [above of=l_3] {$\ell_i$};
\node[state] (l_4) [right of=l_3] {$\ell_4$};
\node[state] (l_5) [right of=l_4] {$\ell_5$};
\node[state] (l_6) [right of=l_5] {$\ell_6$};
\node[state] (end) [right of=l_6] {$\ell_e$};
\path[->] (l_1) edge node [swap] {1} (l_2)
(l_2) edge node [swap] {1} (l_3)
(l_3) edge node {3/5} (l_i)
edge node [swap] {2/5} (l_4)
(l_i) edge node {3/5} (l_4)
edge [loop above] node {2/5} ()
(l_4) edge node [swap] {1} (l_5)
(l_5) edge node [swap] {1} (l_6)
(l_6) edge node [swap] {1} (end)
(end) edge [loop right] node {1} ();
\end{tikzpicture}
\end{document}
2

Result

\documentclass{article}

    \usepackage{tikz}
    \usepackage{amsmath}
    \usetikzlibrary{matrix}

    \usepackage{tikz-cd}
    \usetikzlibrary{arrows.meta, 
            shapes.geometric}
    \usepackage{amsmath}

\begin{document}

\begin{tikzpicture}[
shorten >=1pt,
node distance=3cm,
auto,
state/.style={rectangle, draw=black!60, very thick, minimum size=5mm
},
]
\node[state] (l_1) {
    \begin{tabular}{c} \( A:1.16 \) \\ \( T:-0.22 \) \end{tabular}
};
\node[state] (l_2) [right of=l_1] {
    \begin{tabular}{c} \( C:1.16 \) \\ \( G:-0.22 \) \end{tabular}
};
\node[state] (l_3) [right of=l_2]  {
    \begin{tabular}{c} \( A:1.16 \) \\ \( C:-0.22 \) \end{tabular}
};
\node[state] (l_i) [above of=l_3] {
    \begin{tabular}{c} \( A:-0.22 \) \\ \( C:0.47 \) \\ \(G:-0.22\) \\    \(T:-0.22\) \end{tabular}
};
\node[state] (l_4) [right of=l_3]  {
    \begin{tabular}{c} \( A:1.39 \) \end{tabular}
};
\node[state] (l_5) [right of=l_4]  {
    \begin{tabular}{c} \( G:-0.22 \) \\ \( T:1.16 \) \end{tabular}
};
\node[state] (l_6) [right of=l_5]  {
    \begin{tabular}{c} \( C:1.16 \) \\ \( G:-0.22 \) \end{tabular}
};
\path[->] (l_1) edge node [swap] {1} (l_2)
(l_2) edge node [swap] {0} (l_3)
(l_3) edge node {-0.51} (l_i)
edge node [swap] {-0.92} (l_4)
(l_i) edge node {-0.51} 
(l_4) edge [loop above] node {-0.92} ()
(l_4) edge node [swap] {0} (l_5)
(l_5) edge node [swap] {0} (l_6);
\end{tikzpicture}

\end{document}
2
  • Using [align] in a tikz node is equivalent to using a tabular. Presumably, that is how it is implemented. Commented Sep 5, 2022 at 14:28
  • 1
    I added a screenshot of your drawing, using \documentclass[10pt, border=10pt]{standalone} . // Using a table inside a node is a nice idea. Using 2 columns would e.g. allow to align labels (A, T, C, G, etc.) and values separately.
    – MS-SPO
    Commented Sep 5, 2022 at 14:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .