It is easy enough to do something like \Sexpr{1 + 1}
. But how can I do \Sexpr{mymatrix}
with mymatrix
being a matrix object in R workspace? More generally, how can I print object that is more complex than just a number using inline code chunk?
-
1Use string processing functions to turn your complex objects into a character string. BTW, this is not a question specific to LaTeX, so someone may have to move it to StackOverflow.– Yihui XieApr 17, 2014 at 6:04
-
@Yihui I am not sure I understand how to turn a matrix into a string while preserving its block form. Could you tell me the name of the "string processing function"?– HeisenbergApr 18, 2014 at 20:01
1 Answer
To answer your question in the comment: there is no single name. I said functions, and I mean string manipulation functions like paste()
, substr()
, and so on. You may take a look at the packages for generating tables, and try capture.output()
, then paste()
. But I doubt if it is worth it -- why not using a code chunk? For example,
<<results='asis'>>=
library(xtable)
xtable(mymatrix)
@
<<results='asis'>>=
library(knitr)
kable(mymatrix)
@
You can certainly make it in the inline code if you do not mind the long R expression, e.g. \Sexpr{paste(capture.output(kable(mymatrix)), collapse='\n')}
.