# Reusing same labels across different chapters

I am writing my thesis which uses classicthesis.sty style. The thesis brings together a collection of papers and put them as individual chapters (all the chapters are included in main.tex). Some of the labels for figures, sections, tables etc. in two or more papers are the same and putting them together causes a problem. I would like to find a clean way of resolving this problem.

Based on the assumption that each of the \refs inside a paper only refers to \labels inside the paper itself, you can introduce \label prefixes that should make the references unique:

\documentclass{article}

\AtBeginDocument{% ...if you're using hyperref
\let\oldlabel\label% Copy original version of \label
\let\oldref\ref% Copy original version of \ref
}

\renewcommand{\label}[1]{\oldlabel{#1-##1}}% Update \label
\renewcommand{\ref}[1]{\oldref{#1-##1}}% Update \ref
}
\newcommand{\removelabelprefix}{%
\renewcommand{\label}{\oldlabel}% Restore \label
\renewcommand{\ref}{\oldref}% Restore \ref
}

\begin{document}

% Hypothetical first paper

\section{A section}\label{sec:section}
See section~\ref{sec:section}.

% Hypothetical second paper

\section{Another section}\label{sec:section}
See section~\ref{sec:section}.

\end{document}‎


Note that the references work as expected despite the same label sec:section.

• Hi Werner, Many thanks for your answer. Unfortunately the solution did not work for me. I am using a custom thesis style (classicthesis) from ctan.org/tex-archive/macros/latex/contrib/classicthesis, and the document class is scrreprt. Could this be the reason? May 29, 2016 at 10:19
• @Bishoksan: classicthesis uses hyperref, which in turn redefines the way \label and \ref works. So, you'll have to delay the storing of \oldlabel and \oldref until \AtBeginDocument. I've updated my answer to reflect this.
– Werner
May 29, 2016 at 17:38
• sorry to tell you that it still does not work. I have the following content in my main.tex now. I seriously cannot figure out what could be the problem. Could you help? Thanks May 29, 2016 at 19:13
• @Bishoksan: You need to place \addlabelprefix{blah} before \include{Chapters/2/Chapter02} which should add blah in front of every \label and \ref used in the second chapter.
– Werner
May 30, 2016 at 14:19