# Including only the parts of a document corresponding to a conditional expression

Suppose I have a LaTeX document, which uses a conditional. Ie.

\ifdefined\submit
[....]
\else
[....]
\fi


So, if we have, say, defined

\def\submit{}


then the first part of the conditional is parsed (or whatever the right term is), otherwise the second part is.

Is there some way within TeX, to postprocess the document to strip out all the conditionals, and only include the parts corresponding to \ifdefined\submit if \submit is defined and the other parts if not? It is not a big deal, but was curious whether such a thing is possible.

-
TeX doesn't do text file processing, at least in a reasonably fast way. It's conceivable to write a driver file that reads the main file line by line and decides to keep or discard it, writing the line to a new file in the first case. It's probably best done with a scripting language such as Perl. –  egreg Sep 21 '11 at 10:34

I think that this is technically possible: TeX could scan each line and either throw it away or write it out to a new file. It could also examine the first token on each line to see if it is \ifsubmit or \ifdon'tsubmit to switch between the two behaviours. The beamer class does something like this for fragile frames.

But actually, as egreg says in the comments, I would do this via a Perl script. In addition, I would do this in a way so that Perl didn't have to bother with TeX. So my TeX file would look something like:

%%% Version: submit
\ifsubmit
...
%%% Version: other
\else
...
%%% Version: all
\fi


The script would be:

#! /usr/bin/perl -w

my $print = 1; my$otype = (@ARGV ? shift @ARGV : "all");

while (<>) {
if ($otype eq "all") { print; next; } if (/^%%% Version: (\w*)/) { my$type = $1; if (($type eq $otype) || ($type eq "all")) {
$print = 1; } else {$print = 0;
}
<>;
} else {
if (\$print) {
print;
}
}
}


If you would prefer a syntax of the form:

\ifsubmit %%% Version: submitted


then take out the <>; line.

-
Hi Andrew, I'm more interested in a TeX solution because 1) I feel it would be more instructive. I already know how languages like Perl work, though I don't know Perl itself. 2) I figure since TeX "knows" TeX syntax, it would be in a better position than another language to parse and process a LaTeX file, and the results would be less fragile and more robust. I might be wrong about this. If TeX really doesn't have any advantage in this context, then it may not be worth the work. –  Faheem Mitha Sep 21 '11 at 16:16
The solution you suggest, annotating the file so the Perl script can see it, strikes me as fragile and too user dependent. I'm sure I would do such annotations incorrectly myself and/or fail to update changes correctly. If I was going this route, I'd probably go for a straight parsing approach, probably using a Python parsing library. –  Faheem Mitha Sep 21 '11 at 16:19
@FaheemMitha: I have written TeX parsers in both Perl and PHP and I can say that they are a complete nightmare. To get TeX to do it itself, it would have to both expand and not-expand everything: "expand" to find out where the \ifsubmits are, "not expand" since it should write the original version to the file. You could do something a bit like this with my internet class. My conclusion - based on some experience - is that TeX would be more liable to slip-up here than an external script that only looked for the key words. –  Loop Space Sep 21 '11 at 18:11
Thanks for the clarification, Andrew. It looks like I was barking up the wrong tree then. –  Faheem Mitha Sep 22 '11 at 3:41

Take a look at the tagging package.

Here's a small example:

\usepackage{tagging}
[...]
\usetag{sunny}
\droptag{rainy}    % (Not needed, default - just for illustration)
[...]
\iftagged{sunny}{%
The sun will shine today
}{%
There will be no sunshine at present
}
\untagged{rainy,sunny}{%
There will be no weather today
}

-

If you want to switch only between two values then simply use \iftrue. Changing it to \iffalse will switch to the \else part.

\iftrue
%\iffalse
.... done for \iftrue
\else
...  done for \iffalse
\fi

-