Matrix Multiplication in a Knitr Block Piece

When I include a string of string code with \Sexpr{}, the symbol of the matrix multiplication symbol is %*%interpreted as a Latex comment. How to get around this?

+4
source share
1 answer

This seems like a reasonable problem, but it does not happen to me (with R-devel / 3.1.0, knitr 1.5). In addition, it logically seems that this should not happen, since the contents of the code fragments are evaluated before LaTeX sees the file. Reproduction example please?

(As shown below, you may encounter difficulties if you try hard enough, but as a rule, I think you should not do this.)

If texmult.Rnw:

\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}

library(knitr); knit2pdf("texmult.Rnw")

enter image description here

+4

Source: https://habr.com/ru/post/1534675/


All Articles