2

I would like to know if there's a functionality that would give me the chapter's name by referencing a label that I know is defined somewhere in this chapter?

For example:

\chapter{What's it's name?}
\label{chap:whatsItsName}
\section{Party}
\label{sec:party}
\subsection{Going down}
\label{subSec:goingDown}
[...]

\chapter{Second chapter}
[...]
Like said in Section \ref{subSec:goingDown} in the Chapter \chapterNameRef{subSec:goingDown} [...]

Which would result in a something like this:

Chapter What's it's name?

Party

Going down

[...]

Second chapter

[...]

Like said in Section 1.1.1 in the Chapter What's it's name? [...]

I know that there's nameref which could give me the chapter's name, if I reference the chapter's label, but I want to know if it's possible to reference a subsection's label and get the name of the chapter it's located in.

1
  • 1
    No, it does not. It's entirely different. They are asking about the type of section they are in so their command would return "section"/"chapter"/etc.. But I want the name of the chapter that the label I'm referencing is in.
    – sternAndy
    Dec 12, 2015 at 20:30

2 Answers 2

1

This is a preliminary solution -- it works for chapter only.

\documentclass{book}

\usepackage{refcount}

\usepackage{xpatch}
\usepackage{xparse}


\xpatchcmd{\@chapter}{\refstepcounter{chapter}}{\refstepcounter{chapter}\label{chapautolabel:\number\value{chapter}}}{}{} % Provide an automated label

\usepackage{nameref}


\ExplSyntaxOn
\seq_new:N \l_sternandy_ref_seq 
\NewDocumentCommand{\chapnameref}{O{chapautolabel}m}{%
  \tl_set:Nx \l_tmpa_tl {\getrefnumber{#2}}
  \seq_set_split:NnV \l_sternandy_ref_seq {.} {\l_tmpa_tl} % Works only if the standard numbering scheme is used 
  \nameref{#1:\seq_item:Nn \l_sternandy_ref_seq {1}}
}
\ExplSyntaxOff



\begin{document}

\chapter{What's it's name?}
\label{chap:whatsItsName}
\section{Party}
\label{sec:party}
\subsection{Going down}
\label{subSec:goingDown}

\chapter{Second chapter}


Like said in Section \ref{subSec:goingDown} in the Chapter \chapnameref{subSec:goingDown} 


\end{document}

enter image description here

1

Yes, you can store this information for retrieval later using zref:

enter image description here

\documentclass{book}

\usepackage{xparse,zref}

\let\oldlabel\label
\makeatletter
\zref@newprop{chaptertitle}{\chaptertitle}
\renewcommand{\label}[1]{%
  \oldlabel{#1}%
  \zref@labelbyprops{#1*}{chaptertitle}%
}
\let\oldchapter\chapter
\newcommand{\chaptertitle}{}% This will store the chapter title
\RenewDocumentCommand{\chapter}{s o m}{%
  \renewcommand{\chaptertitle}{#3}% Store chapter title
  \IfBooleanTF{#1}
    {% \chapter*
      \oldchapter{#3}% \chapter*[.]{..}
    }
    {% \chapter
      \IfNoValueTF{#2}
        {\oldchapter{#3}}% \chapter{..}
        {\oldchapter[#2]{#3}}% \chapter[.]{..}
    }
}
\newcommand{\chapterNameRef}[1]{%
  \zref@extract{#1*}{chaptertitle}% Extract chapter title
  \zref@refused{#1*}% Reference has been used
}
\makeatother

\begin{document}

\chapter{What's it's name?}\label{chap:whatsItsName}
\section{Party}\label{sec:party}
\subsection{Going down}\label{subSec:goingDown}

\chapter{Second chapter}
Like said in Section \ref{subSec:goingDown} in the Chapter \chapterNameRef{subSec:goingDown}\ldots

\end{document}

It would be easy to extend this to other sectional units as well.

You must log in to answer this question.

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