- 追加された行はこの色です。
- 削除された行はこの色です。
SIZE(20){COLOR(blue){Mac で Sweave}}
Mac OS X El Capitan
** MacTeX のインストール [#be080a3f]
// http://d.hatena.ne.jp/pyopyopyo/20150710/p1 に従う
http://d.hatena.ne.jp/pyopyopyo/20150710/p1 に従う
ftp://ftp.tug.org/tex/mactex/ から MacTeX.pkgをダウンロード(このページ作成時の最新版は basictex-20150613.pkg)
ダブルクリックでインストール
必要なパッケージを追加インストールする(「ターミナル」で tlmgr コマンドを使う)
まずは tlmgr 自身を更新
sudo tlmgr update --self
次に導入済みパッケージの更新
sudo tlmgr update --all
** スタイルファイルなど [#webd7039]
ファインダーの option+「移動」メニューで「ライブラリ」ディレクトリに入り,texmf/tex/platex ディレクトリに使用する style ファイルを置く
システムのライブラリではなく,
Macintosh HD-->ユーザ-->hoge-->ライブラリ-->texmf-->tex-->platex-->スタイルファイル(のディレクトリ)
置いた後には必ず sudo mktexlsr の実行を忘れないこと
なお,*.Rnw,*.tex ファイルは utf-8 エンコーディングとするので,style ファイルも utf-8 にしておくこと
** UpTeX のインストール [#z1939e5f]
http://www2.kumagaku.ac.jp/teacher/herogw/index.html から「Drag & Drop UpTeX」(20151009版以降)をダウンロードし,ダブルクリッックしてできる UpTeX.app を「アプリケーションフォルダ」へ移動する
同梱されている説明書を読み,環境設定を行う
** TeXShop のインストール [#u675fd21]
http://pages.uoregon.edu/koch/texshop/obtaining.html から、「Latest TeXShop Version3」をダウンロードし,ダブルクリッックしてできる TeXShop.app を「アプリケーションフォルダ」へ移動する
パス設定は
/Applications/UpTeX.app/Contents/Resources/texbin
LaTeX の設定は
ptex2pdf -u -l -ot "-synctex=1 -file-line-error"
tex-->dvi-->pdf は
ptex2pdf -u -l -ot "-synctex=1 -file-line-error"
を使う
(dvipdfmx が別のパスのものを指しているときがあるので注意。別の dvipdfmx などは消去するか改名する)
** *.Rnw,*.tex [#u23b093e]
encoding は utf-8 とする
dvipdfmx,uplatex の使用は document class で指定する。
\documentclass[dvipdfmx,uplatex] {jsarticle}
** タイプセットの方法 [#fdef66a2]
*** コマンドラインからの例 [#jdeeff62]
foo ディレクトリに foo.Rnw があるとき,最終的に foo.pdf を作るには以下のようにする
$ cd /foo
$ Rscript --vanilla -e "Sweave('foo.Rnw', encoding='utf-8')"
$ ptex2pdf -u -l -ot "-synctex=1 -file-line-error" foo.tex
*** シェルスクリプトの例 [#qd3933a5]
#!/bin/sh
# make Sweave
#
Rscript --vanilla -e "Sweave('$1.Rnw', encoding='utf-8')"
/uptex/UpTeX.app/Contents/Resources/texbin/ptex2pdf -u -l -ot "-synctex=1 -file-line-error" $1.tex
*** TeXShop からの場合 [#q38c031b]
Rscript で書いた TeXShop の Engine の例
以下の内容のファイル Sweave.engine を,ファインダーの option+「移動」メニューで「ライブラリ」ディレクトリに入り,TeXShop/Engines の中にコピーする。システムのライブラリではなく,
Macintosh HD-->ユーザ-->hoge-->ライブラリ-->TeXShop-->Engines-->Sweave.engine
アクセスモードは chmod 777 Sweave.engine で,実行可能に設定しておく。
書類の種類を選択するボタン(通常は LaTeX などになっている)で,「Sweave」を選択し「タイプセット」ボタンをクリックすると *.Rnw --> *.tex --> *.dvi --> *.pdf の順に処理される。
#!/usr/local/bin/Rscript --vanilla
ms <- function(file, makeindex = FALSE, silent = TRUE, deleteWorkfiles = FALSE, ...) {
if (grepl("\\.", file) == FALSE) {
file <- paste(file, "Rnw", sep = ".")
}
Sys.setlocale(locale = "ja_JP.UTF-8")
Sweave(file, encoding = "utf-8")
base <- sub(".(R|S)nw", "", file)
file <- paste(base, ".tex", sep = "")
cmd <- sprintf("/Applications/UpTeX.app/Contents/Resources/texbin/ptex2pdf -u -l -s -ot '-synctex=1 -file-line-error' %s%s", file, ifelse(silent, " | grep -v \"texmf\"", ""))
repeat {
system(cmd)
con <- file(sub(".tex", ".log", file), open = "r", encoding = "utf-8")
log <- readLines(con)
close(con)
no.error <- TRUE
if (sum(grepl("Emergency stop", log)) > 0) {
no.error <- FALSE
break
}
if (sum(grepl("Rerun to get", log)) == 0)
break
}
if (no.error) {
if (makeindex) {
system(sprintf("~/mbin/makeindex %s", sub(".tex", "", file)))
system(cmd)
}
system(sprintf("/Applications/UpTeX.app/Contents/Resources/texbin/dvipdfmx -V 5 %s", sub(".tex", ".dvi", file)))
}
if (deleteWorkfiles) {
system(sprintf("/bin/rm %s.aux %s.dvi %s.log %s.tex %s.toc", base, base,
base, base, base))
}
}
# 以下の xfigure 関数は,src パラメータで渡された描画プログラムに基づいて,figure というサブディレクトリに pdf 画像ファイルを作り,それを pdf 文書ファイル中に include するためのもの
xfigure <- function(src, caption = "caption", fn, label = fn,
base = "figure", size = 320, width = size * 6/320, height = 2/3,
mgp = c(1.8, 0.6, 0), mar = c(3, 3, 1, 1), rev = -3, htbp = "htbp") {
height <- width * height
pdf(sprintf("%s/%s.pdf", base, fn), width = width, height = height)
par(mgp = mgp, mar = mar)
parsed <- parse(text = src)
results <- eval(parsed)
dev.off()
header <- sprintf("\\begin{figure}[%s]\n\t\\centering",
htbp)
trailer <- sprintf("\t\\vspace{%smm}\\caption{%s}\n\t\\label{%s}\n\\end{figure}",
rev, caption, label)
cat(sprintf("%s\n\t\\includegraphics[bb=0 0 %sin %sin, clip, width=%sbp]{%s/%s.pdf}\n%s\n",
header, width, height, size, base, fn, trailer), fill = TRUE)
invisible(results)
}
grDevices::pdf.options(family="Japan1")
arg <- commandArgs()
ms(arg[7])
warn <- warnings()
if (length(warn)) head(warn, 2)