裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

Sweave-2

2011年09月29日 | RとLaTeX

ラッパー関数を作った

2011/910/13 版
texi2pdf にならい,Rerun to get ... が出なくなるまで複数回タイプセットを繰り返すようにした

ms <- function(file, encoding="euc-jp")
{
    read.lines <- function(file, encoding)
    {
        con <- file(file, open="r", encoding=encoding)
        x <- readLines(con)
        close(con)
        return(x)       
    }
    Sweave(file, encoding=encoding)
    file <- sub(".(R|S)nw", ".tex", file)
    x <- read.lines(file, encoding=encoding)
    bb <- sum(grepl("mediabb", x))
    con <- file(file, open="w", encoding=encoding)
    sapply(x, function(y) {
        if (bb == 0 && grepl("begin{document}", y, fixed=TRUE)) {
            cat("\\usepackage{mediabb}\n", file=con, append=TRUE)
        }
        if (grepl("includegraphics", y) && !grepl("(.jpg|.JPG|.png|.PNG)", y)) {
            y <- sub("}", ".pdf}\n", y)
        }
        cat(sprintf("%s\n", y), file=con, append=TRUE)
    })
    close(con)
    repeat {
        system(sprintf("/Applications/UpTeX.app/teTeX/bin/platex -kanji=euc %s", file))
        log <- read.lines(sub(".tex", ".log", file), encoding)
        if (sum(grepl("Rerun to get", log)) == 0) break
    }
    system(sprintf("/Applications/UpTeX.app/teTeX/bin/dvipdfmx %s", sub(".tex", ".dvi", file)))
}

旧版

ms <- function(file, encoding="euc-jp")
{
    Sweave(file, encoding=encoding)
    file <- sub(".(R|S)nw", ".tex", file)
    con <- file(file, open="r", encoding="euc-jp")
    x <- readLines(con)
    close(con)
    bb <- sum(grepl("mediabb", x))
    con <- file(file, open="w", encoding="euc-jp")
    sapply(x, function(y) {
        if (bb == 0 && grepl("begin{document}", y, fixed=TRUE)) {
            cat("\\usepackage{mediabb}\n", file=con, append=TRUE)
        }
        if (grepl("includegraphics", y)) {
            y <- sub("}", ".pdf}\n", y)
        }
        cat(sprintf("%s\n", y), file=con, append=TRUE)
    })
    close(con)
}
ms("sweave.Snw")

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

Sweave

2011年09月29日 | RとLaTeX

1. Sweave で作成される tex 内で,\includegraphics{ } で指定される画像ファイルの指定には,拡張子がつかない。
\usepackage[dvipdfm]{graphicx} を使うと,自動的に .pdf が補われるということだけど,補われない。

\usepackage{ctable} があるとこのようになるようだ。

2. バウンディング・ボックスを ebb で用意しなくても,\usepackage{mediabb} を使えばよい。

3. コメントを残すためには,\SweaveOpts{keep.source=TRUE} を使う。

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

PVアクセスランキング にほんブログ村

PVアクセスランキング にほんブログ村