4

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

Heisenberg
  • 8,386
  • 12
  • 53
  • 102
  • Are you using LaTeX-style rather than Rnw-style syntax? See (linked) http://stackoverflow.com/questions/12746684/can-i-use-a-knitr-inline-expression-in-the-title-of-a-latex-document?rq=1 – Ben Bolker Apr 02 '14 at 15:44
  • so, since you've accepted my answer -- have you figured out what your actual problem was? – Ben Bolker Apr 02 '14 at 19:31
  • I didn't actually have a problem -- I was re-installing the entire tex-live so I couldn't test before posting the question. The problem is a concern I had in my mind that turned out not true. – Heisenberg Apr 02 '14 at 22:16
  • 1
    I don't want to be mean, but maybe you should test such surmises before posting them to StackOverflow ... – Ben Bolker Apr 02 '14 at 22:34
  • I agree. It was my fault completely. – Heisenberg Apr 02 '14 at 22:35

1 Answers1

4

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

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453