0

How can I reference a toc entry created with \addcontentsline?

enter image description here

\documentclass{l3doc}
\usepackage{hyperref}

\begin{document}

\tableofcontents

\addcontentsline{toc}{section}{\protect\numberline{\thesection}Foo}\label{opt:foo}
\DescribeOption{Foo}

Section~\ref{opt:foo} % Expect: % Section~0

\end{document}
3
  • Why l3doc as document class?
    – Johannes_B
    Apr 10, 2020 at 5:15
  • @johannes_B Tha's the context in which the issue arose. If you have to think about what could replace \DescribeOption{Foo} using \documentclass{article}, then probably more straightforward to stick to l3doc. I'm open to a contrary opinion.
    – Erwann
    Apr 10, 2020 at 5:20
  • PS: In the original, the reference appears in \changes. I have just realized that it works after exec the glo+index commands given in the *log file' (Package l3doc Info: Generate the change,,,).
    – Erwann
    Apr 10, 2020 at 5:30

1 Answer 1

1

\label needs the reference info in \@currentlabel, but \addcontentsline does nothing to \@currentlabel. You could do for instance:

\makeatletter
\def\@currentlabel{\p@section\thesection}%
\makeatother

or more explicitly, \makeatletter\def\@currentlabel{0}\makeatother (or another value) right before using \label to define this info, but the usual way to set \@currentlabel is to use \refstepcounter.

Since you appear to want 0 as the section number and \refstepcounter increments the counter before storing the \protected@edef-expansion of \p@<counter>\the<counter> in \@currentlabel, I use \setcounter{section}{-1} before the first \refstepcounter{section} call here.

\documentclass{l3doc}
\usepackage{hyperref}

\makeatletter
\newcommand*{\mySetNameref}{\def\@currentlabelname}
\makeatother

\begin{document}

\tableofcontents
\bigskip

% First option
\leavevmode
% The next “section” will be numbered 0
\setcounter{section}{-1}\refstepcounter{section}%
\mySetNameref{Option \texttt{Foo}}%
\label{opt:foo}%
\addcontentsline{toc}{section}{\protect\numberline{\thesection}Foo}%
\DescribeOption{Foo} Description of \texttt{Foo}

% Second option
\leavevmode
\refstepcounter{section}%
\mySetNameref{Option \texttt{Bar}}%
\label{opt:bar}%
\addcontentsline{toc}{section}{\protect\numberline{\thesection}Bar}%
\DescribeOption{Bar} Description of \texttt{Bar}

\medskip
Section~\ref{opt:foo} is named \nameref{opt:foo}. Section~\ref{opt:bar} is
named \nameref{opt:bar}.

\end{document}

enter image description here

Of course, you should probably wrap this in a macro to make it more automatic.

4
  • Thanks. Is there a way to make it work for nameref as well?
    – Erwann
    Apr 10, 2020 at 11:32
  • I've updated the example to define the nameref of each label.
    – frougon
    Apr 10, 2020 at 12:38
  • Thanks. Why is \leavevmode needed?
    – Erwann
    Apr 12, 2020 at 1:03
  • 1
    To make sure the label (a \write whatsit) is in the same horizontal box as the following option from \DescribeOption.
    – frougon
    Apr 12, 2020 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .