# \verbatim [beamer package]

June 11, 2012
By

(This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers)

Once again working on my slides for the AMSI Lecture 2012 tour, it took me a while to get the following LaTeX code (about the family reunion puzzle) to work:

\begin{frame}[fragile,label=notleM2]
\slidetitle{A family meeting}
\begin{block}{Random switch of couples}
\only<1>{
\begin{itemize}
\item Pick two couples [among the 20 couples] at random with probabilities proportional
to the number of other couples they have not seen\\
{\verb+prob=apply(meet(T)==0,1,sum)+}
\item switch their respective position during one of the 6 courses
\item accept the switch with Metropolis--Hsatings probability\\
{\verb#log(runif(1))<(penalty(old)-penalty(new))/gamma#}
\end{itemize}
}
\only<2>{
\begin{verbatim}
for (t in 1:N){
prop=sample(1:20,2,prob=apply(meet(T)==0,1,sum))
cour=sample(1:6,1)
Tp=T
Tp[prop[1],cour]=T[prop[2],cour]
Tp[prop[2],cour]=T[prop[1],cour]
penatp=penalty(meet(Tp))
if (log(runif(1))<(penat-penatp)/gamma){
T=Tp
penat=penatp}
}
\end{verbatim}
}
\end{block}
\end{frame}

since I was getting error messages of the form

[86] (./simulation.38.vrb) [87] (./simulation.39.vrb
!Illegal parameter number in definition of \[email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */
l
l.12 }
?


Using two frames in a row instead of the “only<2>” version  did not help…

So I eventually inserted another “normal” slide in-between:

\begin{frame}[fragile,label=notleM2]
\slidetitle{A family meeting}
\begin{block}{Random switch of couples}
\begin{itemize}
\item Pick two couples [among the 20 couples] at random with probabilities proportional
to the number of other couples they have not seen\\
{\verb+prob=apply(meet(T)==0,1,sum)+}
\item switch their respective position during one of the 6 courses
\item accept the switch with Metropolis--Hsatings probability\\
{\verb#log(runif(1))<(penalty(old)-penalty(new))/gamma#}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}
\slidetitle{A family meeting}
\begin{block}{Random switch of couples}
For instance, propose to replace
$$S = \left( \begin{matrix} 1 &1 &3 &2 &4 &2\\ 2 &1 &3 &3 &2 &4\\ \vdots&\vdots &\vdots &\vdots &\vdots &\vdots \\ 3 &2 &2 &2 &4 &4\\ \end{matrix}\right) \quad\text{with}\quad S^\prime = \left( \begin{matrix} 1 &1 &3 &2 &\RedOrange{2} &2\\ 2 &1 &3 &3 &\RedOrange{4} &4\\ \vdots&\vdots &\vdots &\vdots &\vdots &\vdots \\ 3 &2 &2 &2 &4 &4\\ \end{matrix}\right)$$
\end{block}
\end{frame}
\begin{frame}[fragile,label=notleM2bis]
\slidetitle{A family meeting}
\begin{block}{Random switch of couples}
\begin{verbatim}
for (t in 1:N){
prop=sample(1:20,2,prob=apply(meet(T)==0,1,sum))
cour=sample(1:6,1)
Tp=T
Tp[prop[1],cour]=T[prop[2],cour]
Tp[prop[2],cour]=T[prop[1],cour]
penatp=penalty(meet(Tp))
if (log(runif(1))<(penat-penatp)/gamma){
T=Tp
penat=penatp}
}
\end{verbatim}
\end{block}
\end{frame}

and it worked… Fairly pedestrian, but then… (For whatever reason, I could not use the “sourcecode” presentation with LaTeX code because “>” and “<” were turned into their HTML version.)

Filed under: R, Statistics, University life Tagged: Beamer, LaTeX, Le Monde, mathematical puzzle, sourcecode, verbatim, WordPress

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: , , , , , , , , ,