# How do I use an auxilliary file for my own commands?

I would like to extend the \label and \ref commands to provide me not only with the number of the section but also some sort of heading.

What I have in mind is something like this:

\documentclass{article}
\usepackage{xparse}
\makeatother
\let\oldlabel\label
\def\Ref#1{see~\ref{#1}~(\csname l@#1\endcsname)}
\DeclareDocumentCommand{\label}{m o}%
{
\oldlabel{#1}
\IfValueTF{#2}%
{\expandafter\gdef\csname l@#1\endcsname{#2}}
{}
}
\makeatother
\begin{document}
\section{Intro}\label{intro}[Introduction]
Some text referencing \Ref{intro} or \Ref{outro}
\section{Outro}\label{outro}[Outro]
blah...
\end{document}


However, if I use \Ref with a forward reference I get an error because \l@outro is not yet defined. I would like to fix that by writing these \gdefs into an aux file and including at the begin of the document at the second latex run.

Therefore, my questions are: 1) How can I properly write \gdefs into an aux file, say \jobname.ref ? 2) How can I safely read the aux file? (It might not yet exist...)

The problem with undefined commands isn't a problem since \@ifundefined can be used. But let's assume all labels specify the optional argument so far.

• I am sorry, actually I don't get an error but \csname...\endcsname just results in an empty text. Sep 3, 2013 at 23:21
• I sort of figured it out... I am going to post the answer as soon as possible but not tonight :) Sep 4, 2013 at 0:09

I figured out how to use the newfile package for my purposes.

    \documentclass{article}
\usepackage{xparse,ifthen}
\usepackage{newfile,hyperref}
\makeatother
\AtBeginDocument{%
\newoutputstream{refs}
\let\oldlabel\label
\let\oldsect\section
\DeclareDocumentCommand{\label}{m o}%
{
\oldlabel{#1}
\IfValueTF{#2}%
{%
}
{}
}
\def\Ref#1{see~\ref{#1}~(\expandafter\ifx\csname l@#1\endcsname\undefined{??}\else{\csname l@#1\endcsname}\fi)}
\def\section#1\label#2{\oldsect{#1}\label{#2}[#1]}}
\makeatother
\begin{document}
\IfFileExists{\jobname.ref}{\input{\jobname.ref}}{}
\openoutputfile{\jobname.ref}{refs}

\section{Intro}\label{intro}
Some text referencing \Ref{intro} or \Ref{outro}
\section{Outro}\label{outro}
blah... see \pageref{intro} or \autoref{intro}


In the end, I redefined the commands \section#1\label#2 to store the section title #1 into \l@#2 for later use by \Ref. Of course, in the end I need to redefine \chapter, \subsection etc. too.
• Well, the reason why I redefined \section#1\label#2 is that TeXnicCenter recognized these two commands for the navigation panel and the code completion. I did not want to define new commands which are not recognized by TeXnicCenter OR which would be redundant by defining arguments twice such as \section{A}\label{sec:A}\sect{sec:A}{A}. Sep 4, 2013 at 8:32