5

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.

migrated from stackoverflow.com May 28 '16 at 16:45

This question came from our site for professional and enthusiast programmers.

8

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:

enter image description here

\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? – Bishoksan May 29 '16 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 '16 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 – Bishoksan May 29 '16 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} – Bishoksan May 29 '16 at 19:17
  • @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 '16 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.