[R] OT: Adding verbatim R code text into LaTeX documents: texttt; verb or url?

Peter Dunn PDunn2 at usc.edu.au
Wed Jan 28 07:41:43 CET 2009


Hi all

I use Sweave extensively to mix R and LaTeX, and often have R code appearing in my LaTeX document.

Just a quick question then: What is the best way to add example of R commands into LaTeX in-line?  (That is, not using Sweave.)  For example, suppose I wish to place in my document this instruction:



...is done in R using the command  \verb|lm( y ~ var.one + var.two )| as follows:



I used  \verb  above, but I see three options:  \verb, \url (package url), or \texttt; there are probably others.

Here are my comments on these three:

- Using \texttt is OK, but it disappears my tildes and can hyphenate

- Using \verb is good, but it can hyphenate.

- Using \url is very good, but it:
* disappears my spaces; so for the above example, the spaces added for clarity are gone.
* Minor:  I like my verbatim text a little smaller (\small size), and change the font size for verbatim using  \def\verbatim at font{\small\ttfamily} but \url seems to ignore this and appears larger than if I used \text or \verb.

Also, using \url often adds line-breaks mid-variable at the dots (for example, splitting  var.one  to have "var." on one line, and "one" on the next). I'm not sure this is a problem or not; here it is just an observation.

Ideally, one would want a LaTeX function, say \rcode{}, that displayed in-text using non-proportional font, kept tildes, kept spacing, uses my verb-font changes, and broke at sensible places for R.  (I don't want much, do I?)

So two questions:

* What do other people do?  Maybe there is a solution I have over-looked.

* Is there an easy solution?  I suppose writing such a command in LaTeX is possible, but there is strong evidence to reject the hypothesis that I would be able to write one.  Maybe one of the above choices are easily adopted.

If no easy solutions exist or emerge, I'm happy to run with \url.

Thanks again.

P.

Peter Dunn
Biostatistician
School of Health and Sport Science
Faculty of Science, Health and Education
University of the Sunshine Coast
 
Tel: +61 7 5456 5085
Fax: +61 7 5430 2896
Email: pdunn2 at usc.edu.au
www.usc.edu.au


CRICOS Provider Number: 01595D

This communication is intended for the recipient only and should not be forwarded, distributed or otherwise read by others without express permission. The views expressed in this email are not necessarily those of the University of the Sunshine Coast.

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.




More information about the R-help mailing list