使用条件忽略命令不起作用

使用条件忽略命令不起作用

我正在尝试将文件转换为 pdflatex 但出现以下错误:

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

错误的根源是我从这里使用的补丁:Allowframebreaks 导致 beamer miniframes 行为异常

我使用带有的构造\if来加载某些包,具体取决于我使用的文档类。通常,如果某些东西只适用于 beamer,我会将其放在 \iftargetBeamer-\else-\fi-block 中,并在文档开头写上“\targetReporttrue如果我不使用它”。

我现在的问题是,如果条件为假,LaTeX 不会忽略文本。

我已经为我的问题创建了一个最小“工作”示例:

\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}

如果您注释掉第一个\patchcmd,则一切都会正常工作。这表明 存在错误\ifnum(我还没有完全理解)。但我认为这根本不应该发生,因为 LaTeX 应该忽略\iftargetBeamer-block 内的所有内容。

请告诉我如何在使用报告时成功忽略阻止。

答案1

跳过条件的常见问题。如果外部条件为假,则 Tex 将跳过此

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

所以它不会\patchcmd以任何方式扩张,但是它将要看到两个\ifnum并将它们添加到其内部\if匹配中,因此将期望看到两个\fi关闭内部 if。

您可以给它两个\fi类似\@gobble{\fi\fi}但不太模糊且更易于维护的修补程序,将其放入您输入的外部文件中,以便在跳过时看不到它。

答案2

当跳过条件中未使用的分支中的标记时,TeX 仍会跟踪条件,因此当\iftargetBeamer为假时,两者会与您提供的\ifnum仅有的两个标记配对。\fi

只需删除\ifnum,因为补丁不需要它。

\newif\iftargetReport
\newif\iftargetBeamer

\targetReporttrue
%\targetBeamertrue

\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)}
    \expandafter\expandafter\expandafter\endinput
  \fi
\fi

\iftargetBeamer
  \makeatletter
  \usepackage{etoolbox} 
  % replace the subsection number test with a test that always returns true
  \patchcmd{\slideentry}
    {#2>0}
    {2>0}
    {}{\@error{unable to patch}}
  % 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}

或者,使用regexpatch,这样您就可以“隐藏”条件。

\newif\iftargetReport
\newif\iftargetBeamer

\targetReporttrue
%\targetBeamertrue

\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)}
    \expandafter\expandafter\expandafter\endinput
  \fi
\fi

\iftargetBeamer
  \makeatletter
  \usepackage{regexpatch}
  % replace the subsection number test with a test that always returns true
  \regexpatchcmd{\slideentry}
    {\c{ifnum}\cP.2>0}
    {\c{iftrue}}
    {}{\@error{unable to patch}}
  % use frame numbers instead of subsection slide numbers so that frames 
  % broken over slides get separate circles
  \xpatchcmd{\slideentry}
    {\c@subsectionslide}
    {\c@framenumber}
    {}{\@error{unable to patch}}
  \xpatchcmd{\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}

相关内容