How to import separate standalone latex documents and assemble into one document

The standalone package got written to do exactly that, i.e. remove preambles from sub-files. For this it redefines \documentclass (after \begin{document}) to strip everything until \begin{document} and ignore any additional document environments. The package works fine without the accompanying standalone class. However, I didn't explicitly tested it with the import package yet, but it should work.


As suggested by Matrin above, I moving my updates into separate answer place to leave the original question as is.

Thanks to Martin's package, this is an example of how to do it:

main.tex

\documentclass{article}
\usepackage{standalone}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{import}
\begin{document}    
   \subimport{A/}{a}        
\end{document}

and a.tex

\documentclass{article}
\usepackage{graphicx}
\usepackage{listings}    
\begin{document}    
  \includegraphics{a.png}
  \lstinputlisting{a.txt}
\end{document}

Now I can do pdflatex a.tex and also pdflatex main.tex with no editing or commenting out anything. Both work!

Just make sure to include all the packages included in all the children latex document in the main latex document. That is all.

Thanks again for Martin's standalone package.

second update

For some reason, tex4ht gives an error on the main.tex file above. But does not give an error on a.tex. Here is the error using the same files shown in the above example

>htlatex main.tex
This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian)
 restricted \write18 enabled.
entering extended mode
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, lo
aded.
(./main.tex (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf/tex/generic/tex4ht/tex4ht.sty)
(/usr/share/texmf/tex/generic/tex4ht/usepackage.4ht)
(/usr/share/texlive/texmf-dist/tex/latex/standalone/standalone.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texlive/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
(/usr/share/texlive/texmf-dist/tex/latex/xkeyval/xkeyval.sty
(/usr/share/texlive/texmf-dist/tex/generic/xkeyval/xkeyval.tex
(/usr/share/texlive/texmf-dist/tex/generic/xkeyval/keyval.tex)))
! Extra \else.
l.227     \else

? 

I wonder if these is a fix for this. I'd really like to be able to generate an HTML from these latex files as well if possible.

ps. FYI, I just sent an email on this to the tex4ht mailing list. This might be just a tex4ht issue.

Update 3

To answer the comment on using subfiles. It does not seem to support relative path as it ends up using input. Hence I can't use it. Here is a self contained example using the same tree structure as above but now using subfiles instead of standalone

main.tex

\documentclass{article}
\usepackage{subfiles}
\usepackage{listings}
\usepackage{graphicx}
\begin{document}    
   \subfile{A/a}        
\end{document}

and a.tex in folder A below the above is

\documentclass[../main.tex]{subfiles}
\begin{document}
\includegraphics{a}
\lstinputlisting{a.txt}
\end{document}

(folder A contains a.eps and a.png and a.txt which are read using relative path to a.tex).

a.tex will compile OK! But not main.tex:

>latex main.tex 
This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/Debian)
 restricted \write18 enabled.
entering extended mode
....
(./A/a.tex    
! LaTeX Error: File `a' not found.    
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.4 \includegraphics{a}

? 

Conclusion: can't use subfiles package as is.


I spent some time exploring the various methods of doing his kind of thing last year and eventually came to the conclusion that all the packages for doing this are fundamentally taking the wrong approach and you're better off not using them at all.

Instead, create entirely separate "content" files from "framework" files so that no file contains both text you want to appear in your document and preamble. So in the simplest case, you might do something like this:

framework.tex

\documentclass{article}
\begin{document}
  \input{content.tex}
\end{document}

content.tex

Ipsus wotsisus thingyus \emph{bimble}

So content files will only work when included in framework files and all the display options get set from the the framework file If you then separate your preamble to a new file you can include you can include it in different framework files collecting particular chapters, etc without any difficulty. This approach has several advantages:

  1. It doesn't require any special packages to be juggled.
  2. It entirely separates your content from it's display options; putting your content into a new setup is as simple as creating a new framework file.
  3. You don't have to repeat yourself at the start of every sub-file.

And, in your case, you can use \import as you need. The disadvantage is that you have more files overall. I can't say I see much problem with that YMMV.

Tags:

Import