Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr: Display tilde ~ in beamer overlay

This might be an edge case, but I'm trying to create a beamer presentation with knitr where I want to display a code chunk using different formulas as arguments for a function. I found that when using overlays, the tilde in the code chunks disappear. Is there a way to get them to display?

Here's a minimal reproducible example:

\documentclass{beamer}
\begin{document}


\begin{frame}[fragile]
\frametitle{Slide with overlay}
\only<1>{
<<notilde, eval = FALSE>>=
myfunction(data, formula = ~ x)
@
}
\only<2>{
<<notilde2, eval = FALSE>>=
myfunction(data, formula = y ~ x)
@
}
\end{frame}

\begin{frame}[fragile]
\frametitle{Slide without overlay}
<<tilde, eval = FALSE>>=
myfunction(data, formula = ~ x)
@
\end{frame}

\end{document}

The first frame of the presentation looks like this:

slide with overlay

Any help is appreciated.

EDIT:

The slide without overlay looks like this:

slide without overlay

To clarify, the syntax of <<...>> and @ is specific to the R package knitr. Anything between these symbols are interpreted by knitr as R code chunks and converted to latex to include syntax highlighting.

like image 561
ZNK Avatar asked Nov 10 '22 03:11

ZNK


1 Answers

You can replace the ~ with the math mode $\sim$

If not in a special environment like verbatim, the tilde in latex is a protected space, i.e. a space at which no line break occurs.

It is not clear from your question: is it working for your second frame, without overlay? I don't know part of your syntax: is the << ... >>= ... @ equivalent to a verbatim or code environmet? It looks a bit, as your text is set in a fixed space font and has syntax highlihting.

like image 173
gschenk Avatar answered Dec 29 '22 22:12

gschenk