楼主: oliyiyi
1293 0

Makefiles for R/LaTeX projects [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


If you are using a Mac or Linux, you will already have make installed. If you are using Windows and have Rtools installed, then you will also have make. Otherwise, Windows users will need to install it. One implementation is in GnuWin.

A typical project of mine will include several R files containing code that fit some models, and generate tables and graphs. I try to set things up so I can re-create all the results by simply running the R files. Then I will have a LaTeX file which contains the paper or report I am writing. The tables and graphs produced by R are pulled in to the LaTeX file. Consequently, all I need to do is run all the R files, and then process the tex file, and the paper/report is generated.

Make relies on a Makefile to determine what it must do. Essentially, a Makefile specifies what files must be generated first, and how to generate them. So I need a Makefile that specifies that all the R files must be processed first, and then the LaTeX file.

The beauty of a Makefile is that it will only process the files that have been updated. It is smart enough not to re-run code if it has already been run. So if nothing has changed, running make does nothing. If only the tex file changes, running make will re-compile the tex document. If the R code has changed, running make will re-run the R code to generate the new tables and graphs, and then re-compile the tex document. All I do is type make and it figures out what is required.

A Makefile for LaTeX

It is easy to tell if the latex document needs compiling — make simply has to check that the pdf version of the document is older than the tex version of the document. Here is a simpleMakefile that will just handle a LaTeX document.

TEXFILE= paper$(TEXFILE).pdf: $(TEXFILE).tex        latexmk -pdf -quiet $(TEXFILE)


The first line specifies the name of my file, in this case paper.tex. The second line specifies that the pdf file must be created from the tex file, and the last line explains how to do that. MikTeX users might prefer pdftexify instead of latexmk.

To use the above Makefile, copy the code into a plain text file called Makefile and store it in the same directory as your tex file. Change the first line so the name of your tex file (without the extension) is used. Then type make from a command prompt within the same directory as the tex file, and it should do whatever is necessary to convert your tex to pdf.

Of course, you wouldn’t normally bother with a Makefile if that is all it did. But throw in a whole lot of R files, and it becomes very worthwhile.

A Makefile for R and LaTeX

We need a way to allow make to be able to tell if an R file has been run. If the R files are run using

R CMD BATCH file.R

then the output is saved as file.Rout. Then make only has to check if file.Rout is older thanfile.R.

I also like to strip out all the white space from the pdf figures created in R before I put them in a LaTeX document. There is a nice command pdfcrop which does that. (You should already have it on a Mac or Linux, and also on Windows provided you are using MikTeX.) So I also want myMakefile to crop all images if they have not already been done. Once an image is cropped, an empty file of the form file.pdfcrop is created to indicate that file.pdf has already been cropped.

OK, now we are ready for my marvellous Makefile.

# Usually, only these lines need changingTEXFILE= paperRDIR= .FIGDIR= ./figs # list R filesRFILES := $(wildcard $(RDIR)/*.R)# pdf figures created by RPDFFIGS := $(wildcard $(FIGDIR)/*.pdf)# Indicator files to show R file has runOUT_FILES:= $(RFILES:.R=.Rout)# Indicator files to show pdfcrop has runCROP_FILES:= $(PDFFIGS:.pdf=.pdfcrop) all: $(TEXFILE).pdf $(OUT_FILES) $(CROP_FILES) # May need to add something here if some R files depend on others. # RUN EVERY R FILE$(RDIR)/%.Rout: $(RDIR)/%.R $(RDIR)/functions.R        R CMD BATCH $< # CROP EVERY PDF FIG FILE$(FIGDIR)/%.pdfcrop: $(FIGDIR)/%.pdf        pdfcrop $< $< && touch $@ # Compile main tex file and show errors$(TEXFILE).pdf: $(TEXFILE).tex $(OUT_FILES) $(CROP_FILES)        latexmk -pdf -quiet $(TEXFILE) # Run R filesR: $(OUT_FILES) # View main tex fileview: $(TEXFILE).pdf        evince $(TEXFILE).pdf & # Clean up stray filesclean:        rm -fv $(OUT_FILES)         rm -fv $(CROP_FILES)        rm -fv *.aux *.log *.toc *.blg *.bbl *.synctex.gz        rm -fv *.out *.bcf *blx.bib *.run.xml        rm -fv *.fdb_latexmk *.fls        rm -fv $(TEXFILE).pdf .PHONY: all clean


Download the file here.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Projects Project Files LaTeX Late Windows generate running already include

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-28 22:41