In order to prevent conflicts with labels when gathering exercices from a database, I need to redefine the commands \label, \ref and \pageref to add a local prefix to labels. Unfortunately, the package varioref does not work anymore.
Here is a MWE.
\documentclass{article}
\usepackage{varioref}
\newcommand*{\globallabel}[1]{global:#1}
\AtBeginDocument{%
\let\origref\ref
\renewcommand*{\ref}[1]{%
\origref{\globallabel{#1}}%
}
\let\origpageref\pageref
\renewcommand*{\pageref}[1]{%
\origpageref{\globallabel{#1}}%
}
\let\origlabel\label%
\renewcommand*{\label}[1]{%
\origlabel{\globallabel{#1}}
}
}
\makeatother
\begin{document}
Example 1: figure~\vref{fig:example1}.
Example 2: figure~\vpageref{fig:example2}.
\begin{figure}
\caption{Example 1}
\label{fig:example1}
\end{figure}
\clearpage
\begin{figure}
\caption{Example 2}
\label{fig:example2}
\end{figure}
\end{document}
I get
Example 1: figure 1 on page 1.
Example 2: figure on page 2.
instead of
Example 1: figure 1.
Example 2: figure on the next page.
In the log file, I found
LaTeX Warning: Reference `fig:example1' on page 1 undefined on input line 31.
LaTeX Warning: Reference `fig:example2' on page 1 undefined on input line 33.
I've tried to redefine the commands \vref and \pagreref in the same way ; without success.

hyperrefpackage. I replaced\letby\LetLtxMacroas you said here. But at compile time, I get:TeX Warning: Reference 'global:global:fig:example1' on page 1 undefined on in put line 44.– Éric Guirbal Jun 15 '13 at 19:00babelfor french language. Replacing the active character ':' in labels with '-', or\usepackage[babel,kerning=true]{microtype}solve the problem. – Éric Guirbal Jun 15 '13 at 19:09\usepackage[french]{babel}to both examples. – Heiko Oberdiek Jun 15 '13 at 19:21Missing \endcsname inserted. <to be read again> :. According to egreg: "The problem is that varioref and cref don't like babel shorthands in the labels." Putting\shorthandoff{:}in\AtBeginDocumentis another solution. – Éric Guirbal Jun 15 '13 at 20:35varioref. There are fixes forfrench/babelin versions 2010/11/12 v1.4y and 2011/10/02 v1.4z of packagevarioreffor LaTeX bug reportslatex/4093andtools/4159. – Heiko Oberdiek Jun 15 '13 at 20:49\vrefworks perfectly but\vpageref{fig}has the same output aspage~\pageref{fig}. Loading packagevariorefin block\AtBeginDocumentmake\vpagerefworks properly. But when I addbabelfor french language, Bada Boum !Missing \endcsname inserted. <to be read again> \penalty l.47 Example 1: figure~\vref{fig:example1} ?! Extra \endcsname. \@ifundefined ...dafter \ifx \csname #1\endcsname \relax \expandafter \@firs...– Éric Guirbal Jun 16 '13 at 12:26babelactivates its shorthands in\begin{document}. That disturbs packages that are loaded afterwards via\AtBeginDocument. – Heiko Oberdiek Jun 16 '13 at 12:55