[Tex/LaTex] Using the xr package in LyX (for cross-referencing an external LyX document)

cross-referencingexternal fileslyx

Question: What steps are required to use the xr package in LyX in order to reference items in other, independent LyX documents?

Steps already taken:

  1. The tools package (that contains the xr package) has been installed in MikTeX.
  2. The xr package has been used in the referencing document (by adding \usepackage{xr} to the referencing document's preamble).
  3. A pointer to the referenced document has been set in the referencing document (by adding \externaldocument{referenced.lyx} to the referencing document's preamble).
  4. A label in the referenced document has been cross-referenced in the the referencing document using the \ref tag.

Observed (undesired) behavior: The reference appears as "BROKEN" in the LyX editor. The PDF output shows "??" where the reference number is expected.

Relevant links:

  1. A thread on a LyX usegroup
  2. Another thread on a LyX usegroup

Software used: LyX + MiKTeX + BibTeX on Windows 7

My level of Lyx/LaTeX proficiency: Beginner+

Best Answer

I have been trying to get xr and xcite working for past couple days in LyX and this post and the links it cites were about all I could find as references. Since the current answer doesn't fully explain what is going on in LyX, I'll give what works for me for using xr (or xcite) with LyX:

  1. Put

    \usepackage{xr}
    \externaldocument{referenced}
    

    into the LaTeX preamble in Document->Settings of the referencing document (I will call it referencing.lyx).

  2. Build the referenced document to obtain referenced.aux.

    You can either do File->Export to export referenced.tex and run latex on it to generate referenced.aux or you build the document normally in LyX with the View command. If you use the View command, you will need to find LyX's temporary directory where it ran latex. This directory should be a subdirectory of the Temporary directory listed under the Paths section of LyX's Preferences. The LyX subdirectories (there might be more than one depending on how many document you have opened and rendered during the session) will have names starting with lyx_tmpdir and will contain subdirectories with names beginning with lyx_tmpbuf. One of these lyx_tmpbuf directories should contain referenced.aux (if you start a new LyX session and View referenced.lyx, it should be easy to find since there shouldn't be a lot of extra LyX directories). (Itai, LyX's temporary directory is usually located in a system temporary directory that does not show up in typical file searches, so that might be why you could not find any .aux files).

  3. Once you have referenced.aux, just save it in the same directory as referencing.lyx and LyX should find it when it renders referencing.pdf.

Other usage notes:

  • LyX does not have native support for xr or xcite. Initially, the labels in referenced.lyx will not appear as options in the cross-reference dialog in LyX. To reference items from referenced.lyx such as referenced_label1, you can either do "Insert->TeX Code" and put \ref{referenced_label1} in by hand, or you can keep referenced.lyx open, right-click on the label you want to reference and choose "Copy as reference", and then paste into referencing.lyx at the point where you want to make the reference.

  • Because LyX does not have support for xr, pasted labels will show up in LyX with a BROKEN: prefixed in front of the label. However, they should still be rendered properly in the pdf that is produced. The BROKEN: prefix just means that LyX can't find that label defined within referencing.lyx. One way to get around this is to put referencing.lyx as a child document (Insert -> File -> Child Document) within a LyX note. Then its labels will show in the cross-reference dialog in LyX, but because the inclusion is within a LyX note, it won't get rendered in the .pdf.

  • Pasted labels will appear in LyX's cross-reference dialog (as BROKEN: references). Don't try to reference them from there though because you end up with a reference to a reference.

  • With xcite, if you have a separate .bib file for referenced.lyx (call it referenced.bib), you can add referenced.bib to referencing.lyx's bibliography and cite the items normally (rather than copying and pasting references out of referenced.lyx). Then, when you want to render the referencing.pdf, remove referenced.bib from the bibliography. This should break all of the citations within referencing.lyx but they should still be found by xcite.

  • While searching for how to use xr with LyX, I saw references to a combo document option in LyX that would allow it to use the same temporary directory for two LyX files (in which case referenced.aux file could be made available to referencing.lyx without having to put it in the working directory as described above). As far as I can tell, this feature was removed before LyX 2.0 and is not an option any more.