# Can a file know if it has been \include'ed or \input'ed?

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.

-
Possibly related: About \include in LaTeX –  Werner Aug 31 '11 at 17:31

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.
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:
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 Aug 31 '11 at 17:58