TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a set of documents each with references that look like:


and so on. This is no problem when building each document separately,but creates many duplicate references when using \include{} to create a composite document. Is is possible to redefine the \ref and \label commands to automatically include the source filename in the reference and label. Otherwise, I would have to hand-edit all my source files with new references to solve the duplicate references problem in the composite document. For example, document1.tex would have references like


and document2.tex would likewise have the following:


But I would like this to happen automatically by re-defining the commands instead of having to hand-edit each of the source files.

Any tips appreciated!

share|improve this question
Related: Cross-referencing between different files – Werner Mar 12 '13 at 17:18
up vote 5 down vote accepted

You can indeed redefine locally (in each included .tex file) the \ref and \label commands. Here is an example:



Eq. \ref{eq} 


Eq. \ref{eq} 




In your case you can put the first two lines (which save the original \ref and \label commands) in your main file, and then put the \renewcommand at the beginning of each included file, as I was trying to mimic here using filecontents. Here I am giving manually the text to append to each label in the included file. Replacing stuff with \jobname will not automatically append the name of the included file, because \jobname will be the name of the main file, but perhaps it can be patched (Maybe this can help you). There are probably cleaner solutions, but this seems to work.

share|improve this answer
Thanks! That gets me part of the way there. – reckoner Mar 13 '13 at 13:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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