17

I am not asking about \include or \input here. I know all about these.

Here is the setup. I'd like to be able to build as standalone one of my HW reports which sits in its own folder. Then I also like to include this latex file and others in the main latex file in some parent folder, for my whole course to assemble everything into one document.

The problem is that to build the one HW on its own, I need to have it as a complete separate Latex document. So, when it comes to importing it into the main document, I have to go comment out all the preamble from each HW document otherwise one gets an error.

Note that I have to use the import package for this, and not the \include, since the import package handles relatives paths correctly. So I can't use things like \includeonly{A/a} since a.tex include images using relative paths in them, and I'll get an error about image not find if I just use \input or \include.

I have to use \subimport.

Better to give a simple example than all this rambling. Given this layout

 main.tex
     +
     |
     A/
   a.tex
   a.png

a.tex is

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

and main.tex is

\documentclass{article}
\usepackage{graphicx}
\usepackage{import}
\begin{document}    
\subimport{A/}{a}  %notice that images included by a.tex are OK now
\end{document}

I can build a.tex ok. But to build main.tex, I have to comment a.tex as follows

 %\documentclass{article}
 %\usepackage{graphicx}    
 %\begin{document}    
 \includegraphics{a.png}
 %\end{document}

And to use a.tex again on its own, go back and uncomment the lines. This is getting tiring.

It will be nice if there was an option during an import to tell it to strip out all the latex preamble as it imports the file!

Any suggestions how to do this without this manual step?

I really do not want to collapse all my course tree into one folder just to do this. I like to keep each project and HW in its own folder so I can build each as a separate document, but I like to also be able to build the whole tree documents into one document.

4 Answers 4

12

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.

6
  • wonderful package! Thanks so much for making it. I tried it and it works like a charm. I'll update the question will example using it for future reference.
    – Nasser
    Jan 22, 2013 at 7:17
  • 1
    @Nasser: It would be better to keep the question in its original form and instead post your final solution as a self-answer. This way the question and solution is cleanly separated, which will help other people with the same issue more. Jan 22, 2013 at 10:32
  • There is a small issue that I just found about. For some reason tex4ht gives an error when it tries to process main.tex in the example I have above. I'll post what I tried and the error message. I wonder if you could please look at it. It might be a tex4ht own error and not. I was hoping to also be able to run these latex files by tex4ht to generate HTML. Latex2html does not even support the import package, so that is out of question to use.
    – Nasser
    Jan 22, 2013 at 10:46
  • There is also the subfiles package - it would be nice if someone summarized the differences.
    – marczellm
    Jan 22, 2013 at 11:30
  • @marczellm, thanks for the link to subfiles. I just tried it on the above example I have with main.tex and a.tex. Unfortunately it uses input on the subfiles, which means if a child document includes an image relative to it, then when building the parent document will give an error image not found. The advantage of import package is that it handles relative paths correctly. I'll add an example in a new update FYI, so it is all self contained.
    – Nasser
    Jan 22, 2013 at 11:58
4

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.

2

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.

1
  • 1
    This is a good answer also. I just saw it. I will try it. Yes, there is always this choice, between keeping things simpler, at the cost of may be additional files. But disk space is cheap, and I use Make anyway, so all this can be automated. Thanks for the idea.
    – Nasser
    Oct 31, 2013 at 6:26
-1

Just simply delete the documentclass{} etc. element from your sub latex files. If you are working on nested files on latex you do not need them in your sub latex files but in your main latex file.

2
  • Welcome to TeX.SE!
    – Mensch
    Dec 23, 2020 at 10:06
  • have you ever tried? Jan 20, 2021 at 9:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .