\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 export
the function to highlight source code. Anyway, it is certainly
possible 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 a
character string otherwise it will be an R expression to be evaluated;
you may need to try() highlighting because not all results are valid R
code):

\documentclass{article}
<<setup, include=FALSE>>=
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()'}.

<<test2>>=
rnorm(10)
@
\end{document}


Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 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 <xieyi...@gmail.com>
> 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 file
does not contain R code, and you are asking a pure LaTeX question? If
this is the case, I do not know. I think it is too laborious to type
the 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 <xiey...@gmail.com>


Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Mon, Apr 23, 2012 at 2:41 AM, Weicheng Zhu <min...@gmail.com> 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}
<<setup, include=FALSE>>=
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()'}.

<<test2>>=
rnorm(10)
@
\end{document}

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
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
<amjdom...@gmail.com> 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 ->
> <Anonymous>
>
> 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