Skip to content

including r code in a latex file

7 messages · Brian Ripley, Göran Broström, Ben Bolker +3 more

#
Dear R people,

The header practically says it all. I was wanting to include r code in a
Latex file. Since R code using{ and }, which are interpreted by Latex as
control characters, I would expect it to get upset.

I believe that \{ would probably print as {, but I hoping I don't have to
go through the code and add lots of \. I would rather use some global
commannd, along the lines of \treatastext{R code}.

Is there something like this? I'm not that familiar with Latex.

                                  Thanks, Faheem.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 1 May 2000, Faheem Mitha wrote:

            
Use a verbatim environment (or \verb for inline text).  Lots of other
things like ^ and ~ will cause problems in LaTeX too.  Alternatively,
look hard at how the Rdconv does it using Rd.sty.  (V&R has a custom-made
verbatim-like environment with a custom virtual tt font, for example.)

Alternatively, investigate one of the many program-setting packages you can
find on CTAN, or the S.sty on statlib (used for the White Book).
#
On Mon, 1 May 2000, Faheem Mitha wrote:

            
You could try \begin{verbatim}...R-code ... \end{verbatim}
#
\begin{verbatim}
R code
\end{verbatim}

Or, to do inline R code: you can put R expressions such as
\verb| x <- function(y)y^2| in your code.

By default, all the "verbatim" stuff comes out in typewriter
(Courier/cmtt) font.
On Mon, 1 May 2000, Faheem Mitha wrote:

            

  
    
#
One way, I don't know about the efficiency though, that I use is:
\begin{display}
\begin{verbatim}
blah blah blah and more R code blah...
\end{display}
\end{verbatim}
Faheem Mitha wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Thanks for the flood of responses!       Faheem.


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 1 May 2000, Faheem Mitha wrote:

            
While you could use verbatim, as others have suggested, the lgrind
definitions that I posted here the other day has been written with that in
mind to produce nice R-code program listings for inclusion in LaTeX
documents. 

lgrind is a pretty standard program, you may find it on you system. I
haven't myself been able to make it work, for some unknown reason, but if
you take the following code:
R:\
        :pb=^\d?\p\d<-\dfunction\(\a\):\
        :bb={:be=}:cb=\d#:ce=$sb=":se=\e":lb=':le=\e':id=.:\
        :zb=@:ze=@:tb=%%:te=%%:mb=%\$:me=\$%:vb=%\|:ve=\|%:\
        :kw=while for in repeat if else switch break next\
        return stop warning function:
save it to a file you call e.g. ~/tex/lgrindefs
then running
lgrind -i -d ~/tex/lgrindefs mycode.R > mycode.tex
should produce a nice LaTeXed file. I think... You need to include the
lgrind style file in you document, as well as use a macro to include it.
See the lgrind(1) man page for details

Best,

Kjetil