I have a somewhat large User's Guide for a code I've been working on with collaborators. Each chapter is in a subdirectory of the top directory, which includes the .tex file that grabs and stitches together all the individual chapter .tex files into the complete document.

Sometimes it is useful to compile and look at just one chapter individually. This got me curious to see if (La)TeX can handle the following: Can (La)TeX determine during compilation if a .tex file was \input'ed or \include'ed from another file?

An example use case would be to only include a \documentclass, \begin{document}, etc. if the file were not \include'ed or \input'ed from another file. Yes, I understand this can be worked around in other ways, i.e. smart use of \includeonly, but I'm curious if this can be done in a more automatic fashion.

For those familiar with Python, what I'm really looking for is something equivalent to the usual

if __name__ == "__main__":

check to see if the current file is the main module, or if it has been import'ed by another file.


2 Answers 2


The \input primitive (\@@input in LaTeX2e) simple includes the subfile 'here', as though it was part of the main file. thus the best that can be done is to build mechanisms on top of TeX to track subfiles (which is what LaTeX does). There is no built-in concept of being a subfile.

  • Thanks Joseph; I didn't think there was a built-in concept, but was just curious.
    – cm2
    Commented Aug 31, 2011 at 20:14

Instead of deciding whether or not a file is \include'ed or \input'ed, there are other option that allow you to individually compile "subfiles" of some main/master .tex file. The subfiles documentclass. As mentioned in the class documentation, your main and "subfile" documents have the following structure:

Subfiles documentclass structure in master/subfiles

Alternatively, the combine documentclass provides a similar functionality. Read the package documentation for more information.

  • Thanks for the introduction to the subfiles package and document class - that is another one of the "work around" methods I mentioned in the posting. It is not necessary, but I'm interested if a given file knows it has been included'ed or input'ed. Just a curiosity :-).
    – cm2
    Commented Aug 31, 2011 at 17:58

You must log in to answer this question.

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