23

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.

6
  • You could try some of the flatten alternatives discussed here: tex.stackexchange.com/questions/21838/…
    – Jörg
    Commented Mar 26, 2014 at 10:51
  • Could you add a MWE or at least a snippet of code that demonstrates how subfiles includes subsidiary files.
    – frederik
    Commented Mar 26, 2014 at 11:14
  • @f-tilman Added the MWE
    – mfit
    Commented Mar 26, 2014 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
    Commented Mar 26, 2014 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. Commented Jun 1, 2017 at 12:58

3 Answers 3

10

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

5
  • 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
    Commented Mar 26, 2014 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
    Commented Mar 30, 2014 at 8:10
  • 1
    Note that latexdiff version 1.0.4 is now released and --flatten supports \subfile out of the box
    – frederik
    Commented Nov 4, 2014 at 8:18
  • 1
    @frederik It doesn't work if the subfile is in a subdirectory using \def\input@path{{./tex/} {./} {../}}. Commented Dec 30, 2015 at 10:10
  • 1
    @frederik Also doesn't work with \subfile{"Some File Name"}. Only tested on WIndows. Commented Dec 30, 2015 at 10:42
6

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
1

Post above link was very helpful, but I made my modified version that you can use.

@echo off
setlocal

set "old_path=..\documentation\"
set "new_path=..\documentation_old\"
set "doc_name_filename=main"

echo Generate %doc_name_filename%_flat.tex for %new_path%
cd %new_path%
latexpand %doc_name_filename%.tex > %doc_name_filename%_flat.tex

echo Generate %doc_name_filename%_flat.tex for %old_path%
cd %old_path%
latexpand %doc_name_filename%.tex > %doc_name_filename%_flat.tex

echo Generate diff
cd %new_path%
latexdiff %old_path%%doc_name_filename%_flat.tex %doc_name_filename%_flat.tex > diff.tex
pdflatex  --max-print-line=10000 -shell-escape -synctex=1 -interaction=nonstopmode -file-line-error -recorder  diff.tex 2>&1 > NUL
echo PDF generated in case of problems see diff.log

echo Cleaning up
del %doc_name_filename%_flat.tex
del %old_path%%doc_name_filename%_flat.tex

pause
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .