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](https://i.stack.imgur.com/AsPsE.png)
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.
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:
How to generate the short version mypaper-shorttext.pdf
and the full version mypaper-fulltext.pdf
?
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.
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.
Finally, we activate the “shorttext” branch only and compile the short version.
The outputs are:
![fulltext and shorttext PDF](https://i.stack.imgur.com/EvnKn.png)
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.
First, I think it is good style to consolidate similar preambles. If, e.g., the subfiles define macros for special symbols in their preambles, it would be a good idea to outsource all such definitions into, say, mysybols.sty
, that then can be loaded by all subfiles, and easily consolidated into a main preamble.
Apart from that, you might be looking for the standalone
package. This package has the possibility to execute preambles of subfiles, see example below. Note however, that conflicting definitions or package options in the sub-preambles can not be resolved. This leaves the problem of multiply defined labels. I propose a hack that makes those labels unique by prepending the chapter number. For that, the definition of \label
and \ref
are altered. If you use other cross-referencing macros that don't fall through to \label
and \ref
like \cref
from the cleveref
package, they have to be treated in the same way. Note that redefinition of \label
and \ref
comes after \begin{document}
here to have those redefinitions even after hyperref
changed them.
Here are two small subfiles, both defining a macro, and both using a label fig:1
.
subfile1.tex
is
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\newcommand{\foo}{first subfile}
\begin{document}
\section{First section of \foo}
\begin{figure}
\caption{A figure of \foo}\label{fig:1}
\end{figure}
See Figure~\ref{fig:1}.
\end{document}
subfile2.tex
is
\documentclass{article}
\usepackage[colorlinks]{hyperref}
\newcommand{\baz}{second subfile}
\begin{document}
\section{First section of \baz}
\begin{figure}
\caption{A figure of \baz}\label{fig:1}
\end{figure}
See Figure~\ref{fig:1}.
\end{document}
Now the main document:
\documentclass{report}
\usepackage[colorlinks]{hyperref}
\usepackage[subpreambles=true]{standalone}
\begin{document}
\let\oldlabel\label
\renewcommand{\label}[1]{\oldlabel{\thechapter:#1}}
\let\oldref\ref
\renewcommand{\ref}[1]{\oldref{\thechapter:#1}}
\tableofcontents
\chapter{First chapter including first subfile}
\input{subfile1}
\chapter{Second chapter including second subfile}
\input{subfile2}
\end{document}
If the sub-preambles are conflicting, and you really don't want to tidy that up, your best option is probably joining the pdf files with the pdfpages
package, or even just pasting the pdf files together with a tool like pdfjoin
. Note however, that with pdfpages
you lose all hyperlinks from the sub-pdfs.
Best Answer
It is not exactly clear what you want to achive. The following code includes all files
<num>.pdf
where<num>
goes from start to end value, these values can be easily modified in the code. For details about how\loop...\repeat
works, see Teach me how to use \loop...\repeat constructs, etc .