Is there a way to make latexdiff work with the 'subfiles' package ?

I use subfiles to include parts of the document from different .tex files. Latexdiff does not seem to mark changes in the subfiles.

The --flatten option does not help. Latexdiff version is 1.0.2.

Example :






latexdiff d1/main.tex d2/main.tex --flatten > mydiff.tex

the resulting document simply does not include the contents of the subfile.


%DIF (...)

So yes, the problem lies with the flatten pipeline/workflow, which does not seem to be made to work with \subfile{includeme.tex} includes.

  • You could try some of the flatten alternatives discussed here: tex.stackexchange.com/questions/21838/… – Jörg Mar 26 '14 at 10:51
  • Could you add a MWE or at least a snippet of code that demonstrates how subfiles includes subsidiary files. – frederik Mar 26 '14 at 11:14
  • @f-tilman Added the MWE – mfit Mar 26 '14 at 14:23
  • Well, latexdiff does not know that it should expand the \subfile argument. You can adapt some of the scripts mentioned in the link before to use \subfile in a similar way as \input, but as your example of includeme.tex includes a documentclass I think it's not going to be compilable. – Jörg Mar 26 '14 at 14:28
  • 1
    For posterity: at least as of latexdiff version 1.1.1, using the --flatten option worked for me with subfiles. It took a little tweaking of the file-structure to get it to work, then I had to manually modify the resulting mydiff.tex slightly to get it to build into a PDF... but it worked. – DilithiumMatrix Jun 1 '17 at 12:58

Note that the following is a work-around rather than a full solution:

latexdiff --append-safecmd=subfile d1/main.tex d2/main.tex --flatten > mydiff.tex

will take care of the cases where a \subfile command has been added or removed from the file, and the whole block is marked up (only tested on the MWE, would need to be confirmed for longer included material), or the filename of included file changes. You would still need to copy the included files into the directory where the difference file is generated (current directory in the MWE).

To highlight content changes to the subfiles, you can process each file separately

cat /dev/null > null latexdiff -pnull d1/includeme.tex d2/includeme.tex > includeme.tex

The -p option forces latexdiff to omit the preamble commands that it normally inserts automatically when it finds a \begin{document} (auxiliary file "null" is needed as -p/dev/null is not recognised due to a bug in latexdiff).

Now all that remains is to automate this. The following line is a hacky way to achieve some automation as proof-of-concept but would really need to be expanded into a more robust and flexible small shell script:

grep -v '^%' main.tex | grep subfile\{ | sed 's/^.*subfile{\(.*\)}.*$/\1/' \ | awk '{ print "latexdiff -pnull d1/" $1, "d2/" $1,">", $1 }' | sh

  • I have noted the OP question as a feature request and eventually the --flatten option of latexdiff might be updated to deal with this 'natively'. The 'null' file workaround will probably not be necessary from version 1.0.4 onwards (not yet released at the time of writing) – frederik Mar 26 '14 at 21:30
  • Thanks! This is an actual workaround. Although I have to copy / reference some other files not in the MWE (style, bib, ..), to make the aggregated differences-tex work, this is probably the best/easiest solution for now. (If latexdiff could do it natively, of course that would be nice, too.Thanks for latexdiff as a whole, btw :-) – mfit Mar 30 '14 at 8:10
  • 1
    Note that latexdiff version 1.0.4 is now released and --flatten supports \subfile out of the box – frederik Nov 4 '14 at 8:18
  • 1
    @frederik It doesn't work if the subfile is in a subdirectory using \def\input@path{{./tex/} {./} {../}}. – Avi Ginsburg Dec 30 '15 at 10:10
  • 1
    @frederik Also doesn't work with \subfile{"Some File Name"}. Only tested on WIndows. – Avi Ginsburg Dec 30 '15 at 10:42

2 years after the question was asked, but I ended up writing a batch file for solving this in a windows environment:

@echo off

set "old_path=..\..\tags\old_version\my_folder\"
set "new_path=..\..\..\trunk\my_folder\"
set "doc_name=my_file.tex"

latexdiff --flatten %doc_name% %doc_name% > flat.tex
cd %old_path%
latexdiff --flatten %doc_name% %doc_name% > flat.tex
cd %new_path%
latexdiff --flatten %old_path%flat.tex flat.tex > diff.tex

rm flat.tex
rm %old_path%flat.tex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.