[Tex/LaTex] Explain formula using beamer

beamerformattingtikz-pgf

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.

Best Answer

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