Royun Royun - 25 days ago 5
LaTeX Question

How to insert LaTeX math expressions in the comments of R chunks in Sweave?

The following codes are for the Sweave/Latex beamer:

\begin{frame}[fragile]
\frametitle{Function Basics}
\begin{block}{Elementary Functions}
<<>>=
pi # \texttt{pi} is a predefined const.
sin(pi) # \texttt{sin(pi)}$\neq$0, due to computing error.
sinpi(1) # Instead, we use \texttt{sinpi(x=1)} to get around.
exp(1)
log(10)
@
\end{block}
\end{frame}


My question:

in the above three # comments, after Sweave compiling, \texttt{}, $\neq$ still exist. Sweave does not recognize Latex code in the R chunk.

On the other hand, it seems that Latex does not identify any codes (R or Latex) in the R chunk as well.

So, how to show \texttt{}, $\neq$ in Latex math format in this circumstance? Thank you in advance!

Answer Source

This is expected behaviour. I would suggest a workaround:

\begin{frame}
\begin{tabular}{ll}
  > pi&\# \texttt{pi} is a predefined const.\\
  \Sexpr{pi}&\\
  > sin(pi)&\# \texttt{sin(pi)}$\neq$0, due to computing error.\\
  \Sexpr{sin(pi)}&\\
  > sinpi(1)&\# Instead, we use \texttt{sinpi(x=1)} to get around.\\
  \Sexpr{sinpi(1)}&\\
\end{tabular}
\end{frame}

yields

enter image description here