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
[cc lang="latex"]
% 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
[/cc]
- 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):
[cc lang="latex"]
\newcommand{\title}[1]{\chapter{#1}}
\subimport{chapter1/}{chapter1} % file chapter1/chapter1.tex
[/cc]
- 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.
[cc lang="latex"]
\ifdefined\foo
\message{\string\foo\space is defined}%
\else
\message{no command \string\foo}%
\fi
%
\ifcsname foo\endcsname
\message{\string\foo\space is defined}%
\else
\message{no command \string\foo}%
\fi
[/cc]

