1

Is there a way to get the name of any, not just the current, section (chapter, section, subsection, ...) by its label? Just like the \nameref command, but the hyperref package is loaded and I don't want a clickable link under some circumstances. I'm thinking of a command like \getnameofsection{sec:...}.

For example:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}

\newcommand*{\getnameofsection}[1]{%get the name section referenced by its label as parameter
}

\begin{document}
\section{Introduction}
\label{sec:introduction}

An introduction...

\section{Discussion}
\label{sec:discussion}

A discussion...

\subsection{Pros}
\label{sec:discussion:pros}

The advantages...

\subsection{Cons}
\label{sec:discussion:cons}

The disadvantages. As mentioned in \getnameofsection{sec:introduction}...
    
    
\end{document}

Thanks!

6
  • What exactly do you mean by but without the reference ?
    – daleif
    Jul 27, 2021 at 22:27
  • @daleif Without a clickable link
    – Octribin
    Jul 28, 2021 at 8:11
  • Erh, \nameref does not produce a clickable link unless hyperref is loaded, of which you leave no information (now you know why we always ask for a full but minimal example, such that we know what you are doing)
    – daleif
    Jul 28, 2021 at 8:16
  • If hyperref is loaded \nameref* generates no link.
    – daleif
    Jul 28, 2021 at 8:17
  • 1
    That is not a full example, we cannot copy and test this example, there is no document class or preamble
    – daleif
    Jul 28, 2021 at 8:33

2 Answers 2

0

First of all it is very important to provide a full minimal example to start with especially when hyperref is involved.

The nameref package will normally enable the storrage of sectional titles to be retrieved using \nameref{label}. Whenever hyperref is loaded \nameref naturally becomes a hyperlink. As with many (but not all) link giving constructions, there is a starred version that omits the link.

So in this case use \nameref*.

Note that a class like memoir has the features of \nameref build in and thus the nameref package is not needed.

1

I assume you mean by number and not by "label" since you say without the reference.

It's possible, but it would take some work. If you have a table of contents, you could load the table of contents inside a group where you've redefined \contentsline to save the titles by number into their own macros. Some probably non-working code:

\begingroup
\def\parsecontentsline#1#2#3!!!{%
  \ifx\numberline#1%
    \global\@namedef{namebynum#2}{#3}%
  \fi
}
\def\contentsline#1#2#3#4{%
  \parsecontentsline#2!!!%
}
\@input{\@jobname.toc}
\endgroup

\NewDocumentCommand{\namebynumber}{m}{\@nameuse{namebynum#1}}

If you're adding anything else into the table of contents, those commands will also need to be no-oped out inside the group (and of course the above needs \makeatletter\makeatother to work). But since you don't necessarily know the section numbers, maybe not trying to avoid the nameref package isn't such a bad idea (although nameref might conceivably not work with some document classes).

You must log in to answer this question.

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