Search code examples
latexbeamertikz

Draw arrow between two tikz pictures


I want to draw an arrow from one tikzpicture to another, to include in my Beamer presentation I am making in Rmarkdown. I tried following the answer to this question: Connecting two tikz pictures with arrow. However, it does not work for me as it connects the two images together instead. This is my initial code (to make it fit with bullet points in the slide):

Edit: I have inlcuded my YAML in Rmarkdown.

---
title: "This title must be included in the first slide but is very very long"
subtitle: "Masters Thesis presentation"
institute: "My school"
author: First Last
date: March 2022
output: 
  beamer_presentation: 
    theme: "Madrid"
header-includes:
   - \AtBeginDocument{\title[short title for footline]{This title must be included in the first slide but is very very long}}
   - \usepackage{tikz}
   - \usepackage{subfig}
   - \usepackage{natbib}
   - \usetikzlibrary{automata, positioning, arrows, calc}
   - \usepackage{algorithm}
   - \usepackage{algpseudocode}
   - \usepackage{caption}
   - \captionsetup[table]{position=bottom}
   - \usepackage[utf8]{inputenc}
   - \usepackage{graphics}
   - \usepackage{amsmath}
   - \usepackage{bm}
---
# Recode to three states

\begin{columns}
\column{0.5\textwidth}
\begin{figure}[ht]
  \centering
  \resizebox{0.6\textwidth}{!}{% 
  \begin{tikzpicture}[every state/.style={minimum width={2cm} ,thick,align=center}]
  \node[state] (1) {1: DEAD};
  \node[state] at (3, 2) (3) {3:VFT};
  \node[state] at (6, 0) (5) {5:ROSC};
  \node[state] at (1.5, -3.3) (2) {2:ASY};
  \node[state] at (4.5, -3.3) (4) {4:PEA};
  
  \draw[<-] (1) -- node [midway,above] {} (2);
  \draw[<-] (1) -- node [midway,below] {} (3);
  \draw[<-] (1) -- node [midway,below] {} (4);
  \draw[<-] (1) -- node [midway,below] {} (5);
  
  \draw[<->] (2) -- node [midway,below] {} (3);
  \draw[<->] (2) -- node [midway,below] {} (4);
  \draw[<->] (2) -- node [midway,below] {} (5);
  
  \draw[<->] (3) -- node [midway,below] {} (4);
  \draw[<->] (3) -- node [midway,below] {} (5);
  
  \draw[<->] (4) -- node [midway,below] {} (5);
  \end{tikzpicture}
  }%
\end{figure}


\column{0.4\textwidth}
\begin{figure}%[H]
    \centering
    \resizebox{0.6\textwidth}{!}{% 
    \begin{tikzpicture}[>=stealth', shorten >=1pt, auto, scale=1, 
    transform shape, align=center, 
    state/.style={circle, draw, minimum size=2cm}]

    \node[state] at (0,0) (5) {5:ROSC};  
    \node[state] at (3,-2) (1) {1:DEAD};
    \node[state] at (3,2) (2) {2:PEASY};

    \path[->] (5) edge node {$h_{51}$} (1)
              (5) edge node {$h_{52}$} (2)
              (2) edge node {$h_{21}$} (1)
              (2) edge [bend left] node [right] {$h_{25}$} (5);

    \end{tikzpicture}
    }%
\end{figure}
\end{columns}

Steps to recode:

>- Combine states 2 and 4 into state 2
>- Write function that discards redundant rows (2 $\rightarrow$ 2)
>- Delete `from` = 3 rows
>- Expand dataset to keep censored cases
>- Delete `to` = 3 rows

Image of the original tikzpictures without arrow between them Which works, but does not have an arrow between the two tikzpictures.

Here is what I tried following the answer of the link I provided above, where I tried putting the images into one tikzpicture and using scopes.

\begin{center}
\begin{tikzpicture}[every state/.style={minimum width={2cm} ,thick,align=center}]

\begin{scope}
  \node[state] (1) {1: DEAD};
  \node[state] at (3, 2) (3) {3:VFT};
  \node[state] at (6, 0) (5) {5:ROSC};
  \node[state] at (1.5, -3.3) (2) {2:ASY};
  \node[state] at (4.5, -3.3) (4) {4:PEA};
  
  \draw[<-] (1) -- node [midway,above] {} (2);
  \draw[<-] (1) -- node [midway,below] {} (3);
  \draw[<-] (1) -- node [midway,below] {} (4);
  \draw[<-] (1) -- node [midway,below] {} (5);
  
  \draw[<->] (2) -- node [midway,below] {} (3);
  \draw[<->] (2) -- node [midway,below] {} (4);
  \draw[<->] (2) -- node [midway,below] {} (5);
  
  \draw[<->] (3) -- node [midway,below] {} (4);
  \draw[<->] (3) -- node [midway,below] {} (5);
  
  \draw[<->] (4) -- node [midway,below] {} (5);
