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 \gdef
s 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 \gdef
s 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.
Thanks for your help!