Example: Connecting text and graphics

Published 2007-04-10 | Author: Kjell Magne Fauske

With PGF 1.09 and later, it is possible to draw paths between nodes across different pictures. In this example I have used this feature to connect text with coordinate nodes placed on a picture. The picture is loaded from an external file A background grid is used to make it easier to place the coordinates manually.

Note. This only works with PDF(La)TeX, and you have to run PDFTeX twice.

Source: Inspired by a post to the latex-beamer-users list. The Daniell's pile illustration is made by Augustin E. Bolzan.

Download as: [PDF] [TEX]  •  [Open in writeLaTeX]

Connecting text and graphics

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.

% This example requires PGF >= 1.09 and only works wit PDFTeX
% You have to compile document twice to get correct placement of nodes.
\documentclass{beamer} %
\usepackage{tikz}
\usepackage{verbatim}

\usetikzlibrary{arrows,shapes,backgrounds}

\begin{document}
% For every picture that defines or uses external nodes, you'll have to
% apply the 'remember picture' style. To avoid some typing, we'll apply
% the style to all pictures.
\tikzstyle{every picture}+=[remember picture]
\tikzstyle{na} = [baseline=-.5ex]

\begin{frame}

\frametitle{Daniell's pile, saline bridge version}

\begin{columns}
    \begin{column}{0.4\paperwidth}
        % define source coordinates
        \begin{itemize}
            \item Anode \tikz[na] \coordinate (s-anode);
            \item Cathode \tikz[na] \coordinate (s-cathode);
            \item Saline bridge \tikz[na] \coordinate (s-bridge);
        \end{itemize}
        
    \end{column}
    \begin{column}{0.45\paperwidth}
        % Use a background grid to make it easier to find coordinates
        % When the coordinates have been found, remove the 
        % 'show background grid' option. 
        \tikzstyle{background grid}=[draw, black!50,step=.5cm]
        \begin{tikzpicture}[show background grid]
            % Put the graphic inside a node. This makes it easy to place the
            % graphic and to draw on top of it. 
            % The above right option is used to place the lower left corner
            % of the image at the (0,0) coordinate. 
            \node [inner sep=0pt,above right] 
                {\includegraphics[width=4cm]{img/daniells-pile}};
            % show origin
            \fill (0,0) circle (2pt);
            % define destination coordinates
            \path (0.7,2) coordinate (cathode)
                  (2,1.8) coordinate (bridge)
                  (2.75,2.5) coordinate (anode);
        \end{tikzpicture}
    \end{column}
\end{columns}

% define overlays
% Note the use of the overlay option. This is required when 
% you want to access nodes in different pictures.
\begin{tikzpicture}[overlay]
        \path[->,red,thick] (s-anode) edge [bend left] (anode);
        \path[->,blue,thick] (s-cathode) edge [bend left] (cathode);
        \path[->,red,thick] (s-bridge) edge [out=0, in=-90] (bridge);
\end{tikzpicture}

\end{frame}

\end{document}

Comments

  • #1 Karl, May 29, 2009 at 12:12 p.m.

    Does not work with PGF 2.00, arrows are painted very randomly.

  • #2 Karl Beauchamp, May 29, 2009 at 12:15 p.m.

    my fault, the osx previewer didn't refresh after second pdflatex run. Works fantastic

  • #3 Jason Waskiewicz, January 14, 2010 at 12:58 a.m.

    I really appreciated this diagram. I just used the methods in this document to map arrows from a list to the parts of a leaf in a .jpg file. Very slick!

  • #4 Vladimir S., November 3, 2012 at 4:21 p.m.

    This is really great example! Thank you!

  • #5 Jaunita, April 17, 2013 at 5:18 a.m.

    Have you ever considered about including a little bit more than just your articles? I mean, what you say is fundamental and all. But think of if you added some great photos or video clips to give your posts more, "pop"! Your content is excellent but with pics and videos, this site could certainly be one of the greatest in its field.

    Great blog!

  • #6 Lucretia, May 2, 2013 at 8:05 a.m.

    I would like to thank you for the efforts you've put in penning this site. I'm hoping to see the same high-grade blog posts by you later on as well. In truth, your creative writing abilities has inspired me to get my very own website now ;)

Post a comment

Markdown syntax enabled. No HTML allowed.