4

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?

2
  • 1
    Use 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 Xie
    Apr 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"?
    – Heisenberg
    Apr 18, 2014 at 20:01

1 Answer 1

1

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')}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.