One of the more frustrating features of TeX's macro-expansion based language is that the control code and the code it controls can interact unexpectedly. This means that if you want a conditional block \ifwhatever...\fi
to control some macro \macro
that affects stuff after the block, you need to leapfrog \macro
to finish the conditional first:
\ifwhatever
\expandafter\macro
\fi
<stuff taken as an argument by \macro>
You see, the \expandafter
eats the \fi
before \macro
can. Using this trick, you can write some code that conditionally executes some following code. Say you have a conditional \ifwhatever
, and you want to execute <code>
if it is true, but not if it is false. Then you can write (here I am assuming \makeatletter
is active):
\ifwhatever
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{<code>}
This can be generalized if you want to select among two different pieces of code, which by the way is how the LaTeX-style conditionals \ifwhatever{<true code>}{<false code>}
work:
\ifwhatever
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{<code 1>}
{<code 2>}
I wouldn't advise doing this with more than one conditional, though: if you want to execute <code>
when \ifwhatever
or \ifnot
is true, you have to write:
\ifwhatever
\expandafter\@gobble
\else
\ifnot
\expandafter\expandafter\expandafter\@firstoftwo
\else
\expandafter\expandafter\expandafter\@gobble
\fi
\fi
{<code>}
This profusion of \expandafter
s is to skip \@firstoftwo
or \@gobble
and expand first one token, then another after it, before going back to expand the macro you actually want once there is nothing in its way.
Of course, the technique of using multiple \expandafter
s to skip ahead is itself an idiom. For example, you can expand the first token in a group once:
\expandafter{\macro}
or twice:
\expandafter\expandafter
\expandafter{\macro}
or three times:
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter
\expandafter{\macro}
and you see the pattern. I may as well explain why this works. When the first \expandafter
goes off, it expands in turn the third, then the first in the next row, then the first in the following row, and finally, \macro
. The intervening tokens are not expanded in this "run", but just stuck back in order. The effect is that every other column of \expandafter
s vanishes, \macro
is expanded once, and then TeX starts expanding again with what is now the first \expandater
, which was the second. The configuration has become the one for expanding \macro
twice, and so we end up expanding it three times.
This will allow you to get all (and only) the starred parts of your document I have added some indentation in order to highlight the different groups of version):
\documentclass[10pt,a4paper]{article}
\usepackage{comment}
\includecomment{versionA}
\includecomment{versionB}
\excludecomment{versionC}
\begin{document}
\begin{versionA}
*part1
\begin{versionC}
part2
\end{versionC}
*part3
\end{versionA}
\begin{versionB}
\begin{versionC}
part1
\end{versionC}
*part2
\end{versionB}
\end{document}
This code will allow you to get all the starred parts from the A version (to get the ones from the B version just invert versionA
and versionB
in the include/excludecomment
commands in the preamble):
\documentclass[10pt,a4paper]{article}
\usepackage{comment}
\includecomment{versionA}
\excludecomment{versionB}
\excludecomment{versionC}
\begin{document}
\begin{versionA}
*part1
\begin{versionC}
part2
\end{versionC}
*part3
\end{versionA}
\begin{versionB}
\begin{versionC}
part1
\end{versionC}
*part2
\end{versionB}
\end{document}
Best Answer
As I mentioned here, the simplest way might be conditionals: