[Tex/LaTex] Using conditionals to ignore command does not work

beamerconditionalspatchingreport

I am trying to pdflatex a file but I get the following error:

! Incomplete \iffalse; all text was ignored after line [...].

The source of the error is a patch I used from here: Allowframebreaks causes beamer miniframes to misbehave

I use a construction with \if to load certain packages, depending on which documentclass I use. Normally if something only works with beamer, I put it inside a \iftargetBeamer-\else-\fi-block and at the beginning of the document I just write \targetReporttrue if I don’t wont to use it.

My problem now is that LaTeX does not ignore the text if the condition is false.

I have created a minimum “working” example of my problem:

\newif\iftargetReport
\newif\iftargetBeamer

\targetReporttrue

\iftargetReport
\documentclass[a4paper, 12pt, ngerman]{scrreprt}
\else\iftargetBeamer
\documentclass[ignorenonframetext, ngerman]{beamer}
\else
\typeout{no valid class defined, enter Ctrl-D (Linux) or Ctrl-Z (Windows)}
\endinput
\fi\fi

\iftargetBeamer
\makeatletter
\usepackage{etoolbox} % <- You can savely uncomment this if using report. This implies LaTeX does ignore this block.
% HERE is the problem!! Comment out next line to "fix" the issue.
\patchcmd{\slideentry}{\ifnum#2>0}{\ifnum2>0}{}{\@error{unable to patch}}% replace the subsection number test with a test that always returns true
% use frame numbers instead of subsection slide numbers so that frames broken over slides get separate circles
\patchcmd{\slideentry}{\c@subsectionslide}{\c@framenumber}{}{\@error{unable to patch}}
\patchcmd{\beamer@writeslidentry}{\c@subsectionslide}{\c@framenumber}{}{\@error{unable to patch}}
\makeatother
\else
\usepackage{beamerarticle}
\fi

\begin{document}
    \begin{frame}
        Some text.
    \end{frame}
\end{document}

If you comment out the first \patchcmd, everything just works fine. That suggest there is an error with the \ifnum (which I do not understand fully yet). But I think this shouldn’t even occur because LaTeX should ignore everything inside the \iftargetBeamer-block.

Please show me how I can successfully ignore the block when using the report.

Best Answer

Usual problem with skipping over conditionals. If the outer condition is false then Tex will be skipping over this

\patchcmd{\slideentry}{\ifnum#2>0}{\ifnum2>0}{

so it will not expand \patchcmd in any way but it will see two \ifnum and add them to its internal \if matching and so will expect to see two \fi closing the inner ifs.

You could give it two \fi with \@gobble{\fi\fi} or similar but less obscure and more maintainable would be to put the patching into an external file that you input so it isn't seen when skipping.