Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm writing a document such as the following one:

\section{A}\input{A}
\section{B}\input{B}
\section{C}\input{C}
...

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
2  
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
show 7 more comments

3 Answers

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

\unprotect
\def\markedinput#1%
  {\letvalue{@@input@@#1@@}\empty
    \inputgivenfile{#1}}

\def\checkdependency#1%
  {\doifundefined{@@input@@#1@@}\endinput}

\protect

Then use

\section{A}\markedinput{A}
\section{B}\markedinput{B}
\section{C}\markedinput{C}

and each file can start with

\checkdependency{A}
\checkdependency{C}

Of course, this cannot check future dependencies.

share|improve this answer
add comment

Would something like the following work?

\let\sectionAincluded\relax
This is section A.

\ifx\sectionAincluded\relax
  \ifx\sectionCincluded\relax
    \let\sectionBincluded\relax
    This is section B.
  \fi
\fi

\ifx\sectionAincluded\relax
  \let\sectionCincluded\relax
  This is section C.
\fi

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
\makeatother
\def\markedinput#1%
  {\expandafter\let\csname @@input@@#1@@\endcsname\empty
    \input{#1}}

\def\checkdependency#1%
  {\expandafter\ifx\csname @@input@@#1@@\endcsname\empty\else\endinput\fi}
\makeatletter

% A.tex
This is section A.

% B.tex
\checkdependency{A}
\checkdependency{C}
This is section B.

% C.tex
\checkdependency{A}
This is section C.
share|improve this answer
add comment

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).

\begin{filecontents}{A.tex}
\depend{B}
\depend{C}
\section{A}
This depends on B and C.
\end{filecontents}
\begin{filecontents}{B.tex}
\depend{C}
\section{B}
This depends on C.
\end{filecontents}
\begin{filecontents}{C.tex}
%\depend{B}
\section{C}
Uncommenting the \verb+\depend{B}+ causes a circular dependency.
\end{filecontents}

\documentclass{article}
\usepackage{etoolbox}
\usepackage{currfile}
\newcommand*\depend[1]{%
    \ifcsdef{depend@#1}{}{%
        \ifcsdef{resolving@#1}{%
            \circulardependencyerror
        }{}%
        \cslet{resolving@\currfilebase}\relax
        \input{#1}%
        \csundef{resolving@\currfilebase}%
        \cslet{depend@#1}\relax
    }%
}
\begin{document}
\depend{A}
\end{document}

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
add comment

Your Answer

 
discard

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