如何使用 latexdiff 比较 .bib 文件中的变化?

如何使用 latexdiff 比较 .bib 文件中的变化?

我读过几本弗雷德里克的答案latexdiff,但我仍然很难标记latexdiff出参考文献中的差异(即通过打印\printbibliography)。

假设我有两个.tex相应的.bib文件,它们都位于同一个文件夹中。

旧版

  • 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.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}
    }
    

使用latexdiff,我不仅想查看文件中的差异.tex,还想查看文件中的差异.bib。例如,作者姓名的变化(即,Hastie与交换Tibshirani),以及标题字段的变化(即,在标题中替换LearningMachine)。为清楚起见,以下是文件之间的变化.tex

  • old.tex
    • This is a simple citation \cite{hastieElementsStatisticalLearning2009}.
  • new.tex
    • This is an updated citation \cite{hastieElementsStatisticalLearning2009}.

这些是文件之间的变化.bib

  • 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},

为此,我在.tex.bib文件所在的根文件夹中运行了以下命令:

# 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

根据frederik 的回答,我现在应该latexdiff.bbl文件运行并生成一个包含和文件dif.bbl之间差异的文件。old.bibnew.bib

# Run `latexdiff` on the `.bbl` files.
latexdiff ./build/old.bbl ./build/new.bbl > ./build/diff.bbl

然后,我可以继续运行latexdiff实际.tex文件来生成该diff.tex文件。

# Run `latexdiff` on the `.tex` files.
latexdiff old.tex new.tex > ./build/diff.tex

最后,现在我必须运行pdflatexdiff.tex生成.pdf

# Run `pdflatex` to produce the `.pdf`.
pdflatex -output-directory=build -interaction=nonstopmode diff.tex

这会产生有关未定义引用的警告:

LaTeX Warning: There were undefined references.


Package biblatex Warning: Please (re)run Biber on the file:
(biblatex)                diff
(biblatex)                and rerun LaTeX afterwards.

但是,虽然.pdf生成了,但是参考书目差异并没有被发现latexdiff,如下所示:

latexdiff 输出

如果我按照警告中的说明进行操作并重新运行biber,则创建的diff.bcf新内容将覆盖先前创建的内容(即通过命令)。diff.bbldiff.bbllatexdiff ./build/old.bbl ./build/new.bbl > ./build/diff.bbl

我发现frederik 的另一个回答建议使用--append-textcmd=field--flatten标志。因此我重试了以下操作,但不幸的是,结果与上述操作完全相同:

# 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

我还按照编译器输出的指令运行以下后续命令:

# 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

而且,同样没有任何变化,输出是一样的:

latexdiff 输出

你有什么想法吗?我该如何latexdiff标记参考部分以及引用本身?


更新(1)基于弗雷德里克的评论

1. 替换\printbibliography\bibliography

  • \printbibliography例如,我用\bibliography{old.bib}in替换了,old.tex但不起作用。文件编译并生成输出,尽管! LaTeX Error编译器输出中有一个。但是,引用和参考都没有创建。

2.替换\printbibliography内容.bbl

  • 这导致了致命错误,并且没有.pdf生成任何文件
  • 有很多! Undefined control sequence.消息(例如,针对\field\strng

3. 添加--append-textcmd=field选项

  • --append-textcmd=field在生成diff.bbl使用时添加了latexdiff,即:

    latexdiff --append-textcmd=field ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
    
  • --flatten从中删除了该选项latexdiff old.tex new.tex > ./build/diff.tex,即:

    latexdiff old.tex new.tex > ./build/diff.tex
    
  • 因此,现在运行的命令序列是:

    # 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
    
    # Create the `diff.bbl` file.
    latexdiff --append-textcmd=field ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
    
    # Run `latexdiff` on the `.tex` files with the options removed.
    latexdiff old.tex new.tex > ./build/diff.tex
    
    # Run `pdflatex` to produce the `.pdf`.
    pdflatex -output-directory=build -interaction=nonstopmode diff.tex
    

这已经是一个进步了,因为现在参考书目中的改动已经被采纳了:

latexdiff 改进输出

编译器输出仍然建议重新运行biberdiff.bcf然后执行,但这样做会删除文件中pdflatex添加的所有命令。latexdiffdiff.bbl

唯一没有被注意到的是作者姓名的变化。也许--append-textcmd=field应该调整选项以包括除 之外的其他内容\field


更新(2)

看来调整选项--append-textcmd可以解决问题。例如,调整它以包含名称:

latexdiff --append-textcmd="field,name" ./build/old.bbl ./build/new.bbl > ./build/diff.bbl

产生以下内容:

latexdiff 所需输出

答案1

old.tex假设使用问题中提供的和文件进行设置new.tex,以下命令将在build目录中产生所需的输出。

  • 创建build目录

    mkdir ./build
    
  • pdflatex在每个文件上运行.tex以生成.bcf文件

    pdflatex -output-directory=build -interaction=nonstopmode old.tex
    pdflatex -output-directory=build -interaction=nonstopmode new.tex
    
  • biber在每个文件上运行.bcf以生成.bbl文件

    biber --output-directory=build --input-directory=build old.bcf
    biber --output-directory=build --input-directory=build new.bcf
    
  • 运行latexdiff文件.bbl来标记更改

    • 笔记。选项--append-textcmd在这里至关重要,因为latexdiff我们要知道我们感兴趣的是哪种更改。我们可以查看其中一个.bbl文件的内部,了解每条信息的存储方式。在上面的问题中,更改在标题(即\name{author})和作者(即\field{title})中。因此,我们可以将这两个添加到--append-textcmd--append-textcmd="field,name"
    latexdiff --append-textcmd="field,name" ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
    
  • 运行latexdiff文件.tex以生成.tex包含更改的文件

    • 笔记。如果我正确理解了 的文档,那么在下面的命令中latexdiff添加 选项将使我们无需像上面那样对文件进行运行。使用此选项,将在输出中用 的内容(即动态生成)替换命令。但是,由于我们使用作为引文后端,因此我们有而不是。作为--flattenlatexdiff.bbllatexdiff\bibliography{...}diff.bbl.texbiber\printbibliography\bibliography{...}弗雷德里克上述注释中指出的latexdiff未配置为查找\printbibliography命令。原则上,我们仍然可以通过手动添加文件内容来创建“平面”文档,.bbl这个答案
    latexdiff old.tex new.tex > ./build/diff.tex
    
  • 最后,运行pdflatexdiff.tex产生.pdf

    pdflatex -output-directory=build -interaction=nonstopmode diff.tex
    

上述操作将导致如下所示的显著变化。

latexdiff 所需输出

相关内容