I am preparing a presentation on statistics. I want to include a slide on Bayes' Theorem which says:

p(\theta | D) = \frac{p(D|\theta) p(\theta)} {p(D)}

I want to explain each term using arrows. How can I do this?

I tried to follow this example but my main problem is that I have a fraction. How can I split the fraction?

This is exactly what I want to do. Without the last line.

  • 1
    Can you prepare a document with just that one slide? – hkBst Jul 11 '15 at 14:19
  • The linked document has fractions. I assume you want to highlight the terms in the numerator and denominator independently? – Mike Renfro Jul 11 '15 at 14:33
  • @MikeRenfro, I added an image. It is exactly what I want to do – aloha Jul 11 '15 at 14:44
up vote 17 down vote accepted

You can use the tikzmark library; the idea is to place some marks at the desired locations and then to use those marks to have pointers for the explanations:

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{tikz}
\usetikzlibrary{tikzmark,positioning}

\begin{document}

\begin{frame}
\begin{exampleblock}{Baye's theorem}
\[
\tikzmark{ptd}p(\theta\, |\, D) = \frac{\tikzmark{pdt}p(D\,|\,\theta) p(\theta)}{\tikzmark{pd}p(D)}
\]
\begin{tikzpicture}[
  remember picture,
  overlay,
  expl/.style={draw=orange,fill=orange!30,rounded corners,text width=3cm},
  arrow/.style={red!80!black,ultra thick,->,>=latex}
]
\node<2->[expl] 
  (ptdex) 
  at (2,-2cm)
  {Some explanation};
\node<3->[expl] 
  (pdtex) 
  at (6,3.5cm)
  {Some other explanation; this one is a little longer};
\node<4->[expl] 
  (pdex) 
  at (9,-3cm)
  {Some other explanation};
\draw<2->[arrow]
  (ptdex) to[out=100,in=180] ([yshift=0.5ex]{pic cs:ptd});  
\draw<3->[arrow]
  (pdtex.west) to[out=180,in=180] ([yshift=0.5ex]{pic cs:pdt});  
\draw<4->[arrow]
  (pdex.north) to[out=90,in=180] ([yshift=0.5ex]{pic cs:pd});  
\end{tikzpicture}
\end{exampleblock}
\end{frame}

\end{document}

An animation of the result:

enter image description here

Update

If, for some reason, the tikzmark library is not available and no overlays are wanted, use

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{tikz}

\newcommand\tikzmark[1]{
  \tikz[remember picture,overlay] \coordinate (#1);
}

\begin{document}

\begin{frame}
\begin{exampleblock}{Baye's theorem}
\[
\tikzmark{ptd}p(\theta\, |\, D) = \frac{\tikzmark{pdt}p(D\,|\,\theta) p(\theta)\tikzmark{pt}}{\tikzmark{pd}p(D)}
\]
\begin{tikzpicture}[
  remember picture,
  overlay,
  expl/.style={draw=orange,fill=orange!30,rounded corners,text width=3cm},
  arrow/.style={red!80!black,ultra thick,->,>=latex}
]
\node[expl] 
  (ptdex) 
  at (2,-2cm)
  {Some explanation};
\node[expl] 
  (pdtex) 
  at (4,3.5cm)
  {Some other explanation; this one is a little longer};
\node[expl] 
  (pdex) 
  at (9,-3cm)
  {Some other explanation};
\node[expl] 
  (ptex) 
  at (8,3.5cm)
  {Some other explanation; this one is a little longer};
\draw[arrow]
  (ptdex) to[out=100,in=180] ([yshift=0.5ex]{ptd});  
\draw[arrow]
  (pdtex.west) to[out=180,in=180] ([yshift=0.5ex]{pdt});  
\draw[arrow]
  (pdex.north) to[out=90,in=180] ([yshift=0.5ex]{pd});  
\draw[arrow]
  (ptex.east) to[out=0,in=0] ([yshift=0.5ex]{pt});  
\end{tikzpicture}
\end{exampleblock}
\end{frame}

\end{document}

The result:

enter image description here

  • This is almost what I want. Can it be done using tikz? I am having troubles downloading other packages. I am using ubuntu. I'd rather do it with tikz. – aloha Jul 11 '15 at 14:46
  • @po6 tikzmark is a TikZ library. It comes with modern LaTeX systems. Don't you have it installed? Why is it "almost" what you want? What is missing? – Gonzalo Medina Jul 11 '15 at 14:47
  • I still need to define p(\theta). I tried running the code you posted, I got an error: `can't find 'tikzlibrarytikzmark.code.tex' – aloha Jul 11 '15 at 14:49
  • I have though tikz installed and it is working fine. – aloha Jul 11 '15 at 14:50
  • @po6 Please see my updated answer, The version under "Update" works with just TikZ. Since internal calculations are involved, you need to run the code three times for the arrows to reach their final positions. – Gonzalo Medina Jul 11 '15 at 14:51

Gonzolo's answer is probably better (certainly more flexible), but I'm not going to throw this one away. There's nothing special in having fractions here, it's just a pain to get all the braces and semicolons correctly placed for the \node commands. I've also relabeled the nodes to have abbreviated names instead of numbers.

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows,shapes}

\begin{document}
\tikzstyle{every picture}+=[remember picture]
\everymath{\displaystyle}

\begin{frame}
\tikzstyle{na} = [baseline=-.5ex]

\begin{itemize}[<+-| alert@+>]
    \item Class Prior Probability
        \tikz[na]\node [coordinate] (nCPP) {};
    \item Likelihood
        \tikz[na]\node [coordinate] (nL) {};
\end{itemize}

\begin{equation*}
\frac{
\tikz[baseline]{\node[fill=blue!20,anchor=base] (tL) {$p(D|\theta)$};}
\tikz[baseline]{\node[fill=red!20,anchor=base] (tCPP) {$p(\theta)$};}
}
{
\tikz[baseline]{\node[fill=green!20,anchor=base] (tPPP) {$p(D)$};}
}
=
\tikz[baseline]{\node[fill=yellow!20,anchor=base] (tPP) {$p(\theta | D)$};}
\end{equation*}
\begin{itemize}[<+-| alert@+>]
    \item Predictor Prior Probability
        \tikz[na]\node [coordinate] (nPPP) {};
    \item Posterior probability
        \tikz[na] \node[coordinate] (nPP) {};
\end{itemize}

\begin{tikzpicture}[overlay]
        \path[->]<1-> (nCPP) edge [bend left] (tCPP);
        \path[->]<2-> (nL) edge [bend left] (tL);
        \path[->]<3-> (nPPP) edge [out=0, in=0] (tPPP);
        \path[->]<4-> (nPP) edge [out=0, in=-90] (tPP);
\end{tikzpicture}

\end{frame}
\end{document}

Full credit to Gonzolo's Code. This is just a cleaner (in my opinion) way of obtaining the same effect.

Example image

MWE:

\documentclass[10pt,english,compress]{beamer}

\usepackage{tikz}
\usetikzlibrary{shapes,calc}

%My predefined color
\definecolor{myblue}{rgb}{0.1,0.15,0.7}

%Set color for Annotations
\colorlet{annotcol}{myblue!80!black}

%Store coordinates of points
\newcommand\tikzmark[1]{
  \tikz[remember picture,overlay] \coordinate (#1);
  }

%Command to add annotation above
\newcommand{\noteup}[3][0em,0em]{
\begin{tikzpicture}[
  remember picture,
  overlay]
\node[draw=annotcol,fill=white,ellipse,very thick,minimum width=2cm] 
  (mynode) 
  at ([shift=($({#1})+({0em,+5.5em})$)]{#2.north})
  {\begin{minipage}{2cm}\centering #3\end{minipage}};
\draw[annotcol,very thick,->,>=latex]
  (mynode.south) to[out=-90,in=+90] ([xshift=0.5em,yshift=1.3em]{#2}); 
\end{tikzpicture}
}

%Command to add annotation below
\newcommand{\notedown}[3][0em,0em]{
\begin{tikzpicture}[
  remember picture,
  overlay]
\node[draw=annotcol,fill=white,ellipse,very thick,minimum width=2cm] 
  (mynode) 
  at ([shift=($({#1})+({0em,-4em})$)]{#2.south})
  {\begin{minipage}{2cm}\centering #3\end{minipage}};
\draw[annotcol,very thick,->,>=latex]
  (mynode.north) to[out=90,in=-90] ([xshift=0.5em,yshift=-0.1em]{#2}); 
\end{tikzpicture}
}


%Beamer Slides
\begin{document}
\frame{

% Equation
\begin{center}
{\Large $\tikzmark{P}P = \tikzmark{PT}P_T + \tikzmark{PP}P^+ + \tikzmark{PN}P^-$}
\end{center}

%Annotations
\notedown{P}{Total Value of P}
\noteup[-1em,0em]{PT}{Value of P for T}
\noteup[5em,0em]{PP}{Value of P for +}

}
\end{document}

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.