# Why is it not possible to embed \if construction in docstrip

I would like to create a DTX files which could be compiled in two different ways depending of a user's choice. One way to get only the user documentation, and another to get user documentation and code documentation.

I do not want to use \StopEventually since in the second case I do not want to have theses documentations separated in two differents parts, but mixed all along the documentation.

In order to that I setup a DTX template different and simpler than those generally used, i.e. the one from TeX Hacks or from Joseph Wright.

Everything works fine in almost all cases but not in one case I isolated and reduced to this chunk of LaTeX code:

% \newif\ifcode
% \codefalse ^^A Change to \codetrue to include code documentation

% \iffalse
%<*documentation>
\documentclass{ltxdoc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</documentation>
%<*latex>
% \fi

% some user command explanations
% \ifcode
% some code explanations
%    \begin{macrocode}
\renewcommand{\maketitle}
{
\framebox{title}
\else
{title}
\fi
}
%    \end{macrocode}
% \fi
% some other text
% \iffalse
%</latex>
% \fi
\endinput


When I try to compile that with pdflatex I get the following error

! Too many }'s.
l.27 }

?


When line 2 is changed from \codefalse to \codetrue everything works fine.

When \ifcadre, \else and \fi lines are removed, everything works fine in both cases (with \codetrue or \codefalse in line 2).

Did I miss something in docstrip documentation ?

-
David's given an answer as to why: have you considered using l3doc for example as an alternative approach? –  Joseph Wright Feb 27 at 17:42
No since I did not know about l3doc, thank's for the link. I am not familiar enough with LaTeX 3 conventions and I need something stable (which l3doc is not, as warned in the l3doc.dtx file itself). I however really like the \EnableImplementation, \DisableImplementation approach, since that was what I wanted to do at first. After a first quick read it seems they also use the comment environment from the verbatim package but in a clever way than I finally did. I definitively need to take time to read about LaTeX 3 through texdoc expl3 before going further. –  Bruno BEAUFILS Feb 28 at 11:57

This is not really related to dtx but rather the usual "don't define conditionals in a conditional block" issue:

% \ifcode
% some code explanations
%    \begin{macrocode}
\renewcommand{\maketitle}
{
\framebox{title}
\else


if \ifcode is false then \begin{macrocode} is not executed so the block is not read verbatim but skipped while matching if else fi tokens.

\newif\ifcadre is not executed so \ifcadre is not an if but \else is an else and things go wrong.

-

With that explanation a workaround is very easy to set up. Using the verbatim package (and its very nicely coded comment environment) it is easy to define a code environment differently when \ifcode is true or false.

When it's true it does nothing (or almost) when it's false it skips every lines inside it.

Here is the code:

% \newif\ifcode
% \codefalse ^^A Change to \codetrue to include code documentation

% \iffalse
%<*documentation>
\documentclass{ltxdoc}
\usepackage{verbatim}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</documentation>
%<*latex>
% \fi

% \ifcode\let\code\begingroup\let\endcode\endgroup
% \else\let\code\comment\let\endcode\endcomment\fi

% some text explaining things

% \begin{code}
%    \begin{macrocode}
\renewcommand{\maketitle}
{
\framebox{title}
\else
{title}
\fi
}
%    \end{macrocode}
% \end{code}

% some other text

% \iffalse
%</latex>
% \fi
\endinput

-
+1 I was thinking I ought to add working code rather than just say what was wrong, but ran out of time, thanks for picking this up. –  David Carlisle Feb 28 at 11:57