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.
-
See an update here: tex.stackexchange.com/questions/346712/…– user31729Jan 2, 2017 at 16:00
1 Answer
Based on the assumption that each of the \ref
s inside a paper only refers to \label
s 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
}
\newcommand{\addlabelprefix}[1]{%
\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
\addlabelprefix{test}
\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
useshyperref
, 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
-
\documentclass{scrreprt} \input{classicthesis-config} \AtBeginDocument{ \let\oldlabel\label \let\oldref\ref } \newcommand{\addlabelprefix}[1]{% \renewcommand{\label}[1]{\oldlabel{#1-##1}} \renewcommand{\ref}[1]{\oldref{#1-##1}} } \newcommand{\removelabelprefix}{% \renewcommand{\label}{\oldlabel} \renewcommand{\ref}{\oldref} } \begin{document} \pagestyle{plain} \include{Chapters/1/Chapter01} \include{Chapters/2/Chapter02} \end{document} May 29, 2016 at 19:17
-
@Bishoksan: You need to place
\addlabelprefix{blah}
before\include{Chapters/2/Chapter02}
which should addblah
in front of every\label
and\ref
used in the second chapter.– Werner ♦May 30, 2016 at 14:19