4

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}

Thanks in advance

James
  • 173
  • 2
    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.

    –  Nov 03 '14 at 13:04
  • 2
    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 03 '14 at 13:08
  • 1
    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 03 '14 at 13:09
  • 2
    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 03 '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 03 '14 at 15:25
  • @Andrew - I'm using multiple computers to access my Dropbox stored files. Would git work in this case? – James Nov 03 '14 at 17:06
  • You can use symlinks (see command ln -s) for this. – alfC Nov 03 '14 at 17:42
  • Yes git works across multiple machines/accounts. It was written to manage code written by multiple developers so one of its main requirements is that it needs to work across many different computers. I use git for all of my papers these days and sync them remotely with bitbucket and different copmuters. –  Nov 03 '14 at 19:18
  • You can use git/github for version control instead of creating backup of each folders. – SL4566 Jan 27 '22 at 00:26

2 Answers2

4

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 .
Mike Renfro
  • 20,550
  • 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 03 '14 at 17:03
  • 1
    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 03 '14 at 21:00
1

Essentially this question is a special case of Can TeX post-process the PDF it produces?

Solution adapted from https://tex.stackexchange.com/a/648388/250119 , use wrapup_run hook. LuaLaTeX only.

%! TEX program = lualatex
\documentclass{article}
\usepackage[abspath]{currfile}
\usepackage{luacode}
\begin{document}

123

\begin{luacode} luatexbase.add_to_callback("wrapup_run", function() local output_file=token.get_macro("currfiledir")..token.get_macro("currfilebase")..".pdf" print("======== Copying finalized PDF to "..output_file) local outfile=io.open(output_file, "wb") outfile:write(io.open(tex.jobname..".pdf", "rb"):read("a")) outfile:close() end, "final callback to copy pdf file") \end{luacode}

\end{document}

Compile with lualatex -recorder path/to/file.tex.

user202729
  • 7,143