27

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?

doncherry
  • 54,637
adn
  • 11,233

2 Answers2

23

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
egreg
  • 1,121,712
  • Thank you so much. That worked like charm. Why this isn't a default package? It's great! :) – adn Sep 02 '11 at 12:04
  • 4
    A package (called xcite) is on its way to CTAN – egreg Sep 02 '11 at 13:22
  • 3
    The xcite package is on CTAN, waiting for TeX Live to add it, too. – egreg Sep 03 '11 at 20:56
  • 1
    Are there plans to make this work with hyperref (similar to xr-hyper)? – Joachim Breitner Oct 24 '17 at 03:16
  • @JoachimBreitner It appears that xr-hyper is provided by hyperref. In fact, hyperref contains some code that complements xr-hyper. – Spherical Triangle Mar 29 '19 at 05:29
  • @egreg I fact the suggestion of Joachim can be easily done. First, the external documents and the prefixes must be the same so that the part encoded in hyperref does not need to be modified. Second, we must know how hyperref manages bibcite label. I could check the first point. But I have no idea for the second. Do you have any hint ? – Spherical Triangle Mar 29 '19 at 11:36
  • Hi @egreg, I think this only works with bibtex, correct? Does something similar exist for biblatex-biber combination? – Fato39 Feb 15 '21 at 10:05
  • @Fato39 The xcite package has actually been dismissed as its functionality has been taken over by xr. There's no support for biblatex, sorry. – egreg Feb 15 '21 at 10:09
  • I understand, thanks, @egreg! – Fato39 Feb 15 '21 at 10:14
1

BibLaTeX/Biber

The package xcite is now a part of xr and it works just the way you want if you use bibtex. If you use BibLaTeX/Biber, the bibliography is not compiled using \bibitems.

Instead, you can first compile both the main and the derived document and then simply copy main.bbl and rename it derived.bbl. Recompile the derived document, skipping Biber this time and you will get the references numbered as in the main document.

The downside of this manual method is that you always need to remember to copy the main bibliography file, whereas xr would take care of everything for you automatically.

Fato39
  • 296