Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question led to a new package:
xcite

I want to reference to citations from other file. Similar to what xr package does to labels, but I want to apply it for the bibliography.

If I'm able to do \cite{someone} in the main file, and it creates let say the number [4], I want to do the same \cite{someone} in a second file, and also create the same number [4]. Similar to what xr does with the labels.

By the way, I tried to add the main.bbl file from the main file. However, that adds all the references to the end of the second file. I want to avoid this.

Is there any package that accomplish this?

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

Save the following in your working directory as xc.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xc}
         [2011/09/02 v0.1 eXternal Citations (DPC+EG)]
\def\externalcitedocument{\@ifnextchar[\XC@{\XC@[]}}
\def\XC@[#1]#2{{%
  \makeatletter
  \def\XC@prefix{#1}%
  \XC@next#2.aux\relax\\}}
\def\XC@next#1\relax#2\\{%
  \edef\XC@list{#2}%
  \XC@loop{#1}}
\def\XC@aux{%
  \ifx\XC@list\@empty\else\expandafter\XC@explist\fi}

\def\XC@explist{\expandafter\XC@next\XC@list\\}
\def\XC@loop#1{\openin\@inputcheck#1\relax
  \ifeof\@inputcheck
    \PackageWarning{xc}{^^JNo file #1^^JLABELS NOT IMPORTED.^^J}%
    \expandafter\XC@aux
  \else
    \PackageInfo{xc}{IMPORTING LABELS FROM #1}%
    \expandafter\XC@read\fi}
\def\XC@read{%
  \read\@inputcheck to\XC@line
  \expandafter\XC@test\XC@line...\XC@}
\long\def\XC@test#1#2#3#4\XC@{%
  \ifx#1\bibcite
    \bibcite{\XC@prefix#2}{#3}%
  \else\ifx#1\@input
     \edef\XC@list{\XC@list#2\relax}%
  \fi\fi
  \ifeof\@inputcheck\expandafter\XC@aux
  \else\expandafter\XC@read\fi}

\endinput
%%
%% End of file `xc.sty'.

Then say, in your derived.tex document,

\externalcitedocument{main}

(where main.tex is the document you are taking the citations from). Now

\cite{xyz}

in derived.tex will do what you want.

The interface is just the same as xr's: you can also specify a prefix

\externalcitedocument[A-]{main}

and use \cite{A-xyz} in derived.tex.

The xc.sty file has been obtained from xr.sty with some very simple transformations:

  1. xr -> xc
  2. XR -> XC
  3. \externaldocument -> \externalcitedocument
  4. \newlabel -> \bibcite
share|improve this answer
    
Thank you so much. That worked like charm. Why this isn't a default package? It's great! :) –  adn Sep 2 '11 at 12:04
2  
A package (called xcite) is on its way to CTAN –  egreg Sep 2 '11 at 13:22
1  
The xcite package is on CTAN, waiting for TeX Live to add it, too. –  egreg Sep 3 '11 at 20:56
add comment

Your Answer

 
discard

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.