I have a large book broken into several chapters, each of which reference each other heavily. I would like to be able to distribute individual chapters as self-contained .tex files. I can do this except for the fact that references will not work unless I have the .aux file for the entire book (and I use the xr package for external references). I would thus like to be able to include the large .aux file in the individual chapter files when distributing the individual files. Is there a way I can do this?
[Tex/LaTex] Include aux file in tex source
auxiliary-filescross-referencing
Related Solutions
If you want to know what is in the aux-file: Open it in an editor and look it up. Actually every package (and you too) can write something to the aux-file if it want. So the aux-file normally contains quite a lot informations which are very document specific and which would cause troubles in another document.
So if you want to read an external aux-file you must be careful. E.g. an aux-file contains lines which should be written to the toc-file (\@writefile{toc}
) which you don't want to process in this case. The xr-package mentioned by Boris e.g. loops through external aux
-files and looks for \newlabel
commands but ignores everything else.
With the help of When is the aux file read and written? and Barbara Beetons kind assistance I was able to determine what goes wrong and how to prevent it.
This is the problem:
Normally I then don't work with the TeX engine on the console, but simply correct my error and restart.
This is what I should have done on the console:
- Type
?
+Return
to get an idea of what can be done. - Simply hit
Return
repeatedly until Tex is finished. - Type
X
+Return
to finish the run immediately. - Type
E
+Return
to have the cursor jump to the point where the error occurred.
What happens is, that TeX buffers the \write commands to the .aux file in two stages. First stage, it waits until a page is shipped out to get the page numbers right. Second stage, it waits until it sees fit to issue a write()
system call, to reduce the number of (potentially slow) system calls. This second buffer seemingly does not care, if the chunks it writes to file are self-contained legal TeX since it will complete the file before the TeX run is finished anyway.
In the environment of TeXShop however it is tempting to let TeX hang unfinished after an error. Thus the second output puffer is not flushed and the .aux file possibly left unfinished and in an illegal state.
My personal conclusion from this: Always let TeX finish.
The assumption that TeX will have a chance to finish it's run after it stopped for an error was very reasonable before windowed environments, where it became easy and normal to leave processes hanging in the background.
Things that could be done on software side to facilitate this user behavior:
- Change Tex's second buffer behavior to issue a
write()
system call in case of an error or to break only into TeX-legal chunks. - Adjust the environment (TexShop) to either clean the .aux file after a process abortion or provide default input to always correctly finish the process.
Things that the user can do, other than always letting TeX finish:
- Use
\include{...}
. That way the .aux files of the sections before are closed an finished by the time the error occurs. Chances are the .aux file of your current section isn't large enough yet and no TeX-illegal parts were written. - Hack something involving
\immediate\closeout\@mainaux
followed by an\openout\@mainaux
and a copy of the contents so they don't get overwritten. This will effectively flush the output buffer. Does not seem worth the effort though.
Best Answer
You can use either of the environments
filecontents
orfilecontents*
. Each of those takes one required argument, the name of the file to be created, and then then it creates that file with the contents of the environment.The unstarred version adds a few lines of comments at the beginning of the file describing how the file was created, the starred version doesn't add anything to the contents of the environment.
You use these environments before the
\documentclass
command. That is, you'd beging your latex file with, e.g.,and your
\documentclass
command comes after that.