1

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.

Thanks for your help!

migrated from stackoverflow.com Sep 4 '13 at 19:38

This question came from our site for professional and enthusiast programmers.

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

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}%
                {%
                \addtostream{refs}{\protect\DeclareDocumentCommand{\string\l@#1}{}{#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}
            \addtostream{refs}{\protect\makeatletter}

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

            \addtostream{refs}{\protect\makeatother}
            \closeoutputstream{refs}
    \end{document}

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.

If you have any further improvements or found some errors please let me know.

  • 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}. – Matthias Sep 4 '13 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.