`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

您應該在 hyperref 之前加載 xr-hyper。除此之外:對於 xr-hyper,預設情況下顏色是不同的,當您將 更改linkcolor為藍色以及與 非常相似的顏色時,您只是看不到它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}

相關內容