I'm giving a talk tomorrow. My slides are mostly compiled from the pile of talks I gave at the beginning of the year at KITP and while going around applying for jobs. Therefore, my figures are all scattered around in a bunch of different directories. I thought it was dumb to copy them all (again) into the workdir for this talk. This led to the thought that I should symlink them. This led to the thought that it'd be cool if I could convince the Makefile for the talk to figure out which figures are needed for the slides, and do the linking itself. Two hours of playing around later (talk content...um...somewhat postponed, I guess) and I have this (with added commentary):
This bit defines the directories to search for figures in. I also set the target list by using dark shell magics to extract the list of figures used in the slides.tex file.
FIGURE_DIRECTORIES := ../2009_02_24-Job_place ../2009_01_27-KITP_talk
FIGURE_TARGETS := $(shell grep myfig slides.tex | grep -Ev '^%' | grep -v 'newcommand' | sed 's/.*myfig\[.*\]{\(.*\)/\1/; s%^./%%; s/}.*$$//;' | sort | uniq)
FIGURE_TARGETS += PPR_thesis.sty
LINK_TARGETS = ${FIGURE_TARGETS}
LINK_SOURCE :=
CLEAN_OBJS :=
This is the cool bit. We define the linky command that accepts two arguments, the file we care about, and the directory we're looking in now. If we detect that the file exists in that directory, then append the full path to our link sources list, and also add that file to the list of files to auto-clean later. Then, we remove that file from the list we're searching, since we don't need to look for that file in all the subsequent directories.
define linky
ifeq ($(shell [ -e $(2)/$(1) ] && echo exists),exists)
LINK_SOURCE += $(2)/$(1)
CLEAN_OBJS += $(1)
LINK_TARGETS := $(filter-out $(1),${LINK_TARGETS})
endif
endef
This is the loop where we run over the directories and targets, and execute "linky" for each.
$(foreach d,$(FIGURE_DIRECTORIES),\
$(foreach t,$(LINK_TARGETS),\
$(eval $(call linky,$(t),$(d)))))
Now, since the variables we care about have been defined now, we can define the commands to actually do the linking and cleaning.
links :
ln -sf ${LINK_SOURCE} .
clean :
rm -f ${CLEAN_OBJS}
The coolest bit about this is that it's totally general, so I can use this same bit of code in all my LaTeX Makefiles from now on.
PS: It looks like blogger hates the whitespace and line breaks and stuff. It's probably pretty clear what it needs, though.
PPS: No actual necromancy was used for this project.
Thursday, September 10, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment