While working on a multivolume book, I use a smart cross-referencing method as suggested at Smart cross-references between 2 volumes using Zref package. The \sref and \seqref commands described there add to the link, which are printed by the usual \ref and \eqref commands, the chapter number as the prefix "No." if the target (for example, an equation) is located in a different chapter No. If the object is located in the current chapter, the chapter number is not printed.
Everything was fine until I added an appendix. The fact is that the \appendix declaration resets the chapter counter to zero. Therefore, the chapter counter in the first chapter of the main text and in the first appendix has the same value 1. Hence, the \seqref command, which in the Appendix A points to an equation in chapter 1, prints the equation number without prefix "1." Conversely, the \seqref command, which in the first chapter points to an equation in Appendix A, prints the equation number without the prefix "A."
A minimal working example below is slightly modified from the cited above post. Separation of the main text and the code on two files is preserved although this is not obligatory for illustrating the above described problem. Common code is written in test2-zref-common.tex:
% Load packages and declare external docs.
\usepackage{xr-hyper}% <== load xr-hyper package
\usepackage[unicode,colorlinks]{hyperref}% <== load hyperref package
\usepackage[user]{zref}
\makeatletter
\zref@newprop{chaptervalue}[-1]{\the\value{chapter}}% <= note the default value [0]
%%\zref@newprop{chaptervalue}[0]{\thechapter}% <= note the default value [-1]
\zref@localaddprops{main}{chaptervalue}
\makeatother
% Reset default equation numbering
\usepackage{amsmath}
\numberwithin{equation}{chapter}
\renewcommand{\theequation}{\arabic{equation}}
% Define \sref and \seqref macros for smart cross-references of equations
\makeatletter
\newcommand\ifcurrentchapter[3]{%
\def\temp@a{\number\value{chapter}}%
%%\def\temp@a{\thechapter}%
\def\temp@b{\zref@extract{#1}{chaptervalue}}%
\ifnum\temp@a=\temp@b%
%%\if\temp@a\temp@b%
#2%
\else%
\ifnum\temp@b<0
%%\if\temp@b=0
#2%
\else
#3%
\fi%
\fi%
}
\newcommand{\sref}[1]{\hyperref[#1]{\hbox{\ifcurrentchapter{#1}{}{\zref[chaptervalue]{#1}.}\zref{#1}}}}
\newcommand{\seqref}[1]{(\sref{#1})}
\makeatother
% Define a macro to print test text
\newcommand{\testtext}{
Chapter \sref{ch:1}, Chapter \sref{ch:2}, Chapter \sref{ch:1A}.
\par\noindent
Compare \number\value{chapter} with \zref[chaptervalue]{ch:1}. Equal: \ifcurrentchapter{ch:1}{Yes}{No}
\par\noindent
Compare \number\value{chapter} with \zref[chaptervalue]{ch:2}. Equal: \ifcurrentchapter{ch:2}{Yes}{No}
\par\noindent
Compare \number\value{chapter} with \zref[chaptervalue]{ch:1A}. Equal: \ifcurrentchapter{ch:1A}{Yes}{No}
\par\noindent
\seqref{1.1}, \seqref{1.2}; \seqref{2.1}, \seqref{2.2}; \seqref{1A.1}, \seqref{1A.2}.
}
\endinput
Main file just reads the code file:
\documentclass[oneside]{book}
\input{test2-zref-common}
\begin{document}
\chapter{1 in Volume 1}\label{ch:1}\zlabel{ch:1}
\testtext
\begin{equation}
eq1.1
\label{1.1}\zlabel{1.1}
\end{equation}
\begin{equation}
eq1.2
\label{1.2}\zlabel{1.2}
\end{equation}
\chapter{2 in Volume 2}\label{ch:2}\zlabel{ch:2}
\testtext
\begin{equation}
eq2.1
\label{2.1}\zlabel{2.1}
\end{equation}
\begin{equation}
eq2.2
\label{2.2}\zlabel{2.2}
\end{equation}
\appendix
\chapter{A in Volume 1}\label{ch:1A}\zlabel{ch:1A}
\testtext
\begin{equation}
eq1A.1
\label{1A.1}\zlabel{1A.1}
\end{equation}
\begin{equation}
eq1A.2
\label{1A.2}\zlabel{1A.2}
\end{equation}
\end{document}
I tried to modify the code file to solve the problem. First, I changed the declaration of the chatervalue property so that the current value of \thechapter command is written to the .aux file, and not the value of the chapter counter:
%%\zref@newprop{chaptervalue}[-1]{\the\value{chapter}}
\zref@newprop{chaptervalue}[0]{\thechapter}
However, I have not been able to change the command \ifcurrentchapter so that it correctly compares the \thechapter value to the chaptervalue property. The following definition of the command \ifcurrentchapter always gives a false value.
\newcommand\ifcurrentchapter[3]{%
%%\def\temp@a{\number\value{chapter}}%
\def\temp@a{\thechapter}%
\def\temp@b{\zref@extract{#1}{chaptervalue}}%
%%\ifnum\temp@a=\temp@b%
\if\temp@a\temp@b%
#2%
\else%
%%\ifnum\temp@b<0
\if\temp@b=0
#2%
\else
#3%
\fi%
\fi%
}
How to modify its definition to make it working as desired?



By more robust I mean a solution which will not know a priori how the chapter number is formatted in appendix, \Roman{chapter} or, say, \Latin{chapter}.
– Igor Kotelnikov Aug 12 '20 at 09:00\IfCounterchapterTF. Perhaps instead pf\zref@extractdefault{#1}{counter}{0}there should be\zref@extractdefault{#1}{chaptervalue}{0}? – Igor Kotelnikov Aug 14 '20 at 23:08\sref{ch:1}, you don't want the chapter prefix there, as it would lead to double values (A.A, 1.1), force the test to fail with e.g.\IfCounterchapterTF{x#1}then you can see what I mean. – Ulrike Fischer Aug 14 '20 at 23:12{counter}. I has changed{counter}to{chaptervalue}and also got correct result.\IfCounterchapterTF{x#1}also gave correct result. It seems that\IfCounterchapterTF{#1}always evaluates to false. – Igor Kotelnikov Aug 15 '20 at 02:32