## \Sexpr{highlight(...)}

Showing 1-6 of 6 messages
 \Sexpr{highlight(...)} baptiste 4/21/12 11:30 PM Dear list,I'm coming back to knitr after some time not using it and I'm a bit lost in the wealth of possible options and other hooks! Sorry if I overlooked something,given the fast-pace development of knitr, I may well have missed a discussion in the archives or an option in the website. Instead of quoting verbatim inline R code  as in "Let's have a look at the \texttt{read.table()} function", I'd like to use the same syntax highlighting as in the code chunks. I tried redefining \Sexpr{} to make use of the highlight package but did not succeed. Is there a built-in environment/trick for this?Thanks,baptiste Re: [knitr] \Sexpr{highlight(...)} Yihui Xie 4/22/12 7:02 AM Hi baptiste,This is probably not as obvious as other hooks, since I did not exportthe function to highlight source code. Anyway, it is certainlypossible because the inline hook has access to the code in \Sexpr{}.Here is a quick experiment (note I quoted read.table() so that it is acharacter string otherwise it will be an R expression to be evaluated;you may need to try() highlighting because not all results are valid Rcode):\documentclass{article}<>=knit_hooks$set(inline = function(x) { if (is.numeric(x)) return(knitr:::format_sci(x, 'latex')) x = as.character(x) knitr:::hilight_source(x, 'latex', list(prompt=FALSE, size='normalsize'))})@\begin{document}the value of$\pi$is \Sexpr{pi}, and the function to read a table is\Sexpr{'read.table()'}.<>=rnorm(10)@\end{document}Regards,Yihui--Yihui Xie Phone: 515-294-2465 Web: http://yihui.nameDepartment of Statistics, Iowa State University2215 Snedecor Hall, Ames, IA Re: [knitr] \Sexpr{highlight(...)} baptiste auguie 4/22/12 1:57 PM Perfect, thanks a lot!baptiste Re: \Sexpr{highlight(...)} Weicheng Zhu 4/23/12 12:41 AM I'm also curious about this problem, but I'm not used to use Rnw files, I'd like to use tex file directly, would you please tell me how to use inline syntax highlight in tex files? Thanks in advance:) > Yihui Xie > Phone: 515-294-2465 Web:http://yihui.name > Department of Statistics, Iowa State University > 2215 Snedecor Hall, Ames, IA > > On Sun, Apr 22, 2012 at 1:30 AM, baptiste > > > > > > > Re: [knitr] Re: \Sexpr{highlight(...)} Yihui Xie 4/23/12 9:07 AM What do you mean by "use tex file directly"? You mean your tex filedoes not contain R code, and you are asking a pure LaTeX question? Ifthis is the case, I do not know. I think it is too laborious to typethe highlighting commands by hand like\ttfamily\noindent\hlfunctioncall{read.table}\hlkeyword{(}\hlkeyword{)}\mbox{}\normalfont.Why you do not use Rnw files?Regards,Yihui--Yihui Xie Phone: 515-294-2465 Web: http://yihui.nameDepartment of Statistics, Iowa State University2215 Snedecor Hall, Ames, IAOn Mon, Apr 23, 2012 at 2:41 AM, Weicheng Zhu wrote:> I'm also curious about this problem, but I'm not used to use Rnw> files,> I'd like to use tex file directly, would you please tell me how to> use> inline syntax highlight in tex files?> Thanks in advance:)> Re: \Sexpr{highlight(...)} Yihui Xie 3/3/13 10:41 AM Hi, There has been an internal change since then. Now you can do it in an easier way: \documentclass{article} <>= knit_hooks$set(inline = function(x) {   if (is.numeric(x)) return(knitr:::format_sci(x, 'latex'))   knitr:::hi_latex(x) }) @ \begin{document} the value of $\pi$ is \Sexpr{pi}, and the function to read a table is \Sexpr{'read.table()'}. <>= rnorm(10) @ \end{document} Regards, Yihui -- Yihui Xie Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Sun, Mar 3, 2013 at 11:33 AM, António Miguel de Jesus Domingues wrote: > Hi Yihui, > > Being a very new user of Knitr I am trying to replicate some examples, > for instance the one that you suggested to Baptiste. HoweverI get an > error: > > Quitting from lines 8-13 (test.Rnw) > Error in (format %in% c("latex", "html")) && options\$highlight : >   invalid 'y' type in 'x && y' > Calls: knit ... call_inline -> in_dir -> inline_exec -> hook -> > > > i am just copy.-pasing it. Is there something I am doing wrong? > (RStudio, Ubuntu 12.04 - if that makes a difference) > > Cheers, > António