\Sexpr{highlight(...)}

已查看 567 次
跳至第一个未读帖子

baptiste

未读,
2012年4月22日 02:30:032012/4/22
收件人 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

未读,
2012年4月22日 10:02:382012/4/22
收件人 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

未读,
2012年4月22日 16:57:042012/4/22
收件人 Yihui Xie、kn...@googlegroups.com
Perfect, thanks a lot!

baptiste

Weicheng Zhu

未读,
2012年4月23日 03:41:222012/4/23
收件人 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

未读,
2012年4月23日 12:07:042012/4/23
收件人 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

未读,
2013年3月3日 13:41:292013/3/3
收件人 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

未读,
2017年3月26日 11:41:112017/3/26
收件人 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

未读,
2017年3月26日 11:44:292017/3/26
收件人 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:
回复全部
回复作者
转发
0 个新帖子