LoginSignup
3
3

More than 5 years have passed since last update.

Makefile を使った、 LaTeX と 他のツール (R) からの論文作成

Last updated at Posted at 2016-04-04

論文を用意する場合は、R などで図を用意し、 LaTeX で論文を作成することが多い。 ここでは、.tex や .r を用意し、自動的に更新する方法を示します。

ここでは、 Sweave や RMarkdown など、R の中で文書を生成する方法とは異なる方法を説明しています。 実際、ここの方法は他のツール、例えば Python+matplotlib などにも応用することができます。

なお、MacOSX 上で、Fink によってインストールされた texlive と r-base を使用しています。

次に、用意するファイルです。

  • Makefile
  • ronbun.tex
  • fig.r
  • data.csv
Makefile
TARGET := ronbun
PNGFILES := fig.png

REMOVE=rm -f
LATEX=/sw/bin/platex -kanji=utf8 -shell-escape
BIBTEX=/sw/bin/bibtex
DVIPDFMX=/sw/bin/dvipdfmx
EBB=/sw/bin/ebb
RSCRIPT=/sw/Library/Frameworks/R.framework/Resources/bin/Rscript

all: $(TARGET).pdf 

clean:
    $(REMOVE) *.aux *.log *.dvi *.bbl *.bbg *.bb *.xbb

$(TARGET).tex: $(PNGFILES)

.tex.pdf:
    -$(LATEX) $(TARGET).tex && $(BIBTEX) $(TARGET)
    $(LATEX) $(TARGET).tex && $(LATEX) $(TARGET).tex && $(DVIPDFMX) $(TARGET).dvi

.r.png:
    $(RSCRIPT) $<

.SUFFIXES: .tex .bib .pdf .dvi .r .png .bb .xbb

.PHONY: all clean
ronbun.tex
\documentclass[a4paper,12pt]{jsarticle}
\usepackage[a4paper]{geometry} 
\usepackage[dvipdfmx]{graphicx}
\usepackage{amsmath,amssymb}

\begin{document}

\section{はじめに}
論文をかくときは、LaTeXやRを使う。

\includegraphics[width=100mm]{fig.png}

\end{document}
fig.r
png('~/Documents/fig.png', 
    width = 100, height = 100, units = "mm", res=300, pointsize = 8)
par(family="Hiragino Kaku Gothic Pro W6")
Dataset <- read.table("data.csv", header=TRUE, 
    sep=",", na.strings="", dec=".", strip.white=TRUE)
.Table <- xtabs(~X+Y, data=Dataset)
barplot(as.matrix(.Table), 
     ylim=c(-1,50),
     xlim=c(0,3),
     space=0.5,
     xlab="X", ylab="Number of patients", 
     axes = TRUE,
     col=c("white","darkgrey"),
     names.arg=c('X1','X2'),
     legend.text = TRUE, 
     args.legend = list(x = "topleft", bty = "n"),
     border = T)
abline(h=0) 
dev.off()

一行目で保存先を指定しているので、適宜変えてください。

data.csv
X,Y
1,1
1,1
1,1
1,2
1,2
2,1
2,2
2,2
2,2
2,2

では、論文を作成しましょう。

$ make

これで、自動的に .png ファイルが作成され、.tex が .pdf に変換されます。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3