`xr` 交叉引用:仅更改外部引用的颜色

`xr` 交叉引用:仅更改外部引用的颜色

我想在两个文件之间建立引用,比如 Paper1 和 Paper2。我hyperref在每个文档中都使用内部引用。为了明确哪些引用是内部的,哪些是外部的,我想更改xr引用的颜色。

在 中hypersetup,我选择了linkcolor={blue}。本质上,我希望能够说

if reference is external, then set linkcolor={red}
if reference is internal, then set linkcolor={blue}

我在 Overleaf 中建立了一个示例,可以在这里找到:https://www.overleaf.com/4688282852jxtxcsdmnrhs。文件副本如下(抱歉,它们太长了!)

(Overleaf 中有一些额外的“辅助代码”,我认为如果在本地编译的话就不需要了;请参阅https://www.overleaf.com/learn/how-to/Cross_referencing_with_the_xr_package_in_Overleaf

%%File1.tex

\documentclass{article}

\usepackage{hyperref}
\hypersetup{
    colorlinks,
    linkcolor={blue},
    citecolor={blue},
    urlcolor={blue}
}

%%% HELPER CODE FOR DEALING WITH EXTERNAL REFERENCES
\usepackage{xr-hyper}
\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}

\title{\texttt{xr} package example}
\author{Overleaf team}

\begin{document}
\maketitle
We would like to reference section \ref{label1} of file2.tex
\end{document}

%%%%%%%%%%%%%

%%File2.tex

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{Label1}
\label{label1}
This section is referenced by File1.
\end{document}

%%%%%%%%%%%%%

%%latexmkrc

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]\"" );
   }
}

答案1

linkcolor您应该在 hyperref 之前加载 xr-hyper。除此之外:使用 xr-hyper 时,颜色默认不同,您只是在将 更改为蓝色并更改为与 非常相似的颜色时才看不到它filecolor

\documentclass{article}

\usepackage{xr-hyper}
\usepackage[colorlinks]{hyperref}
\hypersetup{
    colorlinks,
    linkcolor={blue},
    filecolor={red}, %<----
    urlcolor={blue},
    citecolor={blue}
}


\externaldocument[ex:]{external}

\begin{document}

\section{My section}\label{sec:mysection}

Local ref: \ref{sec:mysection}, external ref:  \ref{ex:sec:mytitle} 

\end{document}

在此处输入图片描述

%external.tex
\documentclass{article}

\usepackage{xr-hyper}
\usepackage{hyperref}

\begin{document}

\section{MyTitle}\label{sec:mytitle}

\end{document}

相关内容