
Я хочу сделать ссылку между двумя файлами, скажем 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
Вам следует загрузить xr-hyper перед hyperref. Кроме того: с 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}