[R] using dvi with latex object: directory not correctly set, maybe due to error in shQuote()

Willner, Marco Marco.Willner at feri.de
Wed Dec 17 17:48:58 CET 2008


Dear Bernhard

You were right, pushing my knowledge about R once again to the next level! Sys.getenv("R_SHELL") indeed returns "". The fix you are proposing works for me. If I am right, your function dvi.latex2 makes use of shell rather than of sys in the original function dvi.latex and stores results in the specified for tempdir(). On my system the original function dvi.latex also works up to the point when I want to produce a ps file (see output below: dvi can't be opened). 

However the next problem is the output (either by opening the dvi file with yap or the ps file with ghostscript). The page size is not the one specified by the parameters width and height in dvi.latex/dvi.latex2. It looks like DIN A4. I apologize if, at this point, it becomes a problem caused by Miktex 2.7 and geometry package. Ultimately, I want to embed the pdf file into another file and I cannot use the large format.

Cheers
Marco




> tbl.loc   <- matrix(1:4, ncol=2)
> latex.obj <- latex(tbl.loc)
> dvi.obj   <- dvi(latex.obj)
Das System kann den angegebenen Pfad nicht finden.
This is pdfTeX, Version 3.141592-1.40.4 (MiKTeX 2.7 Beta 4)
entering extended mode
(C:/DOKUME~1/ferimawi/LOKALE~1/Temp/Rtmpr4CG3A/file33ea5db2.tex
LaTeX2e <2005/12/01>
Babel <v3.8g> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman, ngerman, french, loaded.
("C:\Programme\MiKTeX 2.7\tex\latex\base\report.cls"
Document Class: report 2005/09/16 v1.4f Standard LaTeX document class
("C:\Programme\MiKTeX 2.7\tex\latex\base\size10.clo"))
("C:\Programme\MiKTeX 2.7\tex\latex\geometry\geometry.sty"
("C:\Programme\MiKTeX 2.7\tex\latex\graphics\keyval.sty")
("C:\Programme\MiKTeX 2.7\tex\latex\geometry\geometry.cfg"))
No file file33ea5db2.aux.
[1] (file33ea5db2.aux) )
Output written on file33ea5db2.dvi (1 page, 300 bytes).
Transcript written on file33ea5db2.log.
> dvips(object=dvi.obj, "tst.ps")
This is dvips(k) 5.96 Copyright 2007 Radical Eye Software (www.radicaleye.com)
dvips: ! DVI file can't be opened.
> dvi.obj2 <- dvi.latex2(latex.obj)
This is pdfTeX, Version 3.141592-1.40.4 (MiKTeX 2.7 Beta 4)
entering extended mode
("H:/Asset Allocation/PROJECTS/MAM/latex/plot_tbl/file48cc23c9.tex"
LaTeX2e <2005/12/01>
Babel <v3.8g> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman, ngerman, french, loaded.
("C:\Programme\MiKTeX 2.7\tex\latex\base\report.cls"
Document Class: report 2005/09/16 v1.4f Standard LaTeX document class
("C:\Programme\MiKTeX 2.7\tex\latex\base\size10.clo"))
("C:\Programme\MiKTeX 2.7\tex\latex\geometry\geometry.sty"
("C:\Programme\MiKTeX 2.7\tex\latex\graphics\keyval.sty")
("C:\Programme\MiKTeX 2.7\tex\latex\geometry\geometry.cfg"))
No file file48cc23c9.aux.
[1] (file48cc23c9.aux) )
Output written on file48cc23c9.dvi (1 page, 300 bytes).
Transcript written on file48cc23c9.log.
> dvips(object=dvi.obj2, "tst.ps")
This is dvips(k) 5.96 Copyright 2007 Radical Eye Software (www.radicaleye.com)
' TeX output 2008.12.17:1739' -> tst.ps
<C:/Programme/MiKTeX 2.7/dvips/base/tex.pro>
<C:/Programme/MiKTeX 2.7/dvips/base/texps.pro>. 
<C:/Programme/MiKTeX 2.7/fonts/type1/bluesky/cm/cmr10.pfb>[1]


-----Ursprüngliche Nachricht-----
Von: Pfaff, Bernhard Dr. [mailto:Bernhard_Pfaff at fra.invesco.com] 
Gesendet: Wednesday, December 17, 2008 4:25 PM
An: Willner, Marco; r-help at r-project.org
Cc: charles.dupont at vanderbilt.edu
Betreff: AW: [R] using dvi with latex object: directory not correctly set,maybe due to error in shQuote()

Hello Marco,

as might not be evident at first sight, but have you set the environment variable "R_SHELL"? If you spot at the dvi method for latex you will find a call to sys(), which will call shell() and if the argument shell is unset then the contents of "R_SHELL" will be used. Hence, what does:

Sys.getenv("R_SHELL")

yield at your machine? I reckon "" will be returned. Therefore as a first step: 
Sys.setenv(R_SHELL = "cmd.exe") or permanently in your R environment file. Having done so and running dvi(latex.obj) now produces at least not the warning that everything beyond "cd" is skipped and the command via paste is parsed. The next problem is the path the randomly generated file that latex cannot handle. The following alternative might work for you too:

dvi.latex2 <- function (object, prlog = FALSE, nomargins = TRUE, width = 5.5, height = 7, ...) 
{
    fi <- object$file
    sty <- object$style
    if (length(sty)) 
        sty <- paste("\\usepackage{", sty, "}", sep = "")
    if (nomargins) 
        sty <- c(sty, paste("\\usepackage[paperwidth=", width, 
            "in,paperheight=", height, "in,noheadfoot,margin=0in]{geometry}", 
            sep = ""))
    tmp <- tempfile(tmpdir = tempdir())
    tmptex <- paste(tmp, "tex", sep = ".")
    infi <- readLines(fi, n = -1)
    cat("\\documentclass{report}", sty, "\\begin{document}\\pagestyle{empty}", 
        infi, "\\end{document}\n", file = tmptex, sep = "\n")
    sc <- if (under.unix) {
        "&&"
    } else {
        "&"
    }
    shell(paste("cd", shQuote(tempdir()), sc, optionsCmds("latex"), 
        "-interaction=scrollmode", shQuote(tmp)), translate = TRUE)
    if (prlog) 
        cat(scan(paste(tmp, "log", sep = "."), list(""), sep = "\n")[[1]], 
            sep = "\n")
    fi <- paste(tmp, "dvi", sep = ".")
    structure(list(file = fi), class = "dvi")
}


And therefore:

tbl.loc   <- matrix(1:4, ncol=2)
latex.obj <- latex(tbl.loc)
tempdir <- function(){"H:/PROJECTS/data"}
Sys.getenv("R_SHELL")
Sys.setenv(R_SHELL = "cmd.exe")
Sys.getenv("R_SHELL")
## options(xdvicmd='dviout') set appropriately I use TeXLive and have not yap installed; 
## working with MikTeX there should be no need to change the default viewer
dvi.latex2(latex.obj)
## It might be the case that the dvi file is not displayed immediately after production but can be opened 
## manually


Does this work for your? Probably it is also a good idea to address this problem directly to the package maintainer (already cc'ed).

Best,
Bernhard


>
>Dear friends of R,
>
>I want to produce a pdf file with the contents of a matrix. I 
>employ the latex command in combination with dvi, both 
>contained in the Hmisc package. It seems to me that the 
>function does not correctly set the directory. 
>
>> tbl.loc   <- matrix(1:4, nc=2)
>> latex.obj <- latex(tbl.loc)
>> dvi(latex.obj)
>warning: extra args ignored after 'cd'
>H:\PROJECTS\data
>warning: extra args ignored after 'yap'
>
>When I have a look at the function dvi.latex I find the 
>following line which, I guess, is meant to set the new 
>directory and to run latex.
>
>    sys(paste("cd", shQuote(tempdir()), sc, optionsCmds("latex"), 
>        "-interaction=scrollmode", shQuote(tmp)), output = FALSE)
>
>Running just the piece shQuote(tempdir()) returns
>> shQuote(tempdir())
>[1] "\"C:\\DOKUME~1\\ferimawi\\LOKALE~1\\Temp\\Rtmpr4CG3A\""
>> tempdir()
>[1] "C:\\DOKUME~1\\ferimawi\\LOKALE~1\\Temp\\Rtmpr4CG3A"
>
>Is the leading "\" causing the problem? How can I fix the problem?
>
>The R-help dealt with a related problem some while ago but I 
>do not think that it resolves my problem:
>http://finzi.psych.upenn.edu/R/Rhelp02a/archive/62975.html
>
>I am using Windows XP, R version 2.7.2 (2008-08-25) and Hmisc 
>version 3.4-4.
>
>Thanks in advance for your help.
>Regards
>Marco
>
>Marco Willner 
>Senior Analyst Quantitative Asset Allocation 
>Feri Finance AG 
>Haus am Park     
>Rathausplatz 8-10 
>D-61348 Bad Homburg v.d.H 
>Tel: +49 (6172) 916 3037 
>Fax: +49 (6172) 916 1037 
>E-Mail: marco.willner at feri.de 
>Internet: www.feri.de 
>Handelsregister des Amtsgerichts Bad Homburg v.d.H. (HRB 7473) 
>Vorstände: Michael Stammler (Sprecher), Dr. Matthias Klöpper, 
>Dr. Helmut Knepel, Dr. Heinz-Werner Rapp, Arndt Thorn 
>Vorsitzender des Aufsichtsrates: Dr. Uwe Schroeder-Wildberg  
>Disclaimer: 
>Diese Nachricht enthält vertrauliche und/oder ausschließlich 
>für den Adressaten bestimmte Informationen. Wenn Sie nicht der 
>vorgesehene Adressat dieser E-Mail oder dessen Vertreter sein 
>sollten, so beachten Sie bitte, dass jede Form der 
>Kenntnisnahme, Veröffentlichung, Vervielfältigung oder 
>Weitergabe des Inhalts dieser E-Mail und der E-Mail selber 
>unzulässig ist. Sollten Sie diese E-Mail irrtümlich erhalten 
>haben, so bitten wir Sie, den Absender unverzüglich durch 
>Antwort-E-Mail oder Anruf unter +49 (6172) 916-0 zu 
>informieren und diese Nachricht zu löschen. Soweit nicht 
>anderweitig angegeben, ist diese Nachricht weder ein Angebot 
>noch die Einholung eines Angebots zum Kauf oder Verkauf von 
>Investitionen jedweder Art. Wir senden und empfangen E-Mails 
>nur auf der Grundlage, dass wir nicht für Datenkorruption, 
>Abfangen von Daten, nicht autorisierte Änderungen, 
>Verfälschung und Viren und deren Konsequenzen haften.
>This message contains confidential and/or privileged ...{{dropped:15}}



More information about the R-help mailing list