\Sexpr{highlight(...)}

565 views
Skip to first unread message

baptiste

unread,
Apr 22, 2012, 2:30:03 AM4/22/12
to kn...@googlegroups.com
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

Yihui Xie

unread,
Apr 22, 2012, 10:02:38 AM4/22/12
to baptiste, kn...@googlegroups.com
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

baptiste auguie

unread,
Apr 22, 2012, 4:57:04 PM4/22/12
to Yihui Xie, kn...@googlegroups.com
Perfect, thanks a lot!

baptiste

Weicheng Zhu

unread,
Apr 23, 2012, 3:41:22 AM4/23/12
to knitr
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>

Yihui Xie

unread,
Apr 23, 2012, 12:07:04 PM4/23/12
to Weicheng Zhu, knitr
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

Yihui Xie

unread,
Mar 3, 2013, 1:41:29 PM3/3/13
to António Miguel de Jesus Domingues, knitr
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

iJatrat

unread,
Mar 26, 2017, 11:41:11 AM3/26/17
to knitr, amjdom...@gmail.com
Hey Yihui,

Thanks for this solution! I tried to implement this. However, whenever I now use the \Sexpr{} expression (be it to render R-Variables or simply highlight R-Code in-line), I always receive following error:
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) 

Any idea what could be going wrong?

iJatrat

unread,
Mar 26, 2017, 11:44:29 AM3/26/17
to knitr, amjdom...@gmail.com
Solved it: It should have been highr:::hi_latex(x) instead of knitr:::hi_latex(x)  :)

Thanks!


Am Sonntag, 3. März 2013 19:41:29 UTC+1 schrieb Yihui Xie:
Reply all
Reply to author
Forward
0 new messages