![使用条件忽略命令不起作用](https://linux22.com/image/347083/%E4%BD%BF%E7%94%A8%E6%9D%A1%E4%BB%B6%E5%BF%BD%E7%95%A5%E5%91%BD%E4%BB%A4%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我正在尝试将文件转换为 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}