I'm trying to solve a problem that's come up in producing a large document that I'm breaking up into smaller ones. The basic structure is this.

A main document (main.tex) that includes formatting info for the whole project, and individual files for chapters (part1.tex, part2.tex, etc).

I need to produce a lot of cross-references, mostly to enumerate items within a part, but ideally also to some elements across parts.

My basic workflow is editing LaTeX documents in emacs using the RefTeX capabilities to produce cross-references. My problem is that when RefTex automatically supplies me with labels (and I'm not willing to give up that convenience), it'll start numbering the labels from scratch for each part.

Thus, part1.tex might look like this.

\item first thing in part 1.\label{item:1}
\item second thing in part 1.\label{item:2}
Reference to \ref{item:1}

Correspondingly, part2.tex might look like this.

\item first thing in part 2.\label{item:1}
\item second thing in part 2.\label{item:2}
Reference to \ref{item:1}

(NB: I've got the \setcounter command just to ensure that the numbers differ between the two parts so I can see whether the cross-referencing works properly.)

The main document that calls these is the basic main.tex.



The problem is that, because RefTeX starts over in the labels it provides, the cross-references are screwed up. Both of the references in part1.tex and part2.tex shown above will be "3", the number of the enumerate item the last \label{item:1} is attached to.

In the past, I've simply brute-forced the issue. When it came time for a final compile, I search-and-replaced all of the labels in my code to include unique prefixes for each input-file. For example, all of the item:xx labels in part1.tex would be turned into item-part1:xx labels, mutatis mutandis for part2.tex. But that is ugly, and surely there must be a better way.

I don't know if there are packages that solve this problem for me (I looked at xr, but couldn't get it to work out for me), or if there is a way to customize RefTeX so that, when it automatically produces labels within one of the parts, it's automatically aware of the labels that are in use in all of the other parts and hence ensures that no duplicates will be created.

For what it's worth, I'm running TeX on OSX, in case that matters.

  • 6
    You approach of renaming the labels is just as bad as not using labels and hardcoding the numbers that LaTeX generates for the labels. I'd just use meaningful names for the labels, as opposed to using meaningless labels of the form label:<number>. – user10274 Jan 13 '12 at 6:35
  • the only result from running texdoc reftex is a readme file from the cjk package, but it indicates pretty clearly that reftex is an emacs function. thus it should be possible to enhance it to add a suitable prefix to each label. but you'll need to find a good emacs-lisp programmer. try the stackoverflow.com if you don't get a good answer here. i'm adding the tag emacs to give your question better visibility. – barbara beeton Jan 13 '12 at 17:12

I've figured out the solution, and I'm posting it here for completeness.

RefTeX in conjunction with AucTeX can be made aware of labels in all of the documents that make up a multi-document structure of the sort I'm using. One just has to include pointers to the master file in each of the files making up the structure, in this example, main.tex, part1.tex, part2.tex:

%%% Local Variables:
%%% TeX-master: "main"
%%% End:

Tip: Putting the declaration into the master file (in this case main.tex) is crucial. I started looking around the web for a solution to my problem because on previous attempts, I had only included pointers to the master file in the input files, not the master itself.

| improve this answer | |

A \ref to the same \label will not give different results. Your \jobname.log file should contain a warning,

there were multiply defined labels. 

The only option is to change the \label. For large documents you just need to come up with a convention that makes sense to you, and would be fairly simple for others to understand.

For example, if your part1.tex is all about exponential functions, you might use the following labels

\label{exp:item:description}  % for items
\label{exp:fig:description}   % for figures
\label{exp:tab:description}   % for tables
\label{exp:sec:description}   % for sections

and so on, where description is something relevant to the object you are labelling. If part2.tex is all about polynomial functions, then you might use the same structure as above, except write poly instead of exp.

The setup you described sounds comparable to a situation in which you put two people named Bob into a room, call out the name Bob, and expect the correct person to answer.

| improve this answer | |
  • What you suggest is exactly what I do by hand at the time of compile---add a prefix of the sort you suggest to each of the labels. Unfortunately, it isn't feasible for me to generate such labels by hand at the time that I'm writing the code because I use up to a hundred numbered examples in each part (in the manner of a linguist), and hence RefTeX's capability to ensure that there aren't any duplicate labels is extremely useful, so useful that I don't want to forgo it. And you're right, of course, that having the same label in two parts won't work. I was hoping for a more elegant solution. – Bernhard Jan 14 '12 at 20:43

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.