Skip to content

Instantly share code, notes, and snippets.

@caiofcm
Last active June 13, 2018 19:47
Show Gist options
  • Save caiofcm/e9c7cc628bc309b09e5488477b465ac4 to your computer and use it in GitHub Desktop.
Save caiofcm/e9c7cc628bc309b09e5488477b465ac4 to your computer and use it in GitHub Desktop.
Create revision pdf file from latex using git commits

Create PDF file from two latex git commits

See Answer: https://tex.stackexchange.com/questions/266879/latex-git-mark-differences-since-specific-commit

  1. Start by getting the version of a file from a previous commit, with

git show <commit>:filename.tex > tmp.tex

  1. Run latexpand to include possible \input and \include files

latexpand tmp.tex -o tmp_exp.tex latexpand current_file.tex -o cur_exp.tex

  1. Run latexdiff to generate a new .tex file with the differences highlighted:

latexdiff tmp_exp.tex cur_exp.tex > tmpdiff.tex

  1. Compile tmpdiff.tex as usual.

Issue on Windows MikTex 2.9:

latexdiff error (script not found)

  1. Install perl (padre IDE)
  2. Download latexdiff from: https://ctan.org/pkg/latexdiff
  3. Unzip content into "C:\Program Files\MiKTeX 2.9\scripts\latexdiff" or similar

Encoding Issue:

Solved from Notepad++ changing the Encoding > UTF8 DOM

Caveat

If using include / input it may not work properly. An workaroun is cited here, which is also an alternative to all the steps:

https://gitlab.com/git-latexdiff/git-latexdiff

Ease to use script:

Example:

python git_tex_compare ./file.tex master

import sys
import os
import subprocess
def main():
if len(sys.argv) < 3:
raise ValueError('Enter: tex_file commit_hash arguments (use relative path for tex file)')
tex_filename = sys.argv[1]
commit_ref = sys.argv[2]
string_command = 'git show {}:{} > tmp.tex'.format(commit_ref, tex_filename)
subprocess.call(string_command, shell=True)
subprocess.call('latexpand tmp.tex -o tmp_exp.tex', shell=True)
subprocess.call(
'latexpand {} -o cur_exp.tex'.format(tex_filename), shell=True)
subprocess.call('latexdiff tmp_exp.tex cur_exp.tex > tmpdiff.tex', shell=True)
os.remove('tmp.tex')
os.remove('tmp_exp.tex')
os.remove('cur_exp.tex')
print(string_command)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment