Modular papers with LaTeX


Earlier this week I wrote about how to structure an academic project directory and one of those directories was paper. The project write-ups (read: papers) will live here. My tool of choice for writing is LaTeX. It is not for everyone, and since you’re this far I’m going to assume that you’re already a fan!

While templates for many academic journals are available from ShareLaTeX, Overleaf, and often the journals themselves, they share a common problem: each template is a new file with the content copied into it. One of main reasons that we use LaTeX is the clean separation of content and style, and having a new, single tex file for each journal violates this principle. We can overcome this by using multiple files for the content of our writing. To start, we can take a paper that has files looking like this:

emotional-arcs-EPJ-template.tex
bmc-mathphys.bst
bmcart.cls
everything.bib

Into one that looks like this:

emotional-arcs-EPJ.tex
emotional-arcs.body.tex
emotional-arcs.abs.tex
bmc-mathphys.bst
bmcart.cls
everything.bib

We’ve taken the both and the abstract out, and included them inside the EPJ template using \input{\filenamebase.abs} and \input{\filenamebase.body} where for convenience we define the working name of the paper only once in the file with \newcommand{filenamebase}{emotional-arcs}.

Immediately, we can now easily switch between formats and rely on the same body and abstract files:

emotional-arcs-EPJ.tex
emotional-arcs-revtex4.tex
emotional-arcs.body.tex
emotional-arcs.abs.tex
bmc-mathphys.bst
bmcart.cls
unsrtabbrv.bst
everything.bib

Expanding on this idea, we’ll end up with a directory that looks just like the first academic paper directory I ever encountered, one laid out by Peter Dodds:

bmc-mathphys.bst
bmcart.cls
emotional-arcs-EPJ-supp.tex
emotional-arcs-EPJ.tex
emotional-arcs-dissertation-supplementary.tex
emotional-arcs-dissertation.tex
emotional-arcs-revtex4.tex
emotional-arcs.abs.tex
emotional-arcs.acknowledgments.tex
emotional-arcs.author.EPJ.tex
emotional-arcs.author.tex
emotional-arcs.biblio.tex
emotional-arcs.body.captions.tex
emotional-arcs.body.figures.tex
emotional-arcs.body.nocite.tex
emotional-arcs.body.nofigures.tex
emotional-arcs.body.tables.tex
emotional-arcs.body.tex
emotional-arcs.coverletter.tex
emotional-arcs.inputs.txt
emotional-arcs.kwd.tex
emotional-arcs.nbooks.tex
emotional-arcs.settings.tex
emotional-arcs.supplementary.SOM.tex
emotional-arcs.supplementary.SVD-all.tex
emotional-arcs.supplementary.SVD.tex
emotional-arcs.supplementary.construction.tex
emotional-arcs.supplementary.extras.tex
emotional-arcs.supplementary.networks.tex
emotional-arcs.supplementary.null.tex
emotional-arcs.supplementary.prediction.tex
emotional-arcs.supplementary.ringsort.tex
emotional-arcs.supplementary.stories.tex
emotional-arcs.supplementary.tex
emotional-arcs.supplementary.wards-all.tex
emotional-arcs.supplementary.wards.tex
emotional-arcs.title.tex
everything.bib
makefile
unsrtabbrv.bst

Wow, that’s a lot of files! And for your sake, I didn’t even include all of the automatically generated files. For starters, we can note that all five of these files are generated by a Python script from our body file:

emotional-arcs.body.captions.tex
emotional-arcs.body.figures.tex
emotional-arcs.body.nocite.tex
emotional-arcs.body.nofigures.tex
emotional-arcs.body.tables.tex

That allowed us to include the figures after the main text in our EPJ template. For this procedure, on would run

python ${PYTOOLDIR}split_body.py ${PAPER}.body.tex

which a line straight from the makefile. The Python code to do this is over at: https://github.com/andyreagan/kitchentabletools-python.

Other things that are now possible include making a -combined.tex file for pulling in all of the inputs (see input-inputs.py for that script), and copying the combined file into a directory with the figures locally (renamed fig01,fig01, etc).

Some of the .tex files aren’t even ones that we wrote: Python also spit out some of the tables (*.SVD-all.tex) and parameters (*.nbooks.tex) from the analysis itself. Made an update to the code? No problem, the figures and parameter files are refreshed all we need to do is run make all to build the paper!

For a repository with an example layout, see https://github.com/petersheridandodds/universal-paper-template.