# copy finalised PDF to new directory

I want to automatically make a copy of the finalised PDF in a new directory once it is compiled.

As part of my workflow, each article (of my thesis) (and the associated graphics) is stored in its own directory. I then import each file into a master file as a combined thesis.

I already do this with the tex file itself (make a daily backup).

I have the following code at the start of each article tex file (which basically copies the existing tex file to a backup directory and renames it with today's date so I have a history of tex files):

\immediate\write18{mkdir \currfiledir backup}
\immediate\write18{cp '\currfiledir \currfilename' '\currfiledir backup/\currfilebase\string_\the\year-\the\month-\the\day .\currfileext'}


I want to have similar code that copies the finalised PDF file from the master directory into the directory of each article. Something like this (but this didn't work).

\immediate\write18{cp \jobname.pdf  \currfiledir\currfilebase.pdf}


• Once you have a saved copy of the tex file you can easily recreate the PDF file -- you could o this manually or you could use \write18 to run pdflatex directly after you execute the lines above. Alternatively, you could use \write18 to copy the PDF file in addition to the tex file. This said, I think you would be much better off using a version control program to do this properly. I highly recommend git. There is a bit of a learning curve but it will keep track of your daily snap shots without you having to keep all these old files lying around. – Andrew Nov 3 '14 at 13:04
• pdflatex (and other web2c engines) have an commandline flag -output-directory=DIR use existing DIR as the directory to write files in – David Carlisle Nov 3 '14 at 13:08
• making the backup copy when you run tex seems strange, don't you want to make it when you edit the file (ie your editor should do it? – David Carlisle Nov 3 '14 at 13:09
• You can't use \write18 to move the PDF file, because the PDF file is finalized when executing \end, which comes later than any \write in the input. – egreg Nov 3 '14 at 14:08
• Andrew's comment deserves more than the +1 I can provide: you have a Unix-like system, which means you have access to make and your choice of scripting languages and version control systems. This is what they're designed for. Will make a quick example of how this could be accomplished. – Mike Renfro Nov 3 '14 at 15:25

Related (at least in my opinion): How to properly 'make' a latex project? and Using makefile to create tar archive

Basically, I'd make a Makefile, use latexmk to handle document builds, and tar to make regular backups (any version control system is better than tar, but there's practically no learning curve to this one).

Makefile:

.PHONY: MyDoc.pdf all clean

all: MyDoc.pdf

MyDoc.pdf: MyDoc.tex
latexmk -pdf -pdflatex="pdflatex -interactive=nonstopmode" -use-make MyDoc.tex

clean:
latexmk -CA

backup:
tar -czf ../MyDocs-date +%Y.%m.%d.tar.gz .

• Thanks for the answer - I currently don't explicitly use a make file, but I do use latemk. I'm not so interested in more than a daily backup (and I'm not productive enough to think I would be losing that much...), that's why I'm making backups with write18. I'll try out your approach though. Ideally, though, I would like not to have too complicated an approach. A single line in my tex file would be preferable. – James Nov 3 '14 at 17:03
• If you're going to the command line to run latexmk, then running make for compilation isn't any harder. If your editor is automatically running latexmk, then there may be a way to have it run make instead. Embedding shell commands (3, so far) into the document runs against my inclinations, and I think it'd get tedious if you ever add more commands. – Mike Renfro Nov 3 '14 at 21:00