From gray.calhoun at gmail.com  Sat Nov 26 00:12:03 2011
From: gray.calhoun at gmail.com (Gray Calhoun)
Date: Fri, 25 Nov 2011 18:12:03 -0500
Subject: [Rd] Problem with & question about \preformatted in .Rd
Message-ID: <CAGLwjoxLJdLDL+ax7sC5_yoCdbvtrJHwsmM65zNQ9=Aty8UXHw@mail.gmail.com>

Hi,

The "\preformatted" environment in Rd files doesn't seem to escape
long sequences of backslashes properly when converted to pdf (LaTeX)
documentation.  I'm running R version 2.14 (from subversion, revision
57751) on Linux (RHEL).  Here's an example from the command line:

echo "\title{test}\name{test}\section{problems}{\preformatted{print('\\\\\\\\begin\\\\\{block\\\\\}')}}"
> temp.Rd
R CMD Rdconv --type=latex temp.Rd

which outputs:

\inputencoding{utf8}
\HeaderA{test}{test}{test}
%
\begin{Section}{problems}
\begin{alltt}print('\\\\begin\\{block\\}')\end{alltt}
\end{Section}

and the double backslashes become newlines when this text is embedded
in a complete document and converted to pdf.  I expected from the
documentation to get this:

\inputencoding{utf8}
\HeaderA{test}{test}{test}
%
\begin{Section}{problems}
\begin{alltt}print('\bsl{}\bsl{}\bsl{}\begin\bsl{}\bsl{}\{block\bsl{}\bsl{}\}')\end{alltt}
\end{Section}

The problem seems to come from tools:::Rd2latex as this R code indicates:

library(tools)
tools:::Rd2latex(textConnection("\\title{test}\\name{test}
\\section{problems}{
\\preformatted{
print('\\\\\\\\\\\\\\\\begin\\\\\\\\\\{block\\\\\\\\\\}')
}}"))

(the output is the same as from the command line above)

I can get the behavior that I want by removing a few lines from the
relevant part of tools:::Rd2latex and I provide a patch below, but I
assume that the code that I removed was put there for a good reason so
this isn't a good fix.  make check-devel didn't find any errors,
though.

I'm happy to fill out a bug report for this if that's the next appropriate step.

A question: why is there special code to handle "\var" inside
\preformatted environments?  The documentation reads:

"\preformatted{text}
    Indicate text that is a literal example of a piece of a program.
Text is displayed using typewriter font if possible. Formatting, e.g.
line breaks, is preserved.

    Due to limitations in LaTeX as of this writing, this macro may not
be nested within other markup macros other than \dQuote and \sQuote,
as errors or bad formatting may result."

which doesn't indicate that \var should be handled any differently
than any other macro, but the code makes me think that R is trying to
pass the macro through to LaTeX.

Thanks!

--Gray

-- 
Gray Calhoun

Assistant Professor of Economics, Iowa State University
http://www.econ.iastate.edu/~gcalhoun

patch:

Index: src/library/tools/R/Rd2latex.R
===================================================================
--- src/library/tools/R/Rd2latex.R      (revision 57751)
+++ src/library/tools/R/Rd2latex.R      (working copy)
@@ -163,10 +163,7 @@
                BSL = '@BSL@';
                BSL2 = '@BSLBSL@';
                #x <- fsub("\\dots", "...", x)
-               ## escape any odd \, e.g. \n
-               x <- fsub("\\\\", BSL, x) # change even ones
                x <- fsub("\\", BSL2, x)  # odd ones
-               x <- fsub(BSL, "\\\\", x) # change back
                x <- psub("(?<!\\\\)\\{", "\\\\{", x)
                x <- psub("(?<!\\\\)}", "\\\\}", x)
                x <- fsub(BSL2, "\\bsl{}", x)library(tools)



[Rd] Problem with & question about \preformatted in .Rd
Gray Calhoun