我读过几本弗雷德里克的答案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
),以及标题字段的变化(即,在标题中替换Learning
为Machine
)。为清楚起见,以下是文件之间的变化.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.bib
new.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
最后,现在我必须运行pdflatex
来diff.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
,如下所示:
如果我按照警告中的说明进行操作并重新运行biber
,则创建的diff.bcf
新内容将覆盖先前创建的内容(即通过命令)。diff.bbl
diff.bbl
latexdiff ./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
标记参考部分以及引用本身?
更新(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
这已经是一个进步了,因为现在参考书目中的改动已经被采纳了:
编译器输出仍然建议重新运行biber
,diff.bcf
然后执行,但这样做会删除文件中pdflatex
添加的所有命令。latexdiff
diff.bbl
唯一没有被注意到的是作者姓名的变化。也许--append-textcmd=field
应该调整选项以包括除 之外的其他内容\field
?
更新(2)
看来调整选项--append-textcmd
可以解决问题。例如,调整它以包含名称:
latexdiff --append-textcmd="field,name" ./build/old.bbl ./build/new.bbl > ./build/diff.bbl
产生以下内容:
答案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
添加 选项将使我们无需像上面那样对文件进行运行。使用此选项,将在输出中用 的内容(即动态生成)替换命令。但是,由于我们使用作为引文后端,因此我们有而不是。作为--flatten
latexdiff
.bbl
latexdiff
\bibliography{...}
diff.bbl
.tex
biber
\printbibliography
\bibliography{...}
弗雷德里克上述注释中指出的latexdiff
未配置为查找\printbibliography
命令。原则上,我们仍然可以通过手动添加文件内容来创建“平面”文档,.bbl
如这个答案。
latexdiff old.tex new.tex > ./build/diff.tex
- 笔记。如果我正确理解了 的文档,那么在下面的命令中
最后,运行
pdflatex
以diff.tex
产生.pdf
pdflatex -output-directory=build -interaction=nonstopmode diff.tex
上述操作将导致如下所示的显著变化。