使用 latexmk 时,如果参考书目文件不在主文件目录中,则 Bibtex 无法找到该文件

使用 latexmk 时,如果参考书目文件不在主文件目录中,则 Bibtex 无法找到该文件

~/Documents/test我有一个包含以下内容的项目目录......

test
|- refs
|- paper
|-|- main.tex

主要内容是……

\documentclass{article}
\usepackage[superscript,nomove]{cite}

\begin{document}

Hello world\cite{texbook}


\clearpage
\bibliographystyle{unsrt}
\bibliography{../refs}

\end{document}

refs.bib文件是...

@book{texbook,
  author = {Donald E. Knuth},
  year = {1986},
  title = {The {\TeX} Book},
  publisher = {Addison-Wesley Professional}
}

我的.latexmkrc是...

$pdf_mode = 1;
$pdflatex = 'pdflatex --shell-escape -interaction=nonstopmode'; 
$emulate_aux = 1;
$out_dir = '.';
$aux_dir = '/tmp/latexmk';

当我尝试编译时,引用显示为问号,而参考文献本身并未出现。输出是...

$ latexmk main.tex
Rc files read:
  /home/.latexmkrc
Latexmk: This is Latexmk, John Collins, 4 Apr. 2023. Version 4.80.
No existing .aux file, so I'll make a simple one, and require run of *latex.
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex':  Reasons for rerun
Category 'other':
  Rerun of 'pdflatex' forced or previously required

------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex --shell-escape -interaction=nonstopmode  -recorder -output-directory="/tmp/latexmk"  "main.tex"'
------------
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023) (preloaded format=pdflatex)
 \write18 enabled.
