我正在写一篇有大量附录的文章,我们需要一个交叉引用系统。由于我们使用了 hyperref,我尝试了 xr-hyper 包。经过多次尝试,我终于让它与三个外部文档一起工作,但当我添加第四个文档时,我收到错误消息“包 xr 警告:没有文件 file4.aux,输入行 30 上的标签未导入”。
事情是这样的:我们正在使用 Overleaf(和 pdflatex)。遗憾的是,目前无法在本地执行此操作。
我读了所有我能找到的帖子,应用了所有我能找到的工作示例,但无济于事。
我附上了以下各个页面的 MWE。有人能弄清楚它为什么不接受对第四个文件的引用吗?
% the preamble
%\usepackage{xcite}
\usepackage{xr-hyper}
\usepackage{hyperref}
\makeatletter
\newcommand*{\addFileDependency}[1]{% argument=file name and extension
\typeout{(#1)}% latexmk will find this if $recorder=0 (however, in that case, it will ignore #1 if it is a .aux or .pdf file etc and it exists! if it doesn't exist, it will appear in the list of dependents regardless)
\@addtofilelist{#1}% if you want it to appear in \listfiles, not really necessary and latexmk doesn't use this
\IfFileExists{#1}{}{\typeout{No file #1.}}% latexmk will find this message if #1 doesn't exist (yet)
}
\makeatother
\newcommand*{\myexternaldocument}[1]{%
\externaldocument{#1}%
\addFileDependency{#1.tex}%
\addFileDependency{#1.aux}%
}
% put all the external documents here!
\myexternaldocument{file1}
\myexternaldocument{file2}
\myexternaldocument{file3}
\myexternaldocument{file4}
% the latexmkrc file
add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );
sub makeexternaldocument {
# if the dependency isn't one of the files that this latexmk run will consider, process it
# without this test, we would get an infinite loop!
if (!($root_filename eq $_[0]))
{ # PLEASE ENABLE ONLY ONE OF THE FOLLOWING
# DEPENDING ON THE ENGINE YOU'RE USING
# FOR PDFLATEX
system( "latexmk -pdf \"$_[0]\"" );
# FOR LATEX+DVIPDF
# system( "latexmk \"$_[0]\"" );
# FOR XELATEX
# system( "latexmk -xelatex \"$_[0]\"" );
# FOR LUALATEX
# system( "latexmk -lualatex \"$_[0]\"" );
}
}
% File 1
\documentclass[12pt]{article}
\input{preamble}
\begin{document}
\section{section 1 in file 1}
\label{sec1file1}
\section{section 2 in file 1}
\label{sec2file1}
Testing some references: \\
Referencing section one in file one: \ref{sec1file1}\\
Referencing section two in file one: \ref{sec2file1} \\
Referencing section one in file two: \ref{sec1file2} \\
Referencing section two in file two: \ref{sec2file2} \\
Referencing section one in file three: \ref{sec1file3} \\
Referencing section two in file three: \ref{sec2file3} \\
\end{document}
最后,给出其余文件的示例:
\documentclass[12pt]{article}
\input{preamble}
\begin{document}
\section{section 1 in file 3}
\label{sec1file3}
\section{section 2 in file 3}
\label{sec2file3}
Testing some references: \\
Referencing section one in file one: \ref{sec1file1}\\
Referencing section two in file one: \ref{sec2file1} \\
Referencing section one in file two: \ref{sec1file2} \\
Referencing section two in file two: \ref{sec2file2} \\
Referencing section one in file three: \ref{sec1file3} \\
Referencing section two in file three: \ref{sec2file3} \\
Referencing section two in file four: \ref{sec2file4} \\
\end{document}
更新:我现在从 Overleaf 下载了辅助文件,它给了我这个结果:
\relax
\providecommand\hyper@newdestlabel[2]{}
\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
\global\let\oldcontentsline\contentsline
\gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}}
\global\let\oldnewlabel\newlabel
\gdef\newlabel#1#2{\newlabelxx{#1}#2}
\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
\AtEndDocument{\ifx\hyper@anchor\@undefined
\let\contentsline\oldcontentsline
\let\newlabel\oldnewlabel
\fi}
\fi}
\global\let\hyper@last\relax
\gdef\HyperFirstAtBeginDocument#1{#1}
\providecommand\HyField@AuxAddToFields[1]{}
\providecommand\HyField@AuxAddToCoFields[2]{}
\bibstyle{biblatex}
\bibdata{output-blx,hh_library}
\citation{biblatex-control}
\abx@aux@refcontext{nyt/global//global/global}
\providecommand \oddpage@label [2]{}
\@writefile{toc}{\boolfalse {citerequest}\boolfalse {citetracker}\boolfalse {pagetracker}\boolfalse {backtracker}\relax }
\@writefile{lof}{\boolfalse {citerequest}\boolfalse {citetracker}\boolfalse {pagetracker}\boolfalse {backtracker}\relax }
\@writefile{lot}{\boolfalse {citerequest}\boolfalse {citetracker}\boolfalse {pagetracker}\boolfalse {backtracker}\relax }
\@writefile{toc}{\defcounter {refsection}{0}\relax }\@writefile{toc}{\contentsline {section}{\numberline {1}section 1 in file 1}{1}{section.1}\protected@file@percent }
\newlabel{sec1file1}{{1}{1}{section 1 in file 1}{section.1}{}}
\@writefile{toc}{\defcounter {refsection}{0}\relax }\@writefile{toc}{\contentsline {section}{\numberline {2}section 2 in file 1}{1}{section.2}\protected@file@percent }
\newlabel{sec2file1}{{2}{1}{section 2 in file 1}{section.2}{}}
\newlabel{LastPage}{{}{1}{}{page.1}{}}
\xdef\lastpage@lastpage{1}
\xdef\lastpage@lastpageHy{1}
这些帮助有用?
答案1
查看本教程:https://www.overleaf.com/learn/how-to/Cross_referencing_with_the_xr_package_in_Overleaf
假设您有File1.tex
并且File2.tex
想要在 File1 中引用 File2 中的标签,您需要做的是:
- 自定义导入代码
File1.tex
%%% HELPER CODE FOR DEALING WITH EXTERNAL REFERENCES
\usepackage{xr}
\makeatletter
\newcommand*{\addFileDependency}[1]{
\typeout{(#1)}
\@addtofilelist{#1}
\IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother
\newcommand*{\myexternaldocument}[1]{
\externaldocument{#1}
\addFileDependency{#1.tex}
\addFileDependency{#1.aux}
}
%%% END HELPER CODE
% put all the external documents here!
\myexternaldocument{File2}
latexmkrc
文件
add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );
sub makeexternaldocument {
if (!($root_filename eq $_[0]))
{
# FOR PDFLATEX
system( "latexmk -cd -pdf \"$_[0]\"" );
# FOR LATEX+DVIPDF
# system( "latexmk -cd \"$_[0]\"" );
# FOR XELATEX
# system( "latexmk -cd -xelatex \"$_[0]\"" );
# FOR LUALATEX
# system( "latexmk -cd -lualatex \"$_[0]\"" );
}
}
还要注意,该latexmkrc
文件必须位于根(顶级)文件夹中,而不是子文件夹内。
答案2
我也遇到了这个问题(教程不起作用)。我发现对我来说解决方案就是将我的主文档和外部文档移动到我的 overleaf 项目的根目录。