2

I want each chapter to have a closed set of labels, meaning that different chapters can contain the same label, and if I use \eqref in that chapter, only look for the label in that same chapter, otherwise say "not defined label". In summary, I want each chapter to have independent label. Can I do that?

4
  • 3
    Welcome to TeX.SX! You're going to shoot at your own feet with this.
    – egreg
    Sep 25 '16 at 19:57
  • Are you using hyperref?
    – Werner
    Sep 25 '16 at 19:58
  • @Werner Yes I am using hyperref. But if I could, I would use it only for headers, and not for equations. At the moment, hyperref is referring to the closest label it finds, I think. Sep 25 '16 at 20:07
  • @egreg Why do you think that? Sep 25 '16 at 20:07
3

You can create a macro to automatically add chap<chapternumber>: before each label, and change \ref so that it searches for references with chap<chapternumber>:prefix.

\documentclass{book}
\usepackage{amsmath}

\let\normallabel\label
\let\normalref\ref

\makeatletter
% Poor man's version of ConTeXt's \expanded macro
\newcommand\expanded[1]
    {\xdef\EXPANDED@MACRO{\noexpand#1}\EXPANDED@MACRO}
\makeatother

\newcommand\customlabel[1]
    {\expanded{\normallabel{chap\thechapter:#1}}}

\newcommand\customref[1]
    {\normalref{chap\thechapter:#1}}

\let\ref\customref
\let\label\customlabel

% AMSMath changes the behavior of label.
\makeatletter
\let\ltx@label\customlabel
\makeatother


\begin{document}

\chapter{First chapter}
\begin{equation}\label{eq:first}
  a = b + c
\end{equation}

It is shown in \eqref{eq:first} (\ref{eq:first}) that \ldots

\chapter{Second chapter}
\begin{equation}\label{eq:first}
  a = b + c
\end{equation}

It is shown in \eqref{eq:first} (\ref{eq:first}) that \ldots
\end{document}

If you open filename.aux file, you'll see:

\relax 
\@writefile{toc}{\contentsline {chapter}{\numberline {1}First chapter}{1}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\newlabel{chap1:eq:first}{{1.1}{1}}
\@writefile{toc}{\contentsline {chapter}{\numberline {2}Second chapter}{3}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
\newlabel{chap2:eq:first}{{2.1}{3}}

Thus, the labels are written as {chap1:eq:first}, {chap2:eq:first}, etc. So, they are unique per chapter.

13
  • 1
    The way the ams packages typeset tags and labels are... tricky. See the answers to tex.stackexchange.com/questions/319158/… for example. Sep 25 '16 at 20:32
  • @WillieWong: Thanks for the explanation! I have updated the answer.
    – Aditya
    Sep 25 '16 at 20:38
  • @Aditya Thank you very much! I changed your answer a little, but everything is working fine! Sep 25 '16 at 21:03
  • Question: would you consider using this yourself? You don't mention any of the downsides of doing thing or the potential pitfalls.
    – cfr
    Sep 26 '16 at 2:59
  • @BrunoMurino Did you try \usepackage{hyperref}?
    – cfr
    Sep 26 '16 at 3:08

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.