Skip to content

Instantly share code, notes, and snippets.

@gidden
Last active February 1, 2022 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gidden/9710452e4eadfbe6a29ad35e1dd250bc to your computer and use it in GitHub Desktop.
Save gidden/9710452e4eadfbe6a29ad35e1dd250bc to your computer and use it in GitHub Desktop.
Generate diff PDF from one or more latex files
#!/usr/bin/env python
import argparse
import glob
import os
import shutil
import subprocess
def main():
# CLI
descr = "Run latexdiff and generate a pdf"
parser = argparse.ArgumentParser(description=descr,
formatter_class=argparse.RawDescriptionHelpFormatter)
r1 = 'Starting commit/revision/tag.'
parser.add_argument('r1', help=r1)
r2 = 'Ending commit/revision/tag. Defaults to HEAD.'
parser.add_argument('--r2', help=r2, default='HEAD')
args = parser.parse_args()
# args from CLI
r1 = args.r1
r2 = args.r2
# save files
files = glob.glob('*.tex')
for f in files:
shutil.copyfile(f, '.' + f)
# generate diffs
cmd = "latexdiff-vc -r {} -r {}"
subprocess.check_call(cmd.format(r1, r2).split() + files)
# build pdf
diff_file = lambda f, r1, r2: "{}-diff{}-{}.{}".format(
f[:-4], r1, r2, f[-3:])
for f in files:
shutil.copyfile(diff_file(f, r1, r2), f)
subprocess.check_call('make')
shutil.copyfile('paper.pdf', diff_file('paper.pdf', r1, r2))
# clean up
for f in files:
shutil.copyfile('.' + f, f)
rm = glob.glob(diff_file('*.tex', r1, r2)) + glob.glob('*oldtmp*.tex')
for f in set(rm):
os.remove(f)
if __name__ == '__main__':
main()
Copyright (c) 2016, Matthew Gidden
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@gidden
Copy link
Author

gidden commented Oct 23, 2016

This assumes you build your pdf with a Makefile and that the name of the file to build is paper.tex. This can be customized as needed.

@pshriwise
Copy link

Hi @gidden! Recently used this awesome tool. Any chance you'd be alright with me making a repo for it to add some flexibility and features?

@gidden
Copy link
Author

gidden commented Feb 1, 2022

Please go ahead @pshriwise, glad it has been of use =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment