[Tex/LaTex] Lyx cross-referencing between branches

cross-referencinglyxxr

I'm writing a paper in LyX, of which there are two versions — a short and a long version. I've created two branches in the (single) LyX file, and will switch them on and off appropriately.

I'd like to make statements in the short version of the form, "For details, please look at theorem [ref] in the full paper." The problem is that some theorems may be missing from the short version, and so the numbering may be off.

What is the recommended practice (in LyX) of solving this problem, i.e. making cross-references to external documents / different branches of the same document?

There is a previous question on this for LaTeX (Cross-referencing between different files ), but I don't see an immediate way to porting this solution to LyX. That is, use package xr, and have a short version branch inset containing \externaldocument{longversion} in ERT. But now, actually doing the compile will involve first exporting the long version to LaTeX, and then building the short version (and remember that LyX does the build in a temporary folder somewhere else). Or export both versions to LaTeX, and then manually invoke pdflatex. That leaves a bunch of temporary files in my pristine work folder, and kind of defeats all I like about LyX.

Best Answer

  1. Let our LyX document be called mypaper.lyx. Under “Document > Settings...” we add two branches: “fulltext” and “shorttext” both with filename suffix set to “Yes”. (As mentioned, “branches” are used in LyX to manage several different variants of one document.)

    available branches

    When only the branch “fulltext” is activated we call this the full version of the document; when only the branch “shorttext” is activated we call this the short version of the document.

  2. The branch “fulltext” shall contain all the details, e.g., a float table (with the label “tab:1”). Such details are placed inside a “branch box”, which is created by “Insert > Branch > 1. fulltext”. Furthermore, the branch “shorttext” will only contain a cross-reference “Table [Ref: tab:1]” to that table in the long version.

So far we got the following LyX document with two branches:

mypaper.lyx containing branch boxes

How to generate the short version mypaper-shorttext.pdf and the full version mypaper-fulltext.pdf?

  1. First, we activate the “fulltext” branch only and compile the full version (as PDF), to which we want to refer in the short version. The file name of the output will be mypaper-fulltext.pdf as you may see in the PDF viewer.

  2. Then, we add two lines to the LaTeX Preamble of mypaper.lyx.

    \usepackage{xr}
    \externaldocument{mypaper-fulltext}
    

    where mypaper-fulltext is the filename without extension of the just created output.

  3. Finally, we activate the “shorttext” branch only and compile the short version.

The outputs are:

fulltext and shorttext PDF


Warnings

  • Before compiling the short version we have to make sure that the full version is updated (that is, the full version was compiled) otherwise the references may not work correctly!
  • After every new start of LyX one has to compile the full version again. Only after that the short version can be created.
  • Changing only a label in an activated branch does not trigger LyX such that one can recompile the document. A little change of text is necessary for triggering.

Background – how does this work in LyX?

For compiling a document, LyX exports a document to a LaTeX file in a temporary directory and runs the program “pdflatex” (or similar) to create the output. Luckily LyX uses the same temporary directory when compiling the short version and the full version. Hence, the files mypaper-shorttext.tex, mypaper-shorttext.aux, mypaper-fulltext.tex, mypaper-fulltext.aux are in the same directory. By using \externaldocument{mypaper-fulltext} LaTeX will find the necessary files like mypaper-fulltext.aux. After a new start of LyX, the temporary directory is empty again. Hence, the full version needs to be compile first to create the files mypaper-fulltext.tex, mypaper-fulltext.aux.

Extensibility: If the “shorttext” branch contains labels that are referred by the long version then we need to add \externaldocument{mypaper-shorttext} to the LaTeX Preamble, too, and keep the short version compiled (updated), too. More complex scenarios with several branches are possible.

Related Question