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'm writing a document such as the following one:


Each section provides some content but requires other sections to be included before. For example, B could require A and C, C could require A, and A could require nothing. In this case, the above piece of code should just put A and C in the output document as B is before C. I tried something with the ifthen package, but I failed to obtain a solution that doesn't produce a compilation error if the order is wrong. In particular, it complains about undeclared boolean variables, as it may happen I check for a boolean variable before the section that declares it is loaded.

Any idea? Thank you.

share|improve this question
For something like declarations, why not take them out of the input files and put them in a common definitions file? – Charles Stewart Nov 23 '10 at 14:00
I don't really follow what it is you're trying to do. Why wouldn't your document have all of A, B, and C? – TH. Nov 23 '10 at 14:29
@TH.: it's just like dependency resolution for packages in a linux distribution, but for latex sections. – du9 Nov 23 '10 at 14:47
Hmm? You specify B but don't include it because it is not required by C, which comes later? That is not like any package system I know or can fathom. But given the dependencies you list, I could imagine wanting to output A and C if only C is asked for. – Harald Hanche-Olsen Nov 23 '10 at 15:24
When you encounter a dependency, write it to the aux file. In the second run, check the aux file for dependencies and also include them. If there is a long sequence of dependencies, it can take a while to reach the stable solution. – Aditya Nov 23 '10 at 16:10

Based on Caramdir's comment, this is how I will do this in ConTeXt. Perhaps someone can translate the solution to LaTeX




Then use


and each file can start with


Of course, this cannot check future dependencies.

share|improve this answer

Would something like the following work?

This is section A.

    This is section B.

  This is section C.

Here \ifx tests whether \sectionXincluded is defined and equivalent to \relax (instead of \relax on could use any other token).

EDIT: turning Aditya's answer into something non-ConTeXt-dependent:

%in main.tex
  {\expandafter\let\csname @@input@@#1@@\endcsname\empty

  {\expandafter\ifx\csname @@input@@#1@@\endcsname\empty\else\endinput\fi}

% A.tex
This is section A.

% B.tex
This is section B.

% C.tex
This is section C.
share|improve this answer

I think what you're after here is a topological sorting of your sections. This is actually fairly easy to do. Here's a fully LaTeX solution (no plain TeX macros used at all).

This depends on B and C.
This depends on C.
Uncommenting the \verb+\depend{B}+ causes a circular dependency.


The filecontents environment just causes TeX to write these files out verbatim.

\depend checks if its argument has already been input. If it has, nothing happens. If it has not, it checks to see if it was in the process of being \input. If so, then there's a circular dependency that needs to be resolved. If it isn't circular, then it inputs the file and marks it as complete.

share|improve this answer

Your Answer


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