# Latexdiff with subfiles

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 :

main.tex

\documentclass[10pt]{article}
\usepackage{subfiles}
\begin{document}
\subfile{includeme.tex}
\end{document}


includeme.tex

\documentclass[main.tex]{subfiles}
\begin{document}
Text!
\end{document}


Running

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


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

mydiff.tex

\documentclass[10pt]{article}
%DIF LATEXDIFF DIFFERENCE FILE
%DIF (...)
%DIF END PREAMBLE EXTENSION ADDED BY LATEXDIFF
\begin{document}
\subfile{includeme.tex}
\end{document}


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 – zm0 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
• @jorg yes the script would need to strip some parts of the included file, I guess. I was thinking about adding something along that lines to the python version, but not sure if I actually get around to do that. – zm0 Mar 30 '14 at 8:14

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 :-) – zm0 Mar 30 '14 at 8:10
• 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
• @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
• @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
setlocal

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