1

I'd like to nest load one document inside another, skipping the preamble and \begin{document} and \end{document} of the nested document.

So

\documentclass{article}
% load some packages here
\begin{document}
\loaddocument{otherdocument}
text
\end{document}

should be the same as if \loaddocument were replaced with \input except that only the bits between \begin{document} and \end{document} of otherdocument.tex are loaded.

I could probably achieve this using LuaLaTeX but I can't be the first to be looking for this functionality yet I can't find a solution on this forum. Is there an existing solution?

****** EDIT ******

I thank Sam Carter for pointing out subfiles but that package doesn't allow packages to be loaded in the preamble. If the nested file is like this

\documentclass{article}

\usepackage{amsmath}

\begin{document}
\begin{equation}
x=\quad y.
\end{equation}
\end{document}

then compilation stops, so the subfiles package won't do.

The closest to what I have in mind, judging by its documentation, is newclude but that package is old and the documentation claims it provides an \includeenv command, which LaTeX doesn't recognize. But, unless something changes, I'll accept that answer and write my own LuaLaTeX routine.

* SECOND EDIT *

fwiw, I have written a simple LuaLaTeX routine that takes care of the problem.

marked as duplicate by egreg, Stefan Pinnow, Alan Munn, user36296, Mensch Jun 22 '17 at 19:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    there are several packages that do this but normally it's simpler and more robust to put the body of the otherdocument in a separate file that can be input into both its own preamble in otherdocument.tex and in to the current document. – David Carlisle Jun 22 '17 at 18:29
  • 1
    any package doing anything else has to make some local definitions to make the preamble be ignored, there are several approaches one could take for that (the simplest being just \def\documentclass#1\begin{\begin} plus a local definition of the document enviornment to do nothing, but any such local definition has the possibility of breaking something, that's just how tex is:-) – David Carlisle Jun 22 '17 at 18:34
  • 1
    @JPi maybe have a look at tex.stackexchange.com/a/375349/36296 – user36296 Jun 22 '17 at 18:38
  • 1
    @JPi Out of curiosity, what is the problem with the answer I linked to? – user36296 Jun 22 '17 at 19:28
  • 1
    @JPi Thanks for your reply, I did not notice your edit before! – user36296 Jun 22 '17 at 19:33
6

Five Options:

  1. The standalone package.
  2. The subfiles package.
  3. The docmute package.
  4. The combine package.
  5. The newclude package.

The standalone package is designed to allow portions of a larger document to be extracted and compiled alone, yet maintaining its place in the larger document. As such the sub-documents need to use the standalone document class.

The subfiles package is a different implementation of the same main idea: the child documents are parasite subfiles document class that should know about the main document to take the preamble when compiled alone (whereas the standalone could use a Borg assimilation strategy).

The docmute is yet another implementation of the same main idea, but child documents can use any document class (there are no docmute class) that are truly independent files. Simply, the preambles of child documents are discarded when combined, so your responsibility take care that the main preamble have all packages needed for all the subdocuments. Use a common \input{preamble} avoid troubles in this sense.

The result could be the same for simple documents with the threee pacages, but whilst the first have many useful options, in certain situations some alternatives could be better (Fran note: Do not pay me much attention, but I vaguely remember some problem working with both import and standalone that was solved switching to docmute).

The combine package is designed for bundling of papers into a larger publication (such as for the preparation of conference proceedings). All individual documents should be of the same document class (but unlike standalone they can all be article, or report etc.) There is no need to change the original individual documents at all (unlike the case of using standalone to assemble multiple existing files). However, by design the bibliographies, table of contents, section numbering, etc. are local to each individual paper. There are also a journal document class have the same purpose, but it is specific for the paper (article-like) document class.

The newclude package defines an experimental command \includeenv which includes exactly the content within a single environment in the sub-documents. If multiple instances of the environment were used, you can optionally specify which instance to include. By specifying as the name of your environment document, it should do what you want as described in your question. (But if you loaded any packages in your sub-document, it is your responsibility to load it also in the master.)

As you can see from reading their documentations, most packages are designed for different use-cases. It is up to you to decide which fits your need best.

  • 1
    (I do not claim that these are the only three options, but only the three I know about, packaged on CTAN.) – Willie Wong Jun 22 '17 at 18:36
  • Thanks Willie.... I'll accept it, but I'll be doing something else. – JPi Jun 22 '17 at 18:58
  • 1
    Add docmute to the list. – Fran Jun 22 '17 at 19:26
  • @Fran: I am not familiar with docmute, if you don't mind, can you edit the answer to include it with a short paragraph about its pros and cons? Thanks! – Willie Wong Jun 23 '17 at 13:18
  • 1
    @WillieWong Done. I added some more. Please modify as you like. – Fran Jun 23 '17 at 17:38

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