[Tex/LaTex] Latexdiff with subfiles

latexdiffsubfiles

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.

Best Answer

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

Related Question