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.

JPi
  • 13,595
  • I guess you're looking for the standalone package. – egreg Jun 22 '17 at 18:13
  • Great, thanks @egreg! Any gotchas, other than turning page size correction off? – JPi Jun 22 '17 at 18:24
  • 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
  • Thanks @DavidCarlisle, I'm aware of that possibility. When you say more robust, what issues are you referring to? – JPi Jun 22 '17 at 18:30
  • 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 https://tex.stackexchange.com/a/375349/36296 – samcarter_is_at_topanswers.xyz Jun 22 '17 at 18:38
  • Thanks @samcarter: unfortunately, that package isn't helpful. – JPi Jun 22 '17 at 18:53
  • @DavidCarlisle: I was thinking of just writing a LuaLaTeX routine that skips everything up to and including \begin{document} and everything from \end{document} to the end..... Should be pretty robust. – JPi Jun 22 '17 at 18:54
  • 1
    @JPi Out of curiosity, what is the problem with the answer I linked to? – samcarter_is_at_topanswers.xyz Jun 22 '17 at 19:28
  • @samcarter it complains about the \usepackage{amsmath} in the nested document (see the code fragment at the bottom of my question). It appears that one can't load packages in the nested document. – JPi Jun 22 '17 at 19:31
  • 1
    @JPi Thanks for your reply, I did not notice your edit before! – samcarter_is_at_topanswers.xyz Jun 22 '17 at 19:33

1 Answers1

10

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.

Bary12
  • 125
Willie Wong
  • 24,733
  • 8
  • 74
  • 106