About what TeX does after it finds commented conditionals with ltxdoc

Your \newif command is inside a \iffalse so it is not seen, and \ifitsme is now either not an "if" at all, or if the \newif has been executed earlier, the \ifitsme behind the \newif counts too and you have one "if" too much:

This works:

\documentclass{article}
\begin{document}

\newif\ifitsme
\iffalse

\ifitsme hello \fi

\fi 
\end{document}

This fails

\documentclass{article}
\begin{document}


\iffalse
\newif\ifitsme
\ifitsme hello \fi

\fi 
\end{document}

This fails too

\documentclass{article}
\begin{document}

\newif\ifitsme

\iffalse
\newif\ifitsme %\newif not seen, but \ifitsme so one fi missing
\ifitsme hello \fi

\fi 
\end{document}

Morale: Better don't use \newif inside \if--\fi.


As you correctly say, when you do pdflatex test.dtx, the file is read with % a comment character. When TeX finds \DocInput, it will basically ignore % at the start of lines and make ^^A a comment character. So your input is essentially

% \iffalse meta-comment    ^^A This /iffalse
\documentclass{ltxdoc}
\def\showfi{\immediate\write0{{/fi\ on line \the\inputlineno}}}%
\title{Doc Test}%
\newif\ifitsme\itsmetrue
\ifitsme\author{Me :)}%
\else   \author{Not me :/}%
\fi\showfi
\begin{document}%
\iffalse meta-comment    ^^A This /iffalse
\documentclass{ltxdoc}
\def\showfi{\immediate\write0{{/fi\ on line \the\inputlineno}}}%
\title{Doc Test}%
\newif\ifitsme\itsmetrue
\ifitsme\author{Me :)}%
\else   \author{Not me :/}%
\fi\showfi
\begin{document}%
  \DocInput{\jobname}%
\end{document}%
\fi\showfi               ^^A should match this /fi
\fi\showfi               ^^A but it's matching this one
\maketitle
\endinput
\end{document}%
% \fi\showfi               ^^A should match this /fi
% \fi\showfi               ^^A but it's matching this one
% \maketitle
% \endinput

Now \iffalse is seen, but \ifitsme is a conditional found twice in skipped text, so two matching \fi are looked for. And this is where things go wrong, because there is no more a matching \fi for \iffalse, unless you add one, like you do.

The \fi in the uncommented line with “should match this /fi” matches \ifitsme after \newif.

Defining conditionals inside conditionals is always open to this kind of problems. Use

\expandafter\newif\csname ifitsme\endcsname

so when the code is inside skipped text there will be no conditional at all.


Here is a technique for more safely skipping stuff. Put it between

\bgroup\catcode2 0 \catcode`\\ 12 ^^Biffalse

and

^^Bfi^^Begroup

What this does is to inhibit \ starting control sequences. So you are sure that whatever you do with \if, \fi, \else, or \if... etc.. in-between this will have nil influenc.

And it assigns to ascii byte 0x02 this meaning of starting control sequences. we can do a safe ^^Biffalse/^^Bfi thing.

I am not using ^^A as doc assigns to it a use as comment character and we want to keep that (especially if the editor highlights appropriately).

I guess with your MWE it would look like this

% \tracingall
% \bgroup\catcode2 0 \catcode`\\ 12 ^^Biffalse
\documentclass{ltxdoc}
\def\showfi{\immediate\write0{{/fi\ on line \the\inputlineno}}}%
\title{Doc Test}%
\newif\ifitsme\itsmetrue
\ifitsme\author{Me :)}%
\else   \author{Not me :/}%
\fi\showfi
\begin{document}%
  \DocInput{\jobname.dtx}%
  \tracingnone
\end{document}%
% \fi\showfi
% ^^Bfi^^Begroup
% \tracingnone
% \maketitle
% \endinput

with the file being saved as test.dtx.

Caveat: although I use doc for my dtx I never use \DocInput (because it led into endless problems with the way I wanted to handle guards, and because I don't want the documentation part (not the one of code implementation, but the user manual) to be "commented-out") so I don't know if that would be your use case.