\end{scope}



\begin{scope}[xshift=6cm]
    \node[state] at (0,0) (5) {5:ROSC};  
    \node[state] at (3,-2) (1) {1:DEAD};
    \node[state] at (3,2) (2) {2:PEASY};
    \path[->] (5) edge node {$h_{51}$} (1)
              (5) edge node {$h_{52}$} (2)
              (2) edge node {$h_{21}$} (1)
              (2) edge [bend left] node [right] {$h_{25}$} (5);
\end{scope}

\draw[red,->] (6,0) -- (10,0);

\end{tikzpicture}
\end{center}

For some reason the pictures are now connected through the one node called 5: ROSC. Image of the wrong tikzpicture

I want the two original tikzpictures with an arrow going between them, preferrably at the height of the 5:ROSC node.

Edit 2: I have included an image of what I would like the arrow to look like. Image of arrow between the two tikzpictures


Solution

  • I suggest to use tikzmarks for this job:

    \documentclass{beamer}
    
    \usepackage{tikz}
    \usetikzlibrary{arrows}
    \usetikzlibrary{tikzmark}
    
    
    \begin{document}
        
    \begin{frame}
    \begin{columns}
    \begin{column}{0.5\textwidth}
    \centering
      \begin{tikzpicture}[scale=0.6,transform shape,every state/.style={minimum width={2cm} ,thick,align=center},state/.style={circle, draw, minimum size=2cm}]
      \node[state] (1) {1: DEAD};
      \node[state] at (3, 2) (3) {3:VFT};
      \node[state] at (6, 0) (5) {\tikzmark{foo}5:ROSC};
      \node[state] at (1.5, -3.3) (2) {2:ASY};
      \node[state] at (4.5, -3.3) (4) {4:PEA};
      
      \draw[<-] (1) -- node [midway,above] {} (2);
      \draw[<-] (1) -- node [midway,below] {} (3);
      \draw[<-] (1) -- node [midway,below] {} (4);
      \draw[<-] (1) -- node [midway,below] {} (5);
      
      \draw[<->] (2) -- node [midway,below] {} (3);
      \draw[<->] (2) -- node [midway,below] {} (4);
      \draw[<->] (2) -- node [midway,below] {} (5);
      
      \draw[<->] (3) -- node [midway,below] {} (4);
      \draw[<->] (3) -- node [midway,below] {} (5);
      
      \draw[<->] (4) -- node [midway,below] {} (5);
      \end{tikzpicture}
    \end{column}
    
    \begin{column}{0.4\textwidth}
    \centering
        \begin{tikzpicture}[scale=0.6,transform shape, >=stealth', shorten >=1pt, auto, scale=1, 
        transform shape, align=center, 
        state/.style={circle, draw, minimum size=2cm}]
    
        \node[state] at (0,0) (5) {5:ROSC};  
        \node[state] at (3,-2) (1) {1:DEAD};
        \node[state] at (3,2) (2) {\tikzmark{bar}2:PEASY};
    
        \path[->] (5) edge node {$h_{51}$} (1)
                  (5) edge node {$h_{52}$} (2)
                  (2) edge node {$h_{21}$} (1)
                  (2) edge [bend left] node [right] {$h_{25}$} (5);
    
        \end{tikzpicture}
    \end{column}
    \end{columns}
    
    \begin{tikzpicture}[remember picture,overlay]
    \draw[red,->] (pic cs:foo) -- (pic cs:bar);
    \end{tikzpicture}
    
    \end{frame} 
        
    \end{document}
    

    enter image description here

    (needs at least two compilations to get the coordinates right)


    Here the same in rmarkdown:

    ---
    title: "This title must be included in the first slide but is very very long"
    subtitle: "Masters Thesis presentation"
    institute: "My school"
    author: First Last
    date: March 2022
    output: 
      beamer_presentation: 
        theme: "Madrid"
    header-includes:
       - \AtBeginDocument{\title[short title for footline]{This title must be included in the first slide but is very very long}}
       - \usepackage{tikz}
       - \usepackage{subfig}
       - \usepackage{natbib}
       - \usetikzlibrary{automata, positioning, arrows, calc,tikzmark}
       - \usepackage{algorithm}
       - \usepackage{algpseudocode}
       - \usepackage{caption}
       - \captionsetup[table]{position=bottom}
       - \usepackage[utf8]{inputenc}
       - \usepackage{graphics}
       - \usepackage{amsmath}
       - \usepackage{bm}
    ---
    # Recode to three states
    
    \begin{columns}
    \begin{column}{0.5\textwidth}
    \centering
      \begin{tikzpicture}[scale=0.6,transform shape,every state/.style={minimum width={2cm} ,thick,align=center},state/.style={circle, draw, minimum size=2cm}]
      \node[state] (1) {1: DEAD};
      \node[state] at (3, 2) (3) {3:VFT};
      \node[state] at (6, 0) (5) {\tikzmark{foo}5:ROSC};
      \node[state] at (1.5, -3.3) (2) {2:ASY};
      \node[state] at (4.5, -3.3) (4) {4:PEA};
      
      \draw[<-] (1) -- node [midway,above] {} (2);
      \draw[<-] (1) -- node [midway,below] {} (3);
      \draw[<-] (1) -- node [midway,below] {} (4);
      \draw[<-] (1) -- node [midway,below] {} (5);
      
      \draw[<->] (2) -- node [midway,below] {} (3);
      \draw[<->] (2) -- node [midway,below] {} (4);
      \draw[<->] (2) -- node [midway,below] {} (5);
      
      \draw[<->] (3) -- node [midway,below] {} (4);
      \draw[<->] (3) -- node [midway,below] {} (5);
      
      \draw[<->] (4) -- node [midway,below] {} (5);
      \end{tikzpicture}
    \end{column}
    
    \begin{column}{0.4\textwidth}
    \centering
        \begin{tikzpicture}[scale=0.6,transform shape, >=stealth', shorten >=1pt, auto, scale=1, 
        transform shape, align=center, 
        state/.style={circle, draw, minimum size=2cm}]
    
        \node[state] at (0,0) (5) {5:ROSC};  
        \node[state] at (3,-2) (1) {1:DEAD};
        \node[state] at (3,2) (2) {\tikzmark{bar}2:PEASY};
    
        \path[->] (5) edge node {$h_{51}$} (1)
                  (5) edge node {$h_{52}$} (2)
                  (2) edge node {$h_{21}$} (1)
                  (2) edge [bend left] node [right] {$h_{25}$} (5);
    
        \end{tikzpicture}
    \end{column}
    \end{columns}
    
    \begin{tikzpicture}[remember picture,overlay]
    \draw[red,->] (pic cs:foo) -- (pic cs:bar);
    \end{tikzpicture}
    

    Update

    As your new sketch does not show any interaction at all between your arrow and your images, just add a third column:

    \documentclass{beamer}
    
    \usepackage{tikz}
    \usetikzlibrary{arrows}
    \usetikzlibrary{tikzmark}
    
    
    \begin{document}
        
    \begin{frame}
    \begin{columns}
    \begin{column}{0.5\textwidth}
    \centering
      \begin{tikzpicture}[scale=0.6,transform shape,every state/.style={minimum width={2cm} ,thick,align=center},state/.style={circle, draw, minimum size=2cm}]
      \node[state] (1) {1: DEAD};
      \node[state] at (3, 2) (3) {3:VFT};
      \node[state] at (6, 0) (5) {5:ROSC};
      \node[state] at (1.5, -3.3) (2) {2:ASY};
      \node[state] at (4.5, -3.3) (4) {4:PEA};
      
      \draw[<-] (1) -- node [midway,above] {} (2);
      \draw[<-] (1) -- node [midway,below] {} (3);
      \draw[<-] (1) -- node [midway,below] {} (4);
      \draw[<-] (1) -- node [midway,below] {} (5);
      
      \draw[<->] (2) -- node [midway,below] {} (3);
      \draw[<->] (2) -- node [midway,below] {} (4);
      \draw[<->] (2) -- node [midway,below] {} (5);
      
      \draw[<->] (3) -- node [midway,below] {} (4);
      \draw[<->] (3) -- node [midway,below] {} (5);
      
      \draw[<->] (4) -- node [midway,below] {} (5);
      \end{tikzpicture}
    \end{column}
    
    \begin{column}{.1\textwidth}
    \tikz\draw[blue,->] (0,0) -- (\textwidth,0);
    \end{column}
    
    \begin{column}{0.4\textwidth}
    \centering
        \begin{tikzpicture}[scale=0.6,transform shape, >=stealth', shorten >=1pt, auto, scale=1, 
        transform shape, align=center, 
        state/.style={circle, draw, minimum size=2cm}]
    
        \node[state] at (0,0) (5) {5:ROSC};  
        \node[state] at (3,-2) (1) {1:DEAD};
        \node[state] at (3,2) (2) {2:PEASY};
    
        \path[->] (5) edge node {$h_{51}$} (1)
                  (5) edge node {$h_{52}$} (2)
                  (2) edge node {$h_{21}$} (1)
                  (2) edge [bend left] node [right] {$h_{25}$} (5);
    
        \end{tikzpicture}
    \end{column}
    \end{columns}
    
    \end{frame} 
        
    \end{document}
    

    enter image description here