Is there a simple way to export a self-contained source for a large LaTeX document? I would like it to export all of the class files, etc as well as the sources that I have written in order to create a self-contained source.


LaTeX offers the environment filecontents and filecontents* which allow you to add external files to your .tex file. The environments have to be used before the \documentclass declaration. If you process the .tex file the files are then written to the local directory (but only if there isn't already a file with that name) and thus can be used as part of the document.


... my changes ...


The filecontents environment writes a short header (behind % signs at the top of the generated file, the star form suppresses that header. The latter is useful if you want to generate a file that needs to be kept unchanged. What is not possible is to add real binary files to your document in that way (e.g., a .jpg graphic).

  • In a pinch, you could uuencode a jpg graphic. Also, packages snapshot and bundledoc may offer some automation. – Koji Mar 13 '12 at 20:34
  • Thanks! I may not have asked the right question, so please let me rephrase. I have, directly and indirectly, used quite a few packages in the source of my book. I want to have TeX export all the packages that I used so that I have a self-contained source to send to the publisher. Is this feasible? I can trace everything down by hand, presumably, but this would be very tedious. – Bob Harper Mar 13 '12 at 20:40
  • LaTeX has \listfiles that gives you at least the list of all files used (I think there also a few scripts around to then collect them from the file system and put them into a zip or the like) – Frank Mittelbach Mar 13 '12 at 20:47
  • @Koji sure but you can also put everything in a zip :-) the point is that it normally runs without any manual steps and unless you use system extensions and have a uudecode handy such an encoded file can't be directly used – Frank Mittelbach Mar 13 '12 at 20:50
  • If you include \usepackage{filecontents} then the files will be overwritten. This is very useful when composing test cases so that the file on disk is always the latest version. – Peter Grill Mar 15 '12 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.