裏 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 | トップ | Sweave-3 »
最新の画像もっと見る

コメントを投稿

RとLaTeX」カテゴリの最新記事