I am trying to restrict refs' and labels' scopes to certain sections of a LaTeX document. Cf. this question.
I have a custom \locallabels{} command, modified from here, and a custom \eq{} command:
\documentclass{article}
\AtBeginDocument{%
\let\origref\ref \let\origpageref\pageref \let\origlabel\label \let\origeqref\eqref \newcommand\locallabels[1]{%
\renewcommand\label[1]{\origlabel{#1##1}}% ← notice the change here:
\renewcommand\ref[1]{\origref{#1##1}}% ##1#1, not #1##1
\renewcommand\pageref[1]{\origpageref{#1##1}}%
\renewcommand\eqref[1]{\origeqref{#1##1}}%
}}
\usepackage{amsmath}
\newcommand{\eq}[1]{\eqref{eq:#1}}
\begin{document}
\section{Section 1}
\label{sec}
\begin{equation}
\label{eq:1}
\int3x+4\pi\,dx
\end{equation}
\eq{1} should give (1), but it actually gives (2).
\locallabels{:sec2}
\section{Section 2}
\label{sec2}
\begin{equation}
\label{eq:1}
\int6x+3y\,dx
\end{equation}
\eq{1} should give (2), but it gives "(??)".
\end{document}
Why do I get unresolved references, "(??)" for the second \eq{1}? Why do I get "(1)" for the first \eq{1}?
UPDATE: I discovered that hyperref (not included in the above MWE) complicates things. See the comments below.
Also, how would I define a command that would turn off \locallabels{}'s effect for a certain reference. E.g., this doesn't work:
\newcommand{\refnolocallabels}[2]{\locallabels{}\ref{#1}\locallabels{#2}}
thanks
amsmathredefines\labelitself. – egreg Oct 01 '13 at 16:53\labelafteramsmathredefines it? thanks – Geremia Oct 01 '13 at 19:19