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

10 views
Skip to first unread message

Peter Dunn

unread,
Jan 28, 2009, 1:41:43 AM1/28/09
to R Help
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@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: pdu...@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.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Mark Wardle

unread,
Jan 28, 2009, 2:46:59 AM1/28/09
to Peter Dunn, R Help
2009/1/28 Peter Dunn <PDu...@usc.edu.au>:

> 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:
> [...] *snip*

Try this:

\usepackage{listings}

\lstset{%
basicstyle=\scriptsize,
breaklines=true,
frame=single,
literate=
{<-}{$\leftarrow$}{2}
}

\renewcommand\lstlistlistingname{List of listings}


Have a look at the listings package... You can see I'm making the font
size smaller, giving all code a frame, and converting <- into a proper
left arrow (just for R!)

listings is very capable.

http://www.ctan.org/tex-archive/macros/latex/contrib/listings/
http://www.ctan.org/get/macros/latex/contrib/listings/listings.pdf

bw

Mark
--
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK

JLu...@ria.buffalo.edu

unread,
Jan 28, 2009, 10:43:16 AM1/28/09
to Peter Dunn, R Help, r-help-...@r-project.org
LaTeX offers a verbatim environment.

\begin{verbatim}
This is maintained verbatim, Latex commands and environments are typeset
as written without any processing.
\end{verbatim}

Be sure to use the package verbatim.
---Joe

"Peter Dunn" <PDu...@usc.edu.au>
Sent by: r-help-...@r-project.org
01/28/2009 01:41 AM

To
"R Help" <r-h...@r-project.org>
cc

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


Hi all

So two questions:

Thanks again.

P.


CRICOS Provider Number: 01595D

--
This message has been scanned for viruses and\ dangerous...{{dropped:15}}

Reply all
Reply to author
Forward
0 new messages