Reputation: 2126
Goal
I am trying to use a Make file to automate the generation of a panel figure. The data and figures are generated from Rscripts and subsequently combined using latex to generate a PDF.
Problem description
Let's say, I would like to generate a panel figure consisting of 4 subfigures, arranged in a 2x2 grid. By using the following make file, I am able to generate the data as well as the subfigures:
all : data/plot1.txt plots/plot1.pdf \
data/plot2.txt plots/plot2.pdf \
data/plot3.txt plots/plot3.pdf \
data/plot4.txt plots/plot4.pdf \
plots/figure_5.pdf
#Panel Figure#
#Plot 1
data/plot1.txt : scripts/plot1_sim.R
Rscript scripts/plot1_sim.R $@
plots/plot1.pdf : scripts/plot1_sim.R data/plot1.txt
Rscript scripts/plot1.R $@
#Plot 2
data/plot2.txt : scripts/plot2_sim.R
Rscript scripts/plot2_sim.R $@
plots/plot2.pdf : scripts/plot2_sim.R data/plot2.txt
Rscript scripts/plot2.R $@
#Plot 3
data/plot3.txt : scripts/plot3_sim.R
Rscript scripts/plot3_sim.R $@
plots/plot3.pdf : scripts/plot3_sim.R data/plot3.txt
Rscript scripts/plot3.R $@
#Plot 4
data/plot4.txt : scripts/plot4_sim.R
Rscript scripts/plot4_sim.R $@
plots/plot4.pdf : scripts/plot4_sim.R data/plot4.txt
Rscript scripts/plot4.R $@
Using a latex file, e.g. figure.tex
I am able to compile a panel figure as desired.
However, now I would like to add the construction of the final PDF by latex to the make file. This file is, of course, dependent on plots/plot1.pdf
up to and including plots/plot4.pdf
.
I added it to the code like this:
plots/figure.pdf : scripts/figure.tex plots/plot1.pdf plots/plot2.pdf plots/plot3.pdf plots/plot4.pdf
pdflatex scripts/figure.tex
With four subfigures it is doable (but maybe ugly) to do it like this, but if you generate a panel figure with e.g. 16 subfigures, the code becomes unreadable.
Question
Is there a better way to include a larger list of dependencies, in this case for generating panel figures using make and latex?
Upvotes: 0
Views: 52
Reputation: 28955
As far as I understand you could probably factorize all this with something like:
SIMRS := $(wildcard scripts/plot*_sim.R)
PLOTS := $(patsubst scripts/plot%_sim.R,%,$(SIMRS))
PDFPLOTS := $(patsubst %,plots/plot%.pdf,$(PLOTS))
TXTPLOTS := $(patsubst %,data/plot%.txt,$(PLOTS))
.PHONY: all
all: $(TXTPLOTS) $(PDFPLOTS) plots/figure_5.pdf
data/plot%.txt : scripts/plot%_sim.R
Rscript $< $@
plots/plot%.pdf : scripts/plot%_sim.R data/plot%.txt
Rscript scripts/plot$*.R $@
plots/figure.pdf: scripts/figure.tex $(PDFPLOTS)
pdflatex $<
Note that the plots/plot%.pdf: ...
pattern rule looks strange: its recipe uses scripts/plotX.R
but its prerequisites are scripts/plotX_sim.R
and data/plotX.txt
(no scripts/plotX.R
). Is it normal?
Upvotes: 1