Reading ?RWeaveLatex more carefully, I worked out that the outcome I want could 
be achieved without patching R, by having this towards the beginning:

<<custom=hide>>=
  cairo <- function(name, width, height, ...)
  grDevices::cairo_pdf(file = paste(name, "pdf", sep = "."),
                       width = width, height = height)
@

Then declaring grdevice= like this:

<<another1,fig=TRUE,pdf=FALSE,grdevice=cairo,eval=TRUE,width=6,height=3>>=
grid.text("\u4F60\u597D", y=2/3, gp=gpar(fontfamily="CNS1"))
@

So all is well. is there any chance of either updating ?RWeaveLatex with this 
actual use-case example, or make it easier with the attached patch (and updated 
version of what I did, just adding the corresponding documentation)? I thought 
there might be a a use for cairo_ps() as well, but then most people who can do 
latex could do pdflatex, especially those who has cairo installed.

FWIW, besides Chinese, I am doing Tibetan and Arabic as well - needed/wanted 
those two for Sichuan (south-western China) and Ningxia (northern western, just 
south of Mongolia).

--- On Sun, 23/10/11, Hin-Tak Leung <ht...@users.sourceforge.net> wrote:

> --- On Sat, 22/10/11, Prof Brian
> Ripley <rip...@stats.ox.ac.uk>
> wrote:
> 
> > On Sat, 22 Oct 2011, Duncan Murdoch
> > wrote:
> > 
> > > On 11-10-21 8:57 PM, Hin-Tak Leung wrote:
> > >> I have had some fun in the last few days
> trying to
> > put together an annotated map of China with R and
> some
> > public GIS data:
> > >> 
> > >> http://sourceforge.net/projects/outmodedbonsai/files/snpMatrix%20next/1.17.7.11/China_Choropleth_Maps.pdf/download
> > >> 
> > >> It is done, and rather nice... there are a
> few
> > issues:
> > >> 
> > >> - the default pdf() device cannot do CJK
> with
> > embedded fonts - and cairo_pdf() is not hooked up to
> Sweave
> > yet. I have had a quick look, and it does not look
> too
> > complicated, other than the fact that cairo_pdf() is
> > mutually exclusive with pdf(); and the jpeg/png are
> new to
> > 2.13 so it is probably just nobody has gotten round to
> it.
> > (and cairo_pdf() also behaves differently with the
> onefile
> > option). I guess I am almost saying I might get my
> hand
> > dirty or help somebody else who wants to. So it is
> probably
> > best to start some discussion.
> > > 
> > > It is not hard to write your own device
> functions,
> > especially in 2.14.0.  See rgl::rgl.Sweave for an
> > example.
> > 
> > And guess what the example in the Sweave manual
> uses? 
> > cairo_pdf() ...
> 
> I don't want a new device - I just wanted cairo_pdf() to be
> hooked up to Sweave as an alternative to the default pdf().
> See my later post with the patch against  R trunk and
> the worked examples.
> 
> It looks like cairo_pdf() is just using my current
> fontconfig settings.
> 
>
diff --git a/src/library/utils/R/SweaveDrivers.R b/src/library/utils/R/SweaveDrivers.R
index edc0a84..32ac493 100644
--- a/src/library/utils/R/SweaveDrivers.R
+++ b/src/library/utils/R/SweaveDrivers.R
@@ -60,7 +60,7 @@ RweaveLatexSetup <-
 
     options <- list(prefix = TRUE, prefix.string = prefix.string,
                     engine = "R", print = FALSE, eval = TRUE, fig = FALSE,
-                    pdf = TRUE, eps = FALSE, png = FALSE, jpeg = FALSE,
+                    pdf = TRUE, cairo = FALSE, eps = FALSE, png = FALSE, jpeg = FALSE,
                     grdevice = "", width = 6, height = 6, resolution = 300,
                     term = TRUE, echo = TRUE, keep.source = TRUE,
                     results = "verbatim",
@@ -98,6 +98,9 @@ makeRweaveLatexCodeRunner <- function(evalFunc = RweaveEvalWithOpt)
                            version = options$pdf.version,
                            encoding = options$pdf.encoding,
                            compress = options$pdf.compress)
+        cairo.Swd <- function(name, width, height, ...)
+            grDevices::cairo_pdf(file = paste(chunkprefix, "pdf", sep = "."),
+                           width = width, height = height)
         eps.Swd <- function(name, width, height, ...)
             grDevices::postscript(file = paste(name, "eps", sep = "."),
                                   width = width, height = height,
@@ -119,6 +122,10 @@ makeRweaveLatexCodeRunner <- function(evalFunc = RweaveEvalWithOpt)
                 devs <- c(devs, list(pdf.Swd))
                 devoffs <- c(devoffs, list(grDevices::dev.off))
             }
+            if (options$cairo) {
+                devs <- c(devs, list(cairo.Swd))
+                devoffs <- c(devoffs, list(grDevices::dev.off))
+            }
             if (options$eps) {
                 devs <- c(devs, list(eps.Swd))
                 devoffs <- c(devoffs, list(grDevices::dev.off))
@@ -151,6 +158,7 @@ makeRweaveLatexCodeRunner <- function(evalFunc = RweaveEvalWithOpt)
                 if (options$fig) {
                     if (options$eps) cat(" eps")
                     if (options$pdf) cat(" pdf")
+                    if (options$cairo) cat(" cairo")
                     if (options$png) cat(" png")
                     if (options$jpeg) cat(" jpeg")
                     if (!is.null(options$grdevice)) cat("", options$grdevice)
diff --git a/src/library/utils/man/RweaveLatex.Rd b/src/library/utils/man/RweaveLatex.Rd
index c460c40..39f19b7 100644
--- a/src/library/utils/man/RweaveLatex.Rd
+++ b/src/library/utils/man/RweaveLatex.Rd
@@ -191,6 +191,11 @@ RweaveLatexSetup(file, syntax, output = NULL, quiet = FALSE,
       \code{\link{pdf}} to set the version, encoding and compression (or
       not).  Defaults taken from \code{pdf.options()}.}
     
+    \item{cairo:}{logical (\code{FALSE}), indicating whether PDF figures
+      should be generated using \code{cairo_pdf()}. \code{cairo=TRUE}
+      should not be used together with \code{pdf=TRUE}. Does not use any
+      of the \code{pdf.options()}. Ignored if \code{fig = FALSE}.}
+
     \item{png:}{logical (\code{FALSE}), indicating whether PNG figures
       should be generated.  Ignored if \code{fig = FALSE}.  Only
       available in \R >= 2.13.0.}
______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to