Question

When I include an inline code trunk using \Sexpr{}, the matrix multiplication symbole %*% is interpreted as Latex commenting. How to get around this?

Was it helpful?

Solution

This seems like a reasonable concern, but it doesn't happen to me (with R-devel/3.1.0, knitr 1.5). Also, logically it seems as though it shouldn't happen since the contents of code chunks are evaluated before LaTeX sees the file. Reproducible example please?

(As shown below, you can get into trouble if you try hard enough, but generally I think you shouldn't.)

If texmult.Rnw is this:

\documentclass{article}
\pagestyle{empty}
\begin{document}

hello

<<>>=
m <- matrix(1:4,nrow=1)
x <- 1:4
@

The result is \Sexpr{m %*% x}

This is a bad thing: \Sexpr{"%*%"} and then more stuff

\end{document}

Then library(knitr); knit2pdf("texmult.Rnw") gives me

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top