Use Conditional (\ifdefined) in LaTeX – Tips
Inpendently typeset document fragments
- Inserting the fragments. Problem: \input does not handle relative paths in the inserted files correctly. Solution: package import, command \subimport{dir/}{file}
- Hiding the preamble. Problem: package ifthen provides commands for conditional text inclusion, but this package can only be included after the preamble. Solution: use \ifdefined
1 2 3 4 5 6 7 8 9 10 | % main document \newcommand{marker}{} % subdocument \ifdefined\marker % things that only appear in compound mode \else \documentclass[a4paper]{article} % things that only appear in stand-alone mode \fi |
- Chapter title. Problem: Using the fragment independently, one assigns a \title, inside the main document, this title is assigned via \chapter. Solution (the very first line of the subdocument should then be the title, it comes even before ifdefined and documentclass):
1 2 |
- Referring to the current document (“this chapter” vs. “this document”): TODO, can be done in a single location if one uses the marker.
Conditionals
FIXME: The above solution does not work well with Emacs which needs \chapter to construct the table of contents.

