I am currently trying to write a book containing multiple chapters, with each chapter saved in a separate tex file. In each chapter, I have similar sections with label "sec:introduction", "sec:methods", "sec:conclusions", etc. When combining these chapters with \input command, I got error message saying labels are "multiply defined". I am wondering if it is possible to somehow let latex compiler realize that they are in different files and will not conflict with each other in any references (denoted by \ref)? I know changing these labels altogether would be a choice, but there are other multiply defined labels related to equations, figures, etc which could take a while to fix.

Any suggestions would be greatly appreciated, thank you!

  • 1
    each label should be unique!
    – Zarko
    Mar 21 '20 at 19:57
  • @Zarko Right, I am wondering if there is similar mechanism to C++ namespace, where there could be multiple same labels, but in different "namespace"s (meaning different files). Mar 21 '20 at 19:58
  • it should be easy enough to do a one-off edit to change \label{ and \ref{ to \label{x and \ref{x in a document so that document has a unique set of labels starting x it should just be a single edit in your editor it doesn't matter how many things are labelled Mar 21 '20 at 20:03
  • In theory it would be possible to add a wrapper around \input that locally redefines \label and \ref to be unique. The downside is that you can't \ref \labels from a different file. (I didn't know good search terms here, but I think something like this tex.stackexchange.com/q/365799/35864.) But a search-and-replace to make sure the labels are unique in the source is probably the best way here.
    – moewe
    Mar 21 '20 at 20:07
  • 1
    A slightly simpler version of the idea I linked above: tex.stackexchange.com/q/517729/35864
    – moewe
    Mar 21 '20 at 20:18

Your Answer

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

Browse other questions tagged or ask your own question.