twilsonco / latexdiffcite

latexdiffcite is a wrapper around latexdiff to make citations diff properly
BSD 2-Clause "Simplified" License
26 stars 9 forks source link

latexdiff cannot find \begin{document} \end{document} pair because it does not see the whole tempfiles #3

Closed ftilmann closed 7 years ago

ftilmann commented 7 years ago

When trying to run latexdiffcite I get the following error:

> latexdiffcite file test1.tex test2.tex
processing old revision
processing new revision
running latexdiff /tmp/tmp_old_yrB3JS.tex /tmp/tmp_new_IhZx8l.tex
Traceback (most recent call last):
  File "/home/tilmann/.local/bin/latexdiffcite", line 11, in <module>
    sys.exit(main())
  File "/home/tilmann/.local/lib/python2.7/site-packages/latexdiffcite/latexdiffcite.py", line 163, in main
    run(parsed_args)
  File "/home/tilmann/.local/lib/python2.7/site-packages/latexdiffcite/latexdiffcite.py", line 298, in run
    run_latexdiff(Files.tex_old_tmp_path, Files.tex_new_tmp_path)
  File "/home/tilmann/.local/lib/python2.7/site-packages/latexdiffcite/latexdiffcite.py", line 818, in run_latexdiff
    raise ValueError('latexdiff returned with code {}. Error from latexdiff:\n\n'.format(ret_code) + stderr)
ValueError: latexdiff returned with code 255. Error from latexdiff:

\\begin\{document\} and \\end\{document\} not in the correct order or not present as a pair. at /home/tilmann/bin/latexdiff line 1531.

I tried creating a log file but there did not seem to be anything amiss, so I won't post. Bizarrely, if I run interactively latexdiff /tmp/tmp_old_yrB3JS.tex /tmp/tmp_new_IhZx8l.tex, it works! (for this test I had temporally changed latexdiffcite so that it would not delete the temporary files). I did some more debugging and somehow this problem appears to be related to the file size; for really short files it worked, but when adding material, it breaks; I verified by experimentation that it is not any particular line that causes the problem, but it seems more of a threshold value in file size. The file size of the file, where it fails, is 3938 characters, but my experimentation did not go as far as to get an exact value for the threshold, or check whether it is indeed the number of characters that matters or maybe lines or some other measure of size. (python --version returns 2.7.6). I realise this all might not be enough for you to reproduce the error, so please let me know what info you need.

cmeeren commented 7 years ago

Sorry, I have absolutely no idea why this fails. latexdiffcite simply calls latexdiff [tempfile1] [tempfile2] (see run_latexdiff()), so I don't see why the result would be any different than calling latexdiff manually on the temp files.

The only thing I can think of is if you have specified some latexdiff_args in latexdiffcite's config.

ftilmann commented 7 years ago

OK, I found what the problem is. At the moment latexdiff is run, the temporary files are still open, and the last part of the write is still in the buffer rather than in the filesystem. There is probably some OS dependence in the way the buffer operates, which would explain why you did not see this. The fix is easy. in latexdiffcite.py in routine write_tex_to_temp force the buffer to flush:

61> diff latexdiffcite.py.orig latexdiffcite.py
802c802,804
<     getattr(Files, 'tex_' + oldnew + '_tmp_hndl').write(getattr(FileContents, 'tex_' + oldnew).encode('utf-8'))
---
>     fh=getattr(Files, 'tex_' + oldnew + '_tmp_hndl')
>     fh.write(getattr(FileContents, 'tex_' + oldnew).encode('utf-8'))
>     fh.flush()

(sorry for not submitting this as a proper pull request, but I just installed latexdiffcite via pip, and have not cloned or forked the git repository - I hope that you can add to master in spite of not developing latexdiffcite any further)

cmeeren commented 7 years ago

New version released, try now :)

ftilmann commented 7 years ago

Thanks for the fast response, yes this works now.