Re: [R] inserting R code in a latex document

From: Jan T. Kim <jtk_at_cmp.uea.ac.uk>
Date: Wed 11 May 2005 - 20:46:20 EST

On Wed, May 11, 2005 at 10:09:07AM +0200, Uwe Ligges wrote:
> Thibaut Jombart wrote:
>
> >Hello,
> >
> >I'm trying to insert R source code (functions) in an appendix of a latex
> >document. I guess the easiest way to do so is to use the package Sweaved
> >(file : Sweaved.sty) provided with the latest R version. Latex succeeds

> >in loading the package, but my problem comes from the use of this very
> >package. I tried to use the 'Schunk' environment, but '#' characters
> >generate error (my R functions are annotated, and I want to keep those
> >annotations). Under the 'Sinput' environment, no error is generated but
> >the result is simply not different from what would be obtained under
> >'verbatim' environment : R code lines are cut as they don't fit in the
> >page width.
> >
> >I tried to find answers in the latest Sweaved User Manual,
> >unsuccessfully. I'm a recent latex user and I doubt I can quickly find a
> >solution by myself.
>
>
> I think we are talking about "Sweave" in package "utils"?
> If so, you will find that you have to re-read the manual, because you
> don't need to specify 'Schunk'/'Sinput' yourself: Sweave does it for
> you. Just write the code in code chunks such as

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

> <<>>=
> code
> # comment
> @

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

-- 
 +- Jan T. Kim -------------------------------------------------------+
 |    *NEW*    email: jtk@cmp.uea.ac.uk                               |
 |    *NEW*    WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed May 11 19:58:28 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:41 EST