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?
Asked
Active
Viewed 145 times
2
Werner
- 603,163
Bruno Murino
- 45
1 Answers
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
-
1The way the
amspackages 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 -
-
@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 That's why I asked. If I add
hyperrefto this example, it doesn't work. At least,hyperrefworks. 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
-
-
@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
-
-
hyperref? – Werner Sep 25 '16 at 19:58