Getting Sweave lines of code inside a frame in a frame?

I would like to make a piece of R code (in Sweave) printed inside the frame in the received pdf format.

Is there a simple solution for this?

+4
source share
2 answers

Short answer: yes, there is an easy way. Just add the following lines or something like them to the preamble of your Sweave document:

\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em, frame=single} \DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em, frame=single} 

This works because the appearance of code snippets (and output) is controlled by the definition of Sinput and Soutput . These are both Verbatim environments that are provided by the LaTeX fancyvrb . ( Click here for a 73-page pdf describing the numerous options provided by fancyvrb ).

A quick look in the Sweave.sty file shows the default definition for these two environments:

 \DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl} \DefineVerbatimEnvironment{Soutput}{Verbatim}{} \DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl} 

To change these definitions, simply add \DefineVerbatimEnvironment own developer \DefineVerbatimEnvironment : (a) at the end of the Sweave.sty file; or (b) at the beginning of your *.Snw document.


Finally, here is an example to show how it looks in practice:

 \documentclass[a4paper]{article} \usepackage{Sweave} \DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em, frame=single} \DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em, frame=single} \title{Sweave with boxes} \begin{document} \maketitle <<echo=FALSE>>= options(width=60) @ Here is an example of a code chunk followed by an output chunk, both enclosed in boxes. <<>>= print(rnorm(99)) @ \end{document} 

enter image description here

+7
source

knitr , knitr 's successor, by default displays all echo R code in the fields, and also formats it into fields. Other nice features include syntax coloring and PGF integration.

Sweave code of medium complexity requires only minor if any adaptations for working with knitr .

+2
source

Source: https://habr.com/ru/post/1391549/


All Articles