8

I am attempting to override \label so it will automatically create two labels.

\documentclass[11pt,twoside]{book}
\usepackage{hyperref}
\let\origlabel\label
\renewcommand{\label}[1]{\origlabel{vol1:#1}\origlabel{#1}}
\begin{document}
\chapter{Beginning}\label{chap:begin}
\chapter{Middle}\label{chap:middle}
\chapter{End}\label{chap:end}
\end{document}

When I look at the .aux file, there are two problems:

  1. \newlabel command does not contain "vol1".
  2. There is only one \newlabel per \label instead two.

note

Both answers were acceptable based on my original vague question. Accepted answer was able to interpret the complete question (as updated now).

2

2 Answers 2

12

Addition to egreg's answer:

\label is redefined by package nameref that is loaded by hyperref via \AtBeginDocument. Therefore it is enough to load nameref before redefining:

\usepackage{hyperref}
\usepackage{nameref}

\newcommand{\origlabel}{}
\let\origlabel\label
\renewcommand*{\label}[1]{\origlabel{vol1:#1}}

But the redefinition can additionally be put into \AtBeginDocument for the case there are other packages involved.


Also the questions talks about two labels, then additional stuff has to be done. \label tries to be invisible regarding spaces. If space is detected before it suppresses the following spaces. This mechanism does not work for consecutive \labels and the new definition should be surrounded by \@bsphack and \@esphack:

\makeatletter
\AtBeginDocument{%
  \newcommand*{\orig@label}{}%
  \let\orig@label\label
  \renewcommand*{\label}[1]{%
    \@bsphack
    \orig@label{#1}%
    \orig@label{vol1:#1}%
    \@esphack
  }%
  \makeatother
}%

What is the purpose of the added vol1:? If the labels are used inside another document, then packages that are able to import foreign labels (xr, xr-hyper, zref-xr) usually allow the addition of a prefix for the label names. Assuming the main TeX file is called firstvolume.tex, then the other document uses something like:

xr/xr-hyper: \externaldocument[vol1:]{firstvolume}
zref-xr: \zxrsetup{...}\zexternaldocument*[vol1:]{firstvolumne}
5
  • Purpose was to allow a very large two part publication (many authors and editors) to be built by chapter, part, or the entire book and still have the references work. I am using xr for "standalone" building, thanks for the the other package suggestions.
    – h0tw1r3
    Commented Sep 10, 2012 at 19:02
  • I think (?) there is a typo in the code here: presumably it should be \orig@label{#1}% \orig@label{vol1:#1}% Commented Sep 28, 2022 at 1:06
  • @FreddieManners The first code block is an addition to egreg's example that only uses \orig@label{vol:#1}%. Commented Sep 29, 2022 at 17:16
  • Sorry, I should have been more specific, my bad. In the second code block you have \renewcommand*{\label}[1]{ ... } and inside that command body you have \label{#1}\label{vol1:#1} but this appears to cause \label to enter an infinite loop. Changing the calls inside the body to \orig@label seemed to be what was intended. Commented Sep 30, 2022 at 20:23
  • @FreddieManners Yes I see, thanks, answer fixed. Commented Oct 1, 2022 at 13:53
14

I can reproduce the problem when hyperref is loaded, because it redefines \label.

Workaround:

\documentclass{article}

\usepackage{hyperref}

\AtBeginDocument{
  \let\origlabel\label
  \renewcommand{\label}[1]{\origlabel{vol1:#1}}
}

\begin{document}

\section{A}\label{x}

\end{document}

The entry in the .aux file will be

\newlabel{vol1:x}{{1}{1}{A}{section.1}{}}
3
  • 2
    Sometimes I wish I am having your marble. Commented Sep 7, 2012 at 15:50
  • 6
    +1 Moral of the story: The OP should always supply a complete document that shows the problem (or rely on someone with a crystal ball:-) Commented Sep 7, 2012 at 15:51
  • @DavidCarlisle point well taken!
    – h0tw1r3
    Commented Sep 10, 2012 at 19:06

You must log in to answer this question.

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