# How to have “preprocessor” flags in LaTeX [duplicate]

C programmers often use constructs like

#ifdef foo
printf("foo is defined ");
#endif
#ifndef foo
printf("foo is not defined ");
#endif


to select certain code-paths during the compilation process, by passing the compiler flag like -Dfoo or just sticking a #define foo 0 somewhere in the code.

Is this possible in LaTeX too? So I am looking for something like

\ifdef foo{
short summary, tldr
}
\ifndef foo{
long explanation
}


This can be particularly helpful for producing short summary documents from existing TeX files containing long descriptions. I would like to be able to pass something similar to -Dfoo as a compiler flag, or just define the foo somewhere near the top of the Latex file, just as I would do in the C case.

## marked as duplicate by Henri Menke, siracusa, Troy, Mensch, flavDec 11 '18 at 4:58

You can use \ifdefined to check whether a macro is defined. To check whether a macro is not defined, reverse the conditional by prefixing \unless.

\documentclass{article}
%\newcommand\foo{}
\begin{document}

\ifdefined\foo
short summary, tldr
\fi
\unless\ifdefined\foo
long explanation
\fi

\end{document}


In ConTeXt MkIV you can achieve this behaviour using Modes.

\starttext

\startmode[foo]
short summary, tldr
\stopmode
\startnotmode[foo]
long explanation
\stopnotmode

\stoptext


The great thing about ConTeXt modes is that you can switch them on from the command line of the context program.

\$ context --mode=foo test.mkvi