# \ref, \autoref and hyperref expansion

I'm trying to write section numbers to a file with the newfile package. In details, this MWE basically tries to write to a .ref file with lines like A-B. The A and B are generated by \label and \ref commands: if \ref{A} is written in section 2 and \label{A} is defined in section 1, then we should write 2|1 in the file.

This example actually fails to expand the \ref command, probably due to the insertion of the hyperref package, which makes \ref robust. How can one avoid this?

\documentclass{article}

\usepackage{newfile}
\newoutputstream{refFile}
\openoutputfile{\jobname.ref}{refFile}

\let\oldref\ref
\let\oldAutoref\autoref

%%
%% Write SRC-DST of \ref to file
%%
\newcommand{\myref}[1]{%
\def\val{\thesection|\ref{#1}}
}

%%
%% Redefine \autoref
%%
\let\oldAutoref\autoref
\renewcommand{\autoref}[1]{%
\oldAutoref{#1}
\myref{#1}
}

\begin{document}

\section{S1}\label{sec:one}
\autoref{sec:three}

\section{S2}\label{sec:two}
\autoref{sec:one}

\section{S3}\label{sec:three}
\autoref{sec:two}

\closeoutputstream{refFile}

\end{document}


The current content of the .ref file is

1|\ref {sec:three}
2|\ref {sec:one}
3|\ref {sec:two}


but I would like the expansion of the \ref to give:

1|3
2|1
3|2

-
Check the answer here: tex.stackexchange.com/questions/85956/… –  g.kov Mar 22 '13 at 13:48
I already tried to make it works with this answer, but I did not succeed. I get commands \TheRealRef that are not expanded in the output file. –  wwjoze Mar 22 '13 at 16:13