[R] knitr customization

Yihui Xie xie at yihui.name
Fri May 25 22:23:07 CEST 2012


On Fri, May 25, 2012 at 2:30 PM, Giovanni Petris <gpetris at uark.edu> wrote:
>
> I am trying to transition from Sweave to knitr, but there are a few
> things about customization of the appearence of R input and output that
> I did not get yet. Maybe somebody on the list can help me.
>
> In my Sweave presentations I used a slanted font for the R input and a
> normal font for the output, both in a small font. I also indented
> everything by an extra 2em. All this was achieved by the following
> instructions in the .Rnw file:
>
> \DefineVerbatimEnvironment{Sinput}{Verbatim}{xleftmargin=2em,
>  fontsize=\footnotesize, fontshape=sl}
> \DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
>  fontsize=\footnotesize}
>
> In order to get a similar result (with a tiny size instead of a footnote
> size, but that is not the point) with knitr I included in the .Rnw file
> the lines
>
> \ifdefined\knitrout
>  \renewenvironment{knitrout}{\begin{tiny}\slshape}{\end{tiny}}
> \else
> \fi
>
> What I get is something like this:
>
> http://definetti.uark.edu/~gpetris/knitr-output.pdf
>
> The problem is that the prompt sign '>' did not change either size or
> shape. So I am probably not using the correct approach. How should I be
> doing?

You need to set the chunk option 'size', e.g.

opts_chunk$set(size = 'tiny')

This is equivalent to \SweaveOpts{size='tiny'}.

>
> Also, what do I have customize, and how, in order to get the extra left
> margin?

See below.

>
> Final question: how can I get the continuation sign '+'? (This is for an
> introductory presentation in which I want to show people exactly what
> they are going to see on their screen.)

Use opts_chunk$set(prompt=TRUE), but see FAQ6 for my gripes:
https://github.com/yihui/knitr/blob/master/FAQ.md

Compared to the huge pain of being unable to copy & paste the code in
the PDF, missing > and + is not a big deal in my eyes. They might even
be misleading -- people may think > and + are part of the source code.

>
> Oops, one more... How can I change the spacing between regular text and
> R code (input/output)? With Sweave I would do
>
> \fvset{listparameters={\setlength{\topsep}{0pt}}}
> \renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}}
>
>
> Sorry for the many questions and thank you in advance for any help and
> insight you can give me.
>
> Best,
> Giovanni
>
>
> --
>
> Giovanni Petris  <GPetris at uark.edu>
> Associate Professor
> Department of Mathematical Sciences
> University of Arkansas - Fayetteville, AR 72701
> Ph: (479) 575-6324, 575-8630 (fax)
> http://definetti.uark.edu/~gpetris/
>

It seems you miss the Sweave styles, and it requires trivial efforts
to switch back; just use render_sweave() in your first chunk, and you
will get the familiar look.

I cooked up a minimal example for you here:
https://gist.github.com/2790354#file_petris_style.rnw

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



More information about the R-help mailing list