Skip to content
Prev 69512 / 398525 Next

inserting R code in a latex document

On Wed, May 11, 2005 at 10:09:07AM +0200, Uwe Ligges wrote:
My understanding was that Thibaut tries to use the Sweave style
(\usepackage{Sweave}) in a standard LaTeX document, so my comment
here is focused on LaTeX rather than R.

In this case, the Schunk environment won't be of any use because as
provided by Sweave.sty, it doesn't do anything:

    \newenvironment{Schunk}{}{}

Therefore, LaTeX will interpret the hash as a special character
inside Schunk environments; the hash has to be wrapped into a
verbatim-like environment to be legal as a standard character.

Running Sweave on an rnw file results in the code chunks, such as
to be wrapped in

    \begin{Schunk}
    \begin{Sinput}
    code
    # coment
    \end{Sinput}
    \end{Schunk}

As the Schunk environment does nothing, I have used it as a handle
for adding effects, e.g. if your lines are just a bit too long, you
may be able to fix that by adding

    \renewenvironment{Schunk}{\footnotesize}{}

to the preamble of your LaTeX file. (This can also be useful for rnw
files too.)

If your code lines are way too long and won't even fit with \tiny,
I'm afraid that there is no automatic way to resolve this. With very
few exceptions, automatic reformatting of code does not result in
readable results. You'll have to manually arrange your code such that
line length does not exceed a reasonable maximum (which may generally
improve readability and maintainability of your code).

Finally, even as a LaTeX beginner, don't be afraid to look into style
files and the like. Sweave.sty is really a quite short and understandable
one. A great thing about LaTeX is that you can always check the code,
and while such lessons may not always be what you're looking for e.g.
when under time pressure to get something finished, I've found that (as
very often), I've found that reading LaTeX code has enabled me to use
LaTeX much more effectively over time.

Best regards, Jan