Добавление номера страницы в команду перекрестной ссылки

Добавление номера страницы в команду перекрестной ссылки

в "выделение внешних ссылок"

Я нашел новую команду, написанную ниже, очень полезной. Хотя я хотел бы добавить номер страницы во внешний файл в команде \myautoref, и не могу найти никаких тем на форумах.

Может ли кто-нибудь помочь с добавлением ссылки на номер страницы?

\documentclass{article} 
\usepackage{xcolor}
\usepackage{xr-hyper}

\usepackage{hyperref} 

\externaldocument[A-]{extfile}  %(A)
\makeatletter

\newcommand{\myautoref}[2][A-]{%
  \@ifundefined{r@#1#2}{% Nope A-#2 isn't there
    \@ifundefined{r@#2}{%
    }{%
      \autoref{#2}%
    }%
  }{%
    {\pending{#1#2}}%
  }%
}
\newcommand{\pending}[1]{\color{red}\autoref{#1} from "file.tex"}

\makeatother


\begin{document}
Using \myautoref{sec:vis} and \myautoref{sec:vis}, but \myautoref{localsection}

\section{Local section}\label{localsection}
\end{document}

extfile.tex

\documentclass{article}

\begin{document}
\section{A section} \label{sec:vis}
\end{document}

введите описание изображения здесь

решение1

Я предлагаю вам использовать макрос \pagerefвнутри вашего определения \pending.

Кстати, если вы хотите использовать \autorefдля перекрестных ссылок элементы, расположенные во внешних скомпилированных файлах, вы должны загрузить hyperrefвнешние файлы. Таким образом, extfile.texдолжно выглядеть так:

\documentclass{article}
\usepackage{hyperref}  % <- new
\begin{document}
\section{A section} \label{sec:vis}
\end{document}

Основной файл тогда может выглядеть так:

\documentclass{article} 

\usepackage{xr-hyper}
\externaldocument[A-]{extfile}

\usepackage{xcolor}
\usepackage[colorlinks,allcolors=red]{hyperref} 

\makeatletter
\newcommand{\myautoref}[2][A-]{%
  \@ifundefined{r@#1#2}{% Nope A-#2 isn't there
    \@ifundefined{r@#2}{}{\autoref{#2}}%
  }{{\pending{#1#2}}}}
\makeatother
\newcommand{\pending}[1]{\color{red}\autoref{#1} on page~\pageref{#1} of ``file.tex''}

\begin{document}
Using \myautoref{sec:vis}, but \autoref{localsection}.

\section{Local section} \label{localsection}
\end{document}

введите описание изображения здесь

решение2

Как уже предлагалось: \pagerefили в расширяемом контексте: \getpagerefnumber(требуется refcount) пакет.

\documentclass{article} 
\usepackage{refcount}
\usepackage{xcolor}
\usepackage{xr-hyper}

\externaldocument[A-]{extfile}  %(A)

\usepackage{hyperref} 

\makeatletter

\newcommand{\myautoref}[2][A-]{%
  \@ifundefined{r@#1#2}{% Nope A-#2 isn't there
    \@ifundefined{r@#2}{%
    }{%
      \autoref{#2} on page \pageref{#2}%
    }%
  }{%
    {\pending{#1#2}}%
  }%
}
\newcommand{\pending}[1]{\color{red}\autoref{#1} from "file.tex" on page \getpagerefnumber{#1}}

\makeatother


\begin{document}
Using \myautoref{sec:vis} and \myautoref{sec:vis}, but \myautoref{localsection}

\section{Local section}\label{localsection}
\end{document}

Во избежание autorefжалоб \usepackage{hyperref}рекомендуется добавить во внешний файл:

\documentclass{article}
\usepackage{hyperref}
\usepackage{blindtext}
\begin{document}
\blindtext[5]
\section{A section} \label{sec:vis}
\end{document}

Связанный контент