与 Overleaf 中的 xr 包进行交叉引用(以及自定义章节名称)

与 Overleaf 中的 xr 包进行交叉引用(以及自定义章节名称)

我跟着Overleaf 指南:不同 LaTeX 文档之间的交叉引用。这确实很有效 - 如果我\cref{eqn:supplement-equation1}在我的文档中使用main.tex它,它将正确地在我的supplement.tex文件中呈现对该方程的引用。

即使对于我定义了自定义命名方案的方程式也是如此:而不是“等式 1”, 我有“等式 SI-1”

\renewcommand{\theequation}{SI-\arabic{equation}}

但是,这对于在中定义的自定义部分名称不起作用supplement.tex。我遇到两种情况:

  1. 如果我定义节名称supplement.tex
\renewcommand{\thesection}{Unit \arabic{section}}

它们被正确引用main.tex“单元1”等。然而,这会打乱目录supplement.tex

在此处输入图片描述

  1. 如果我定义节名称supplement.tex
\makeatletter
\renewcommand{\@seccntformat}[1]{Unit \csname the#1\endcsname\quad}
\makeatother

它们被引用main.tex“第 1 节”等。至少supplement.tex现在的目录看起来不错:

在此处输入图片描述

我如何确保对我的supplement.tex文件中某个部分的引用被呈现为“单元1”在我的main.tex文件中,同时维护目录?

MWE(也可访问https://www.overleaf.com/read/xywgwfjgbxjt):

main.tex

\documentclass{article}
\usepackage[english]{babel}

% cross-referencing
% https://www.overleaf.com/learn/how-to/Cross_referencing_with_the_xr_package_in_Overleaf#Creating_the_latexmkrc_file
\usepackage{cleveref}
\input{preamble}
\myexternaldocument{supplement}

\begin{document}

A test cross-reference to an equation in the supplement: \cref{eqn:supplement1}. And to a section \cref{sec:sec1}.

\end{document}

preamble.tex

% https://www.overleaf.com/learn/how-to/Cross_referencing_with_the_xr_package_in_Overleaf

\usepackage{xr-hyper} % enable cross-references across different files, must be loaded before 'hyperref'

\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}%
}

supplement.tex

\documentclass{article}
\usepackage[english]{babel}

% custom section names
\makeatletter
\renewcommand{\@seccntformat}[1]{Unit \csname the#1\endcsname\quad}
\makeatother

% custom equation names
\renewcommand{\theequation}{SI-\arabic{equation}}

\title{Your Paper}
\author{You}

\begin{document}

\section{sec1}
\label{sec:sec1}

\begin{equation}
a = b
\label{eqn:supplement1}
\end{equation}

\end{document}

答案1

只有内部引用有效,使用 xr 的外部引用才能起作用。因此,首先要正确引用补充内容。您可以使用以下命令\labelformat更改引用方式:

\documentclass{article}
\usepackage[english]{babel}

% custom section names
\makeatletter
\renewcommand{\@seccntformat}[1]{Unit \csname the#1\endcsname\quad}
\labelformat{section}{Unit~#1}
\makeatother

% custom equation names
\renewcommand{\theequation}{SI-\arabic{equation}}

\title{Your Paper}
\author{You}

\usepackage{cleveref}

\begin{document}

\tableofcontents

\section{sec1}
\label{sec:sec1}

\begin{equation}
a = b
\label{eqn:supplement1}
\end{equation}


\cref{eqn:supplement1}, \ref{sec:sec1}, \cref{sec:sec1}

\end{document}

在此处输入图片描述

相关内容