TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Possibly related: About \include in LaTeX – Werner Aug 31 '11 at 17:31
up vote 3 down vote accepted

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.

share|improve this answer
Thanks Joseph; I didn't think there was a built-in concept, but was just curious. – cm2 Aug 31 '11 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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.