# Context dependent cross reference formatting

I'd like to format my equations so that within any chapter, the label generated is

(\thesection.\theequation) 1+1 =2

(without chapter number).

A cross-reference within a given chapter would be just these two numbers, but a reference to the same equation from a different chapter would yield:

(chapter.section.equation)

I've seen this done (the books by Hairer, Wanner et cie.) but cannot find the right information.

I intended to start from this

\documentclass[chapterprefix=on,leqno,10pt,notitlepage,abstracton]{scrreprt}
\usepackage{amsmath}
\usepackage{amsthm}
\numberwithin{equation}{section}
\begin{document}
\chapter{first}
\label{cha:first}

\section{FA}
\label{sec:fa}
$$x + x = 2x$$

\section{FB}
\label{sec:fa}
$$\label{eq:1} x + b = a$$

\chapter{Second}
\label{cha:second}

\section{SA}
\label{sec:sa}

See Eqn.~\eqref{eq:1}
$$\label{eq:2} e^{i\beta}$$

Eqn.~\eqref{eq:2} is Euler's favorite: it has all the important constants
in it.
\section{b}
\label{sec:b}
From Eqn.~\eqref{eq:1}, we cannot derive Eqn.~\eqref{eq:2}.
\end{document}

• Could you provide the community with a base template of your document structure, preferably in the form of a minimal working example (MWE)? This should start with \documentclass and end with \end{document}. So much so that we can copy-and-paste-and-compile and see exactly something that you're working with... no \include, not \input, just \usepackage and some dummy text. Follow the link provided to get a flavour of what should/not be included. – Werner Jul 24 '15 at 18:45
• Are you using hyperref? What about amsmath? – Werner Jul 24 '15 at 19:18

Here is a start using zref's "property lists":

\documentclass{report}

\usepackage{zref}

\makeatletter
\zref@newlist{relrefprops}
\zref@newprop{chapter}{\arabic{chapter}}
\zref@newprop{chapeqnum}{\thesection.\arabic{equation}}
\zref@newprop{eqnum}{\arabic{section}.\arabic{equation}}

% Mark a possible "relative label"
\newcommand{\releqlabel}[1]{%
\zref@labelbylist{#1}{relrefprops}%
}
% Use a "relative label" in a reference
\newcommand{\releqref}[1]{%
\mbox{(%
\ifnum\value{chapter}=\zref@extractdefault{#1}{chapter}{0}
\zref@extract{#1}{eqnum}% In same chapter
\else
\zref@extract{#1}{chapeqnum}% In different chapter
\fi
)}%
\zref@refused{#1}% Reference has been used
}
\makeatother

\renewcommand{\theequation}{\thesection.\arabic{equation}}

\begin{document}

\setcounter{chapter}{4}% Just for this example
\chapter{A chapter}
\setcounter{section}{8}% Just for this example
\setcounter{equation}{3}% Just for this example
\section{A section}
See \releqref{eq:f} and \releqref{eq:g}.
$$f(x) = ax^2 + bx + c \releqlabel{eq:f}$$

{\let\clearpage\relax% Just for this example
\setcounter{chapter}{7}% Just for this example
\chapter{Another chapter}
}\setcounter{section}{5}% Just for this example
\setcounter{equation}{9}% Just for this example
\section{Another section}
See \releqref{eq:f} and \releqref{eq:g}.
$$g(x) = \alpha x^2 + \beta x + \delta \releqlabel{eq:g}$$

\end{document}


\releqlabel sets a label for an equation that may require a relative reference later. Similarly, \releqref is used to reference an equation that may be relative to the current chapter.

zref is compatible with hyperref, so this approach could be extended to include that.

• That's very close except for the tag formatting. My edits in the next answer. – user82566 Jul 24 '15 at 20:26

A few minor tweaks and now, this is it.

\documentclass[chapterprefix=on,leqno,10pt,notitlepage,abstracton]{scrreprt}
\usepackage{amsmath}
\usepackage{zref}

\usepackage{chngcntr}
\counterwithin{equation}{section}

\makeatletter
\zref@newlist{relrefprops}
\zref@newprop{chapter}{\arabic{chapter}}
\zref@newprop{section}{\arabic{section}}
\zref@newprop{chapeqnum}{\thechapter.\thesection.\arabic{equation}}
\zref@newprop{eqnum}{\arabic{section}.\arabic{equation}}

% Mark a possible "relative label"
\newcommand{\releqlabel}[1]{%
\zref@labelbylist{#1}{relrefprops}%
}
% Use a "relative label" in a reference
\newcommand{\releqref}[1]{%
\mbox{(%
\ifnum\value{chapter}=\zref@extractdefault{#1}{chapter}{0}
\zref@extract{#1}{eqnum}% In same chapter
\else
\zref@extract{#1}{chapeqnum}% In different chapter
\fi
)}%
\zref@refused{#1}% Reference has been used
}
\makeatother

\renewcommand\thesection{\arabic{section}}
\renewcommand{\theequation}{\thesection.\arabic{equation}}

\begin{document}

\setcounter{chapter}{4}% Just for this example
\chapter{A chapter}
\setcounter{section}{8}% Just for this example
\setcounter{equation}{3}% Just for this example
\section{A section}
See \releqref{eq:f} and \releqref{eq:g}.
$$f(x) = ax^2 + bx + c \releqlabel{eq:f}$$

{\let\clearpage\relax% Just for this example
\setcounter{chapter}{7}% Just for this example
\chapter{Another chapter}
}\setcounter{section}{5}% Just for this example
\setcounter{equation}{9}% Just for this example
\section{Another section}
See \releqref{eq:f} and \releqref{eq:g}.
$$g(x) = \alpha x^2 + \beta x + \delta \releqlabel{eq:g}$$

\end{document}