# Explain formula using beamer

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.

• Can you prepare a document with just that one slide? Jul 11, 2015 at 14:19
• The linked document has fractions. I assume you want to highlight the terms in the numerator and denominator independently? Jul 11, 2015 at 14:33
• @MikeRenfro, I added an image. It is exactly what I want to do Jul 11, 2015 at 14:44
• You may want to have a look at this example : texample.net/tikz/examples/beamer-arrows Edit, OK, the example you cite is similar.
– DRi
Oct 26, 2018 at 8:42

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}
\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:

## Update

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

\documentclass{beamer}
\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:

• 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. Jul 11, 2015 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? Jul 11, 2015 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' Jul 11, 2015 at 14:49
• I have though tikz installed and it is working fine. Jul 11, 2015 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. Jul 11, 2015 at 14:51

Gonzalo'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.

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

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

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

\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*}
\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}

Using the annotate-equations package that I've just put on CTAN (building on top of tikzmark and the previous answers here), you can write your example as follows:

\documentclass{article}

\usepackage{amsmath}
\usepackage{annotate-equations}

\begin{document}

\renewcommand{\eqnhighlightheight}{\mathstrut}

\begin{equation*}
\eqnmark{post}{p(\theta | D)} = \frac{\eqnmark{lik}{p(D|\theta)} \eqnmark{prior}{p(\theta)}} {\eqnmark{marglik}{p(D)}}
\end{equation*}
\annotate[yshift=2.5em]{above,right}{lik}{likelihood}
\annotate[yshift=1em]{above,right}{prior}{prior}
\annotate[yshift=1em]{above,left}{post}{posterior}
\annotate[yshift=-1em]{below,right}{marglik}{marginal likelihood}

\end{document}

Which results in

The package also supports highlighting and annotating multiple targets with the same text; you can find detailed explanation of the features in the user manual :)

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

## 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);
}

\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}
}

\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}

There is no need for absolute coordinates and for the instable commands \tikz[remember picture,overlay] and \tikzmark. We can just let Latex math do the work using \overset or \underset. The trick is to prohibit tikz from creating a big bounding box, which could blow the formula apart.

This solution is not restricted to beamer.

\documentclass[16pt]{beamer}

\usepackage{arev}
\renewcommand{\familydefault}{\sfdefault}

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

\newcommand\overnote[3][0em,0em]{% %[relative position] %note above %orig text
\ensuremath{
\overset{
\begin{tikzpicture}
\coordinate (refpoint) at (0,0);
\begin{pgfinterruptboundingbox}
\node[draw=red,fill=white,ellipse, very thick,anchor=south] (content) at ($(0,20pt)+({#1})$) {#2};
\draw[red, very thick, ->,-triangle 45] (content.south) to[out=-90,in=+90] (0pt,10pt) to (0pt,0pt);
\end{pgfinterruptboundingbox}
\end{tikzpicture}
}{#3}%
}%
}

\newcommand\undernote[3][0em,0em]{% %[relative position] %note above %orig text
\ensuremath{
\underset{
\begin{tikzpicture}
\coordinate (refpoint) at (0,0);
\begin{pgfinterruptboundingbox}
\node[draw=red,fill=white,ellipse, very thick,anchor=north] (content) at ($(0,-20pt)+({#1})$) {#2};
\draw[red, very thick, ->,-triangle 45] (content.north) to[out=+90,in=-90] (0pt,-10pt) to (0pt,0pt);
\end{pgfinterruptboundingbox}
\end{tikzpicture}
}{#3}%
}%
}

\begin{document}
\begin{frame}
\begin{center}
$\overnote[-2cm,0cm]{Above a}{a} = \frac{ \sqrt{ b + \overnote{Above c}{c} } }{ \underbrace{ \undernote[-2cm,-0.5cm]{Below d}{d} + \undernote[1.5cm,0cm]{Below e}{e} }_{ \undernote[-0.5cm,-1cm]{Below f}{f} } }$
\end{center}
\end{frame}
\end{document}