I read several of frederik's answers on latexdiff, but I still struggle to get latexdiff to mark differences for references (i.e., printed via \printbibliography).
Suppose I have two .tex and corresponding .bib files, all residing in the same folder.
Old version
old.tex:\documentclass{article} \usepackage[american]{babel} \usepackage[style = apa, backend = biber]{biblatex} \usepackage{csquotes}% Library settings. \DeclareLanguageMapping{american}{american-apa}
% Load the library. \addbibresource{old.bib}
% Main content. \begin{document}
This is a simple citation \cite{hastieElementsStatisticalLearning2009}.
% References. \printbibliography
\end{document}
old.bib:@book{hastieElementsStatisticalLearning2009, title = {The Elements of Statistical Learning: Data Mining, Inference, and Prediction}, author = {Hastie, Trevor and Tibshirani, Robert and Friedman, Jerome}, date = {2009}, edition = {2}, publisher = {Springer-Verlag}, url = {https://www.springer.com/gp/book/9780387848570} }
New version
new.tex:\documentclass{article} \usepackage[american]{babel} \usepackage[style = apa, backend = biber]{biblatex} \usepackage{csquotes}% Library settings. \DeclareLanguageMapping{american}{american-apa}
% Load the library. \addbibresource{new.bib}
% Main content. \begin{document}
This is an updated citation \cite{hastieElementsStatisticalLearning2009}.
% References. \printbibliography
\end{document}
new.bib:@book{hastieElementsStatisticalLearning2009, title = {The Elements of Machine Learning: Data Mining, Inference, and Prediction}, author = {Tibshirani, Trevor and Hastie, Robert and Friedman, Jerome}, date = {2009}, edition = {2}, publisher = {Springer-Verlag}, url = {https://www.springer.com/gp/book/9780387848570} }
Using latexdiff, I would like to see not only the differences in the .tex files, but also the differences in the .bib files. For instance, the change in author name (i.e., swapped Hastie with Tibshirani), and the change in the title field (i.e., replaced Learning with Machine in the title). For clarity, these are the changes between the .tex files:
old.tex:This is a simple citation \cite{hastieElementsStatisticalLearning2009}.
new.tex:This is an updated citation \cite{hastieElementsStatisticalLearning2009}.
And these are the changes between the .bib files:
old.bib:title = {The Elements of Statistical Learning: Data Mining, Inference, and Prediction},author = {Hastie, Trevor and Tibshirani, Robert and Friedman, Jerome},
new.bib:title = {The Elements of Machine Learning: Data Mining, Inference, and Prediction},author = {Tibshirani, Trevor and Hastie, Robert and Friedman, Jerome},
To do this, I ran the following commands in the root folder where the .tex and .bib files are located:
# Create a `build` directory.
mkdir ./build
Run pdflatex on the .tex files to generate .bcf files.
pdflatex -output-directory=build -interaction=nonstopmode old.tex
pdflatex -output-directory=build -interaction=nonstopmode new.tex
Run biber on the .bcf files to generate .bbl files.
biber --output-directory=build --input-directory=build old.bcf
biber --output-directory=build --input-directory=build new.bcf
According to frederik's answer, I should now run latexdiff on the .bbl files and produce a dif.bbl files with the differences between the old.bib and new.bib files.
# Run `latexdiff` on the `.bbl` files.
latexdiff ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
Then, I can go ahead and run latexdiff on the actual .tex files to produce the diff.tex file.
# Run `latexdiff` on the `.tex` files.
latexdiff old.tex new.tex > ./build/diff.tex
Finally, now I have to run pdflatex on the diff.tex to generate the .pdf
# Run `pdflatex` to produce the `.pdf`.
pdflatex -output-directory=build -interaction=nonstopmode diff.tex
This produces a warning regarding undefined references:
LaTeX Warning: There were undefined references.
Package biblatex Warning: Please (re)run Biber on the file:
(biblatex) diff
(biblatex) and rerun LaTeX afterwards.
However, the .pdf is generated, but the bibliography differences are not being picked up by latexdiff, as it can be seen below:
If I follow the instructions in the warning and re-run biber on diff.bcf, the new diff.bbl that gets created overrides the previously created diff.bbl (i.e., via the command latexdiff ./build/old.bbl ./build/new.bbl > ./build/diff.bbl).
I found another answer by frederik where it is recommended to use --append-textcmd=field and --flatten flags. So I retried the following, which sadly produces exactly the same thing as above:
# Remove directory.
rm -rf ./build/
Create a build directory.
mkdir ./build
Run pdflatex on the .tex files to generate .bcf files.
pdflatex -output-directory=build -interaction=nonstopmode old.tex
pdflatex -output-directory=build -interaction=nonstopmode new.tex
Run biber on the .bcf files to generate .bbl files.
biber --output-directory=build --input-directory=build old.bcf
biber --output-directory=build --input-directory=build new.bcf
Run latexdiff on the .bbl files.
latexdiff ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
Run latexdiff on the .tex files with the recommended flags.
latexdiff --append-textcmd=field --flatten old.tex new.tex > ./build/diff.tex
Run pdflatex to produce the .pdf.
pdflatex -output-directory=build -interaction=nonstopmode diff.tex
I also followed the instruction from the compiler output and run the following subsequent commands:
# Run `biber` on the `diff.bcf` to update the `diff.bbl`.
biber --output-directory=build --input-directory=build diff.bcf
Re-run pdflatex to re-generate diff.pdf.
pdflatex -output-directory=build -interaction=nonstopmode diff.tex
And, again nothing changes, the output is the same:
Do you have any ideas oh how I can get latexdiff to mark differences in the References section, as well as in the citation itself?
Update (1) based on frederik's comments
1. Replacing \printbibliography with \bibliography:
- I replaced
\printbibliographywith, for example,\bibliography{old.bib}inold.texand it did not work. The file compiled and produced the output, although there was a! LaTeX Errorin the compiler output. However, neither the citations, nor the references were created.
2. Replacing \printbibliography with the .bbl contents
- this resulted in a fatal error, and no
.pdffile was produced - there were a lot of
! Undefined control sequence.messages (e.g., for\field, or\strng)
3. Adding the --append-textcmd=field option
added the
--append-textcmd=fieldwhen generating thediff.bblusinglatexdiff, i.e.:latexdiff --append-textcmd=field ./build/old.bbl ./build/new.bbl > ./build/diff.bblremoved the
--flattenoption from thelatexdiff old.tex new.tex > ./build/diff.tex, i.e.:latexdiff old.tex new.tex > ./build/diff.texso, the sequence of commands ran now is:
# Remove directory. rm -rf ./build/Create a
builddirectory.mkdir ./build
Run
pdflatexon the.texfiles to generate.bcffiles.pdflatex -output-directory=build -interaction=nonstopmode old.tex pdflatex -output-directory=build -interaction=nonstopmode new.tex
Run
biberon the.bcffiles to generate.bblfiles.biber --output-directory=build --input-directory=build old.bcf biber --output-directory=build --input-directory=build new.bcf
Create the
diff.bblfile.latexdiff --append-textcmd=field ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
Run
latexdiffon the.texfiles with the options removed.latexdiff old.tex new.tex > ./build/diff.tex
Run
pdflatexto produce the.pdf.pdflatex -output-directory=build -interaction=nonstopmode diff.tex
And this is already an improvement because now the changes in the bibliography are picked up:
The compiler output still recommends to rerun biber on diff.bcf followed by pdflatex, but doing that removes all the commands added by latexdiff in the diff.bbl file.
The only thing not being picked up is the change in the author name. Perhaps the option --append-textcmd=field should be adjusted to include something else other than \field?
Update (2)
It seems that adjusting the option --append-textcmd does the trick. For example, adjusting it to also include the name:
latexdiff --append-textcmd="field,name" ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
Produces the following:



\printbibliography, rather it looks for\bibliographycommands. Can you replace\printbibliographywith\bibliography{...}? If this does not work can you try, for testing purposes, to manually replace\printbibliographywith content of diff.bbl file, and see if it compiles (it would be straightforward to adopt latexdiff to recognise\printbibliography) (but see next comment) – frederik Dec 10 '21 at 14:52--append-textcmd=fieldin the command to generate the diff.bbl file, i.elatexdiff --append-textcmd=field ./build/old.bbl ./build/new.bbl > ./build/diff.bbl– frederik Dec 10 '21 at 14:58--flattenyou do not need to generate diff.bbl, by the way, it will be ignored. – frederik Dec 10 '21 at 15:00old.bbl',new.bblanddiff.bbl`. – frederik Dec 10 '21 at 15:01.bblfiles? – Mihai Dec 10 '21 at 17:38nameto the--append-textcmdoption (i.e.,--append-textcmd="field,name"). Now it works flawlessly (i.e., see Update (2) in my question). I will go ahead and create an answer based on your suggestions for others that may be interested. Thanks a lot andlatexdiffis beautiful! – Mihai Dec 10 '21 at 19:06—flattennot recognizing\printbibliography. Have you seen this? I think this might help: https://tex.stackexchange.com/a/12185/134807. – Mihai Dec 10 '21 at 22:19\bibliographycommand effectively does. So implementing this in the--flattenoption is not as easy as I first thought. Your link in the last comment indeed shows a viable way to implement this but there are more urgent issues, and I will not tackle anytime soon. – frederik Dec 11 '21 at 11:56--flattenis correct in the answer. As for implementing it, I totally understand there are more urgent things on the list. I am very happy withlatexdiffand I even got it to work withgitvialatexdiff-vc. It is an amazing tool! – Mihai Dec 11 '21 at 15:54