0

Since \nameref only provides clickable links, which are obviously not printed and therefore not very helpful for anything that will eventually be printed, I need to redefine it (or use another suitable command that I'm unaware of) to achieve this.

What \nameref currently does:

An important part of TDD is \nameref{sec:refactoring}.
→ An important part of TDD is Refactoring.

What I would like it to do:

An important part of TDD is \nameref{sec:refactoring}.
→ An important part of TDD is Refactoring (section 1.2.3).

Italics indicates a hyperref link in these examples, bold is what I would like to change.
The "section" label would optimally be rendered as "chapter" for, well, chapters, and "section" for anything else. I know this is possible in some way, I've done it before, but I don't remember how (and can't look it up).

Does anyone know how to enable this functionality? It doesn't necessarily need to redefine \nameref, a custom command is also fine and eliminates the need for let or similar, so it's enough for proof of concept.

What I have so far is

\newcommand*{\namesectionref}[1]{\nameref{#1} (\ref{#1})}

However, this doesn't add a label in front of \ref.

2
  • a really primitive method is to define two new reference commands, one for chapter and the other for section. the one for section would be nearly what you have now: \newcommand*{\namesectionref}[1]{\nameref{#1} (section \ref{#1})}. (one could observe that the original rationale for \nameref was to avoid including the number in the xref.) Mar 6, 2018 at 18:56
  • I have implemented something similar for arbitrary labels (such as "Mile­stone M1: Spec­i­fi­ca­tion cre­ated" or "Requirement R1"). See ctan.org/pkg/refenums for details.
    – koppor
    Mar 20, 2018 at 13:07

1 Answer 1

4

In many cases, you get the entity name of the reference by \autoref:

\documentclass{article}
\usepackage{hyperref}

\newcommand*{\nameautorefA}[1]{%
  \nameref{#1} (\autoref{#1})%
}
\newcommand*{\nameautorefB}[1]{%
  \hyperref[{#1}]{\nameref*{#1} (\autoref*{#1})}%
}

\begin{document}
\section{Refactoring}
\label{sec:refactoring}
\verb|\nameautorefA|: \nameautorefA{sec:refactoring}\\
\verb|\nameautorefB|: \nameautorefB{sec:refactoring}
\end{document}

Result

The difference between \nameautorefA and \nameautorefB is the link area.

The star forms \nameref* and \autoref* prevent links inside the link \hyperref.

You must log in to answer this question.

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