2

Here is my code in a Rnw file :

\Sexpr{length(which(x %in% c('a', 'b')))}

As % is the syntax for comments, this code doesn't work.
How to use %in% in Sexpr in R/Sweave ?

Marion H.
  • 61
  • 6

1 Answers1

1

I cannot mark questions as duplicate but I got the answer from here and by Yihui: \Sexpr{} special LaTeX characters ($, &, %, # etc.) in .Rnw-file

The following code will allow special latex characters to be used inline in \Sexpr so you can use %in%. It's a more general question than the one you posted.

hook_inline = knit_hooks$get('inline')
knit_hooks$set(inline = function(x) {
  if (is.character(x)) x = knitr:::escape_latex(x)
  hook_inline(x)
})
Sahir Moosvi
  • 549
  • 2
  • 21