# How to override \label to create two \newlabel's?

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).

-
Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. –  Marco Daniel Sep 7 '12 at 15:41
If I try your redefinition and do \label{x}, I get \newlabel{vol1:x}{{1}{1}} in the aux file. –  egreg Sep 7 '12 at 15:45

\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
\label{#1}%
\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}

-
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 Sep 10 '12 at 19:02

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}{}}

-
Sometimes I wish I am having your marble. –  Marco Daniel Sep 7 '12 at 15:50
+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:-) –  David Carlisle Sep 7 '12 at 15:51
@DavidCarlisle point well taken! –  h0tw1r3 Sep 10 '12 at 19:06