Skip to content

inserting R code in a latex document

6 messages · Thibaut Jombart, Uwe Ligges, Jan T. Kim +3 more

#
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.

Thanks,
Thibaut Jombart
#
Thibaut Jombart wrote:

            
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

<<>>=
code
# comment
@
LaTeX code

and apply Sweave on your noweb document, the resulting LaTeX document 
has appropriate environments.


Uwe Ligges
#
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
#
Thibaut Jombart wrote:

            
I have had very good results with the LaTeX package "listings".  It is 
easy to use, and it does the job.
#
Le 11.05.2005 14:00, Michael Prager a ??crit :
There is also highlight : http://www.andre-simon.de/
1 day later
#
I have been trying to use the listings package in LaTeX to do this but 
have not been fully happy with the result.  Here is what I'm trying:

\lstloadlanguages{R}
\lstset{language=R,basicstyle=\smaller[2],commentstyle=\rmfamily\smaller,
  showstringspaces=false,%
  xleftmargin=4ex,literate={<-}{{$\leftarrow$}}1 {~}{{$\sim$}}1}
\newcommand{\co}[1]{{\lstinline|#1|}} # for in-line S phrases
\newcommand{\Co}[1]{{\lstinline|#1|}}

\begin{lstlisting}
... S code ...
\end{lstlisting}

If anyone has a better setup please let me know.  I'm trying to set 
comments in a smaller font and in roman style, and am changing <- to 
left arrows and ~ to $\sim$.  This also requires the LaTeX relsize package.

Thanks