Simple preprocessor for latex: detect whether you are an included file or being compiled stand-alone

2.7k views Asked by At

I work on a huge script in \latex.

I want to be able to compile each of the chapters as stand-alone, because it is easier for hacking sessions with Latex.

On the other hand, I would like to maintain a document which encompasses the whole script so far written.

I do not know how to maintain both these documents without permanently annoying overhead, as a tex-file can either be written stand-alone or to be included.

It would be great help to have something a Latex-preprocessor available that is capable of C-like #define and #ifdef-#else-#endif statements. This would facilitate writing to a great extent. Do you know whether something like this exists in latex, or how can you do something equivalent? Google hasn't supplied me with a satisfying answer to this.

EDIT: Some remarks in order to avoid misunderstandings: I am aware of the very simple built-in TEX-preprocessor, but these commands don't work properly as I expected. Hence a reference to these will not help me out.

The chapters in my script shall look something like this (Pseudo-Code)

IF being_just_included defined
   %No header here, and document has already begun
ELSE
\input{common_header.tex} %Header things all my documents use
\begin{document}
ENDIF

%%% Lots of stuff

IF being_just_included defined
   %Nothing to do here
ELSE
   \end{document}
END IF

In contrast, my complete script source file should look like this

\input{common_header.tex}
DEFINE being_just_included
\begin{document}
\input{preamble.tex}
\input{first_chapter.tex}
\input{second_chapter.tex}
\input{third_chapter.tex}
\end{document}

Could you post a code which performs something like this?

2

There are 2 answers

1
Brent.Longborough On BEST ANSWER

One way of doing this is to use the standalone package, intended for this specific purpose.

You may also care to browse through, and perhaps join, TeX and Friends.

1
shuhalo On

Thank you very much for this package and the hint to the forum.

After some time I've figured out there exists a tex preprocessor, which is similar to the CPP. Maybe not well-engineered, but it serves my purpose quite well.

The magic lines are:

\def\justbeingincluded{justbeingincluded}

\ifx\justbeingincluded\undefined

\fi

to be used appropiatly within the respective source files.