2

There are good answers already on how to refer to the title of the section; and both the title and the number of the section

What I want to do is use the part of the Title; rather than the whole of the title.

\documentclass{book}
\usepackage{hyperref}
\begin{document}
\chapter{Introduction: this is the first theory}
\label{intro}
In the  \nameref{intro}...
\end{document}

I want to get "In the Introduction" rather than "In the Introduction: this is the first theory".

  • Can you provide any rule how TeX/LaTeX etc. should determine which part of the title is interesting? ;-) And you have some typos in your document... – user31729 Aug 27 '17 at 19:17
  • ok, I have corrected the type. I was thinking if there is a way to directly tell the Title when assigning the label for example. \label[Introduction]{intro} – Dellu Aug 27 '17 at 19:20
3

If the optional argument of the \chapter command should not be used but 'any' another free text, there are some ways, e.g. redefining \@currentlabelname to use \nameref still.

I provide a way with the powerful zref package for a new command named \labelshort[optional text]{labelname} and \nameshortref and establishing a new label property shorttitle and having some more flexibility.

Use the optional argument of \nameshortref to add some more explanatory text or correct grammar.

\documentclass{book}
\usepackage{xparse}
\usepackage[user,hyperref]{zref}
\usepackage{hyperref}

\makeatletter
\providecommand{\@currentshorttitle}{}
\zref@newprop{shorttitle}{\@currentshorttitle}
\zref@addprop{main}{shorttitle}

\NewDocumentCommand{\labelshort}{om}{%
  \begingroup
  \IfValueT{#1}{%
    \renewcommand{\@currentshorttitle}{#1}%
    \zlabel{#2}%
  }%
  \endgroup
  \label{#2}%
}

\NewDocumentCommand{\nameshortref}{O{}m}{%
  \zref@ifrefundefined{#2}{%
  }{%
    \hyperlink{\zref@extract{#2}{anchor}}{#1\zref@extract{#2}{shorttitle}}%
  }%
}

\begin{document}
\chapter{Introduction: this is the first theory}
\labelshort[Introduction]{intro}

\section{Foo section}\labelshort[Foo]{foosect}

\clearpage

See \nameshortref[In the ]{intro} or \nameref{intro} or \nameshortref[in the ]{foosect}
\end{document}

enter image description here

  • +1 for providing a solution that is independent of how the chapter should appear in the table of contents. – Tiuri Aug 27 '17 at 19:51
  • @Tiuri: I had a similar idea to your one in my mind but it was too restrictive concerning the optional title, in my point of view. Your solution is clever anyway! – user31729 Aug 27 '17 at 19:54
  • is this working? – Dellu Aug 27 '17 at 20:00
  • @Dellu: Yes, it works – user31729 Aug 27 '17 at 20:07
  • I am getting "Undefined control sequence \labelshort" – Dellu Aug 27 '17 at 20:15
5

The \chapter macro takes an optional argument that is used instead of the proper title (for printing) in the table of contents and in references. So to have \nameref{intro} print just Introduction, you just need to supply this short title as the optional argument:

\documentclass{book}
\usepackage{hyperref}
\begin{document}
\chapter[Introduction]{Introduction: this is the first theory}
\label{intro}
In the  \nameref{intro}...
\end{document}

enter image description here

  • Basically, this answers the question. But, I still need this to work for subsections. Supplying optional text for subsections doesn't seem to work. – Dellu Aug 27 '17 at 19:48
  • It works the same way for all levels (just tested it). If it doesn't work for you, than something else in your code is interfering. – Tiuri Aug 27 '17 at 19:48
  • oh, thanks. Yes, it is working. Just find out that the Table of contents also changes. I was not expecting that. I will try @christian's solution. – Dellu Aug 27 '17 at 20:11

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.