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?

Werner
  • 603,163

1 Answers1

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.

Aditya
  • 62,301
  • 1
    The way the ams packages typeset tags and labels are... tricky. See the answers to http://tex.stackexchange.com/questions/319158/what-does-amsmath-do-to-currentlabel for example. – Willie Wong 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! – Bruno Murino 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 02:59
  • @BrunoMurino Did you try \usepackage{hyperref}? – cfr Sep 26 '16 at 03:08
  • @cfr I already use hyperref. – Bruno Murino Sep 26 '16 at 03:13
  • @BrunoMurino That's why I asked. If I add hyperref to this example, it doesn't work. At least, hyperref works. But the references are not as you want. (I don't think you should want this - I agree with egreg - but since you do.) – cfr Sep 26 '16 at 03:14
  • @crf I don't know, I simplified that code a little and everything is working really fine. – Bruno Murino Sep 26 '16 at 03:23
  • @cfr: I won't use something like this myself but that is because I don't mind using unique references. I don't understand LaTeX internals well enough to know the pitfalls of what I view as a simple hack. – Aditya Sep 26 '16 at 04:21
  • @BrunoMurino Interesting. It didn't work for me when I added hyperref. – cfr Sep 26 '16 at 16:09
  • @cfr I don't know, I didn't use that expand macro, I just defined a newcommand that always adds \thechapter on the argument of \label... , and of course a similar command for \ref... I believe that in my case hyperref feels noting different, since the labels are always different – Bruno Murino Sep 26 '16 at 17:39
  • @BrunoMurino Oh. So you just use \customlabel and \customref directly? – cfr Sep 26 '16 at 22:33
  • @cfr Yes, pretty much it! – Bruno Murino Sep 26 '16 at 22:35