63

Is it possible to make \nameref display not only the title of the section but also its number?

\section{First Section}
\label{sec:some_sec}

\subsection*{Unnumbered subsection}
\label{sec:subsec}

[...]

As we defined in section \nameref{sec:some_sec}, yada yada yada, see section \nameref{sec:subsec}

With normal \nameref I get:

"As we defined in section First Section, yada yada yada, see section Unnumbered subsection"

what I want is a combination of numbers (as in \autoref} and section names AND (if possible) unnumbered subsections should inherit the number of the parenting section:

"As we defined in section 1 First Section, yada yada yada, see section 1 Unnumbered subsection"

lockstep
  • 250,273
cpt. jazz
  • 885
  • 9
    section~\ref{sec:some_sec} \nameref{sec:some_sec} – egreg Jun 30 '13 at 18:05
  • 1
    Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. – Claudio Fiandrino Jun 30 '13 at 18:18
  • 1
    Your label {sec:subsec} has a negative effect. You are using \subsection* and so there is no anchor (number) for any label. – Marco Daniel Jun 30 '13 at 18:55
  • Marco, the answer works as I described above. For un-numbered subsections the number of the parenting section should be used -- and this is the case. Also, when i click the text in the PDF I get to the subsection (as expected). Thats exactly what I wanted ;) – cpt. jazz Jun 30 '13 at 19:02

3 Answers3

69

How about defining a new command \fullref:

\documentclass{article}
\usepackage[colorlinks=true]{hyperref}

% Original definition
% \newcommand\fullref[1]{\autoref{#1} \nameref{#1}} % Two links

% Updated definition, see explanation below
\newcommand*{\fullref}[1]{\hyperref[{#1}]{\autoref*{#1} \nameref*{#1}}} % One single link

\begin{document}

\section{First Section}
\label{sec:some_sec}

\subsection*{Unnumbered subsection}
\label{sec:subsec}

[...]

As we defined in \fullref{sec:some_sec}, yada yada yada, see \fullref{sec:subsec}

\end{document}

enter image description here


Update

The \fullref defined above produces two links, one by \autoref, the other by \nameref. Heiko Oberdiek suggests the following definition that combines the two into one single link:

\newcommand*{\fullref}[1]{\hyperref[{#1}]{\autoref*{#1} \nameref*{#1}}}
Heiko Oberdiek
  • 271,626
Herr K.
  • 17,946
  • 4
  • 61
  • 118
  • 15
    You get a full link in one piece by \newcommand*{\fullref}[1]{\hyperref[{#1}]{\autoref*{#1} \nameref{#1}}}. – Heiko Oberdiek Jun 30 '13 at 21:14
  • @HeikoOberdiek: Thanks :-) Your suggestion is incorporated. – Herr K. Jun 30 '13 at 21:31
  • 2
    I have added the star for \nameref that I have forgotten in the comment. The star forms prevent nested double links. – Heiko Oberdiek Jun 30 '13 at 23:32
  • 1
    Don't you need to add \usepackage{nameref} for this to work? – Patrick Jan 13 '15 at 13:29
  • @for3st: I don't think so. – Herr K. Jan 13 '15 at 16:57
  • 1
    It conflicts with the titlesec package. The reference prints things like "chapter 2" instead of the name of the section. – Velkan Nov 23 '16 at 09:30
  • tcolorbox does many things and, being fully compatible with hyperref, also includes using \cref{exp:5-1} to refer to Example 5-3, \cref{3-0} to chapter 3 \label{3-0}, \cref{5-3} to chapter 5 section 3 \label{3-5}. I just tried \newcommand*{\fullref}[1]{\hyperref[{#1}]{\cref*{#1} \nameref*{#1}}} which makes the whole thing clickable. But, as I prefer (as with \cref) the words chapter and section not to appear in the link, I regretfully reverted to having to type, e.g. \cref{1-0} \nameref{1-0} on page \pageref{1-1} and \cref{1-1} \nameref{1-1} on page \pageref{1-1}. – schremmer Jan 05 '17 at 19:51
  • 2
    @HeikoOberdiek if I want to capitalize the first letter, can I use \newcommand*{\fullref}[1]{\hyperref[{#1}]{\Cref*{#1} \nameref*{#1}}} with cleveref package or is there a better way? – Diaa Apr 29 '17 at 07:32
  • @HerrK. if I only section 1 and section is Section 1, how can I set my tex file? – acsdaswe Oct 09 '19 at 00:03
  • hyperref's sectionautorefname can be set empty to avoid the name `section' to appear. The hyperref manual contains such an example. – Marius Hofert Apr 16 '22 at 12:47
2

Starting with @herr-k 's answer, I went a step farther and defined four (4) new commands: \secref, \subsecref, \subsubsecref, and \parref.

\newcommand*{\secref}[1]{\hyperref[{#1}]{Specification \ref*{#1}, \nameref*{#1}}}
\newcommand*{\subsecref}[1]{\hyperref[{#1}]{Section \ref*{#1}, \nameref*{#1}}}
\newcommand*{\subsubsecref}[1]{\hyperref[{#1}]{Section , \nameref*{#1}}}
\newcommand*{\parref}[1]{\hyperref[{#1}]{Section \ref*{#1}, \nameref*{#1}}}

Doing so allows me to customize how the hyperlink text displays for each level of my document.

Jay Lee
  • 353
  • 1
  • 12
gghh3456
  • 323
0

To get rid of the section in link text: change the \autoref to \ref.

I.e.: \newcommand*{\fullref}[1]{\hyperref[{#1}]{\ref*{#1} \nameref*{#1}}} % One single link

Ji Ma
  • 1