entering extended mode
(./main.tex
LaTeX2e <2023-06-01> patch level 1
L3 programming layer <2023-08-11>
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/article.cls
Document Class: article 2023/05/17 v1.4n Standard LaTeX document class
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2023/texmf-dist/tex/latex/cite/cite.sty)
(/usr/local/texlive/2023/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
(/tmp/latexmk/main.aux)

LaTeX Warning: Citation `texbook' on page 1 undefined on input line 6.

[1{/usr/local/texlive/2023/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
No file main.bbl.
(/tmp/latexmk/main.aux)

LaTeX Warning: There were undefined references.

 )</usr/local/texlive/2023/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx7.pfb>
</usr/local/texlive/2023/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on /tmp/latexmk/main.pdf (1 page, 20534 bytes).
Transcript written on /tmp/latexmk/main.log.
Latexmk: Moving '/tmp/latexmk/main.pdf' to 'main.pdf'
Latexmk: Getting log file '/tmp/latexmk/main.log'
Latexmk: Examining '/tmp/latexmk/main.fls'
Latexmk: Examining '/tmp/latexmk/main.log'
Latexmk: Missing bbl file '/tmp/latexmk/main.bbl' in following:
 No file main.bbl.
Latexmk: Log file says output to '/tmp/latexmk/main.pdf'
Latexmk: Found bibliography file(s):
  ../refs.bib
Latexmk: applying rule 'bibtex /tmp/latexmk/main'...
Rule 'bibtex /tmp/latexmk/main':  Reasons for rerun
Category 'other':
  Rerun of 'bibtex /tmp/latexmk/main' forced or previously required

------------
Run number 1 of rule 'bibtex /tmp/latexmk/main'
------------
Latexmk: Change directory to '/tmp/latexmk/'.
To assist finding of files in document directory, I set
  BIBINPUTS='/home/Documents/test/paper:/tmp/latexmk:'
  BSTINPUTS='/home/Documents/test/paper:'.
------------
Running 'bibtex  "main.aux"'
------------
This is BibTeX, Version 0.99d (TeX Live 2023)
The top-level auxiliary file: main.aux
The style file: unsrt.bst
I couldn't open database file ../refs.bib
---line 4 of file main.aux
 : \bibdata{../refs
 :                 }
I'm skipping whatever remains of this command
I found no database files---while reading file main.aux
Warning--I didn't find a database entry for "texbook"
(There were 2 error messages)
Latexmk: Change directory back to '/home/Documents/test/paper'
Latexmk: Summary of warnings from last run of *latex:
  Latex failed to resolve 1 citation(s)
Latexmk: ====List of undefined refs and citations:
  Citation `texbook' on page 1 undefined on input line 6
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  bibtex /tmp/latexmk/maint: Bibtex errors: See file '/tmp/latexmk/main.blg'

Latexmk: If appropriate, the -f option can be used to get latexmk
  to try to force complete processing.

refs.bib如果我将文件放在与 相同的目录中,则不会发生此问题main.tex。我该如何修复此问题?

答案1

罪魁祸首是-output-directory(在您的情况下,设置$aux_dir)与文件的相对路径的组合bib

以下是该latexmk手册的摘录:

辅助目录和输出目录的选择:辅助目录和输出目录通常作为文档目录的子目录给出,例如通过 -outdir=output。但是,可以提供绝对路径或相对于父目录的路径,例如“/tmp/foo”或“../output”。请注意,这通常会导致问题,尤其是 makeindex 或 bibtex。这是因为这些程序的现代版本默认在发现要求它们写入似乎不是当前工作目录或其子目录之一的目录中的文件时拒绝工作。这是整个 TeX 系统的安全措施的一部分,旨在防止恶意或错误的 TeX 文档错误地干扰用户的文件。

默认情况下,latexmk 会规避此问题:在运行 bibtex 和 makeindex 之前,latexmk 会将工作目录更改为 aux 目录,并适当设置搜索路径。此技巧的使用与否由变量 $bibtex_fudge 和 $makeindex_fudge 控制。不幸的是,此技巧有时会导致 bibtex 和 makeindex 无法找到文件。

如果需要,可以关闭该技巧。但这与辅助目录(如“/tmp/foo”或“../output”)不兼容。如果您确实必须处理这种情况,并且只有当您必须处理这种情况时,您才需要禁用安全措施(并承担任何风险)。这样做的一种方法是将操作系统环境变量 openout_any 临时设置为“a”(如“all”),以覆盖默认的“paranoid”设置。

首次运行 后latexmk,所有辅助文件将按照您的配置放置在 中/tmp/latexmk。接下来,latexmk尝试运行bibtex。为此,它将目录更改为/tmp/latexmk并设置BIBINPUTS为包含您的原始项目目录(main.tex找到 的位置)。但是,它随后遇到文件为 的规范../refs.bib。这完全否定了 的设置,BIBINPUTS因为使用相对路径规范,bibtex仅搜索指定路径,即/tmp/latexmk/../refs.bib,当然不存在。

这适用于\bibliography{refs}因为在这种形式下,bibtex将搜索(按顺序)列出的目录BIBINPUTS并将第一个匹配项作为要使用的正确文件。

解决方案

(以下任何一种都可以:按我的个人喜好顺序列出。)

  1. 不要使用-output-directory。它常常会造成比它本身更大的麻烦。
  2. 将您的refs.bib文件放在与您发现的相同的目录中main.tex
  3. 使用\bibliography{refs.bib}并添加~/Documents/test/到您的(如果您不想全局污染搜索路径,您可以通过添加特定于项目的设置BIBINPUTS来实现)latexmkrc@BIBINPUTS
  4. 使用绝对路径而不是相对路径:所以使用\bibliography{/home/<your username>/Documents/test/refs}

我不建议按照上面引用的文本中概述的那些会在您的机器上打开安全漏洞的步骤进行操作。

答案2

以下是@Willie Wong 的回答的补充

-bibtexfudge-您还可以通过向 latexmk 提供选项或通过放入$bibtex_fudge=0;latexmkrc 文件来解决问题。

这适用于 4.71 以上版本的 latexmk,至少适用于 TeXLive 2019 或更高版本中的 bibtex。不幸的是,关于这一点的 latexmk 文档已经过时了。在 v. 4.71 中,对输出(和辅助)目录的处理有一些改进,可以准确处理这个问题的主题,但我忘记了更新文档的必要性。

不过,一般来说,我更喜欢将输出目录(以及单独的辅助目录,如果使用的话)作为文档目录的子目录。将文档的所有相关文件放在一起更方便。(这是我通常的工作方式。)

相关内容