[[Mac で Sweave]]

Mac OS X El Capitan で,従来のやり方では日本語処理ができなくなったので,対処法を示す。

★ MacTeX のインストール

http://d.hatena.ne.jp/pyopyopyo/20150710/p1 に従う

ftp://ftp.tug.org/tex/mactex/ から MacTeX.pkg をダウンロード

ダブルクリックでインストール

必要なパッケージを追加インストールする(CUIでtlmgrコマンドを使う)

まずは tlmgr 自身を更新

sudo tlmgr update --self 

次に導入済みパッケージの更新

sudo tlmgr update  --all

★ スタイルファイルなど

ファインダーの option+「移動」メニューで「ライブラリ」ディレクトリに入り,
texmf/tex/platex ディレクトリに使用する style ファイルを置く
おいた後には必ず sudo mktexlsr の実行を忘れないこと
なお,tex ファイルは utf-8 エンコーディングとするので,style ファイルも utf-8 にしておくこと

★ UpTeX のインストール

http://www2.kumagaku.ac.jp/teacher/herogw/index.html から
「Drag & Drop UpTeX」(20151005版以降)をダウンロードする。 
し,インストールする。

アプリケーションフォルダに入れた状態では起動できないかもしれない(その場合は任意のフォルダに入れる)

★ TeXShop のインストール

最新のものをインストール
パス設定は
/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 が別のパスのものを指しているときがある。そのようなときにはので注意)

★ tex

document class は ujarticle,ujbook,ujreport など
\usepackage[dvipdfmx]{graphicx}


★ コマンドラインから は,以下のように

AIC ディレクトリに AICutf.Rnw があるとき,最終的に AICutf.pdf を作るには以下のようにする

$ cd /Users/aoki/Desktop/Rnw/AIC
$ Rscript --vanilla -e "Sweave('AICutf.Rnw', encoding='utf-8')"
$ ptex2pdf -u -l -ot "-synctex=1 -file-line-error" AICutf.tex

★ シェルスクリプトの例

#!/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

★ Rscript で書いた TeXShop の Engine の例

以下の内容のファイル Sweave.engine を,ファインダーの option+「移動」メニューで「ライブラリ」ディレクトリに入り,TeXShop/Engines の中にコピーする。アクセスモードは chmod 777 Sweave.engine で,実行可能に設定しておく。

書類の種類を選択するボタン(通常は LaTeX などになっている)で,「Sweave」を選択し「タイプセット」ボタンをクリックすると *.Rnw --> *.tex --> *.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 <- 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)

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS