4

I want to use a counter in some chapter names, this works fine for the TOC and in the chapter names themselves, but references to the chapter name appear to use the counter at the time of the reference, rather than at the time of the chapter.

Here's a MWE to show the problem:

\documentclass{book}

\usepackage{hyperref}

\newcounter{NoteIdx}
\newcommand{\notechapter}[1]{%
    \stepcounter{NoteIdx}%
    \chapter{Note \arabic{NoteIdx}: #1}%
}

\begin{document}

\tableofcontents

\chapter{Things}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{Don't eat bees}
\label{bees}
Some text here

\chapter{Stuff}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{Always wear a hat}
\label{hat}
Some text here

\end{document}

The text in chapter 1 is:

Refer to the Note 0: Don’t eat bees chapter. Refer to the Note 0: Always wear a hat chapter.

The text in chapter 3 is:

Refer to the Note 1: Don’t eat bees chapter. Refer to the Note 1: Always wear a hat chapter.

Is there a way to define the chapter names such that the numbers generated from the counter get converted to text instead of sending the command to \nameref which gets processed when the counter is at the wrong value?

  • @Werner Do you know how to fix the formatting? – cfr Dec 22 '15 at 3:54
7

I would attempt to play into nameref's wheelhouse. And specifically, nameref uses/needs \@currentlabelname to identify the name, so define it within your \notechapter to look just the way you want:

enter image description here

\documentclass{book}

\usepackage{hyperref}

\newcounter{NoteIdx}
\makeatletter
\newcommand{\notechapter}[1]{%
  \stepcounter{NoteIdx}%
  \chapter{Note \theNoteIdx: #1}%
  \protected@edef\@currentlabelname{Note \theNoteIdx: #1}% To make nameref happy
}
\makeatother

\begin{document}

\tableofcontents

\chapter{Things}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{Don't eat bees}\label{bees}
Some text here

\chapter{Stuff}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{Always wear a hat}\label{hat}
Some text here

\end{document}
6

The formatting is wrong, but you can do this:

\documentclass{book}

\usepackage{hyperref}

\newcounter{NoteIdx}
\newcommand{\notechapter}[2]{%
  \refstepcounter{NoteIdx}\label{note:#1}%
  \chapter{Note \ref*{note:#1}: #2}\label{#1}%
}

\begin{document}

\tableofcontents

\chapter{Things}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{bees}{Don't eat bees}
Some text here

\chapter{Stuff}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{hat}{Always wear a hat}
Some text here

\end{document}

referenced numbers

EDIT Format corrected thanks to Werner.

  • 1
    You should probably use \ref*{..} in the \chapter title. – Werner Dec 22 '15 at 4:33
  • Thanks, @Werner. I couldn't, for some reason, remember how to do this. (I think I've only read this and never used it.) – cfr Dec 22 '15 at 11:01
1

You need to pass \theNoteIdx expanded, rather than tampering with internals.

\documentclass{book}
\usepackage{xparse}
\usepackage{hyperref}

\newcounter{NoteIdx}

\ExplSyntaxOn
\NewDocumentCommand{\notechapter}{m}
 {
  \stepcounter{NoteIdx}%
  \jhabbott_notechapter:fn { \theNoteIdx } { #1 }
}

\cs_new_protected:Nn \jhabbott_notechapter:nn
 {
  \chapter{Note~#1:~#2}
 }
\cs_generate_variant:Nn \jhabbott_notechapter:nn { f }
\ExplSyntaxOff

\begin{document}

\tableofcontents

\chapter{Things}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{Don't eat bees}\label{bees}
Some text here

\chapter{Stuff}
Refer to the \nameref{bees} chapter.
Refer to the \nameref{hat} chapter.

\notechapter{Always wear a hat}\label{hat}
Some text here

\end{document}

There are other ways, without expl3, but this is neat code: the f-variant provides the right fully expanded bit.

enter image description here

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.