\iftrue 在 \let “吞下”以下 \fi

\iftrue 在 \let “吞下”以下 \fi

我已经搜索过类似的问题,但没有发现任何与此相关的问题。

iffalse我有以下 LaTeX 格式部分来定义内联和多行注释,可以通过替换来一次性“注释” iftrue

\providecommand{\comment}[1]{}
\let\commentstart=\iffalse
\let\commentend=\fi
\iffalse
\renewcommand{\comment}[1]{#1}
%\let\commentstart=\iftrue
\fi

但是,我必须取消注释设置commentstart为的行iftrue,否则会产生以下错误:

pdflatex.exe> ! Incomplete \iffalse; all text was ignored after line 12.
pdflatex.exe> <inserted text> 
pdflatex.exe>                 \fi 
pdflatex.exe> <*> document.tex
pdflatex.exe>                 
pdflatex.exe> ! Emergency stop.
pdflatex.exe> <*> document.tex
pdflatex.exe>                 
pdflatex.exe> !  ==> Fatal error occurred, no output PDF file produced!
pdflatex.exe> Transcript written on document.log.

从这个错误中,我假设该行将\fi自己声明为下一个。但是,我认为\let应该抑制它?或者 的\iftrue处理时间早于\let?有什么方法可以建立预期的行为吗?

以下是完整的 Texlipse 控制台输出:

running: C:\texlive\2015\bin\win32\pdflatex.exe -synctex=1 -interaction=nonstopmode --src-specials document.tex 
pdflatex.exe> This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015/W32TeX) (preloaded format=pdflatex)
pdflatex.exe>  restricted \write18 enabled.
pdflatex.exe>  Source specials enabled.
pdflatex.exe> entering extended mode
pdflatex.exe> (./document.tex
pdflatex.exe> LaTeX2e <2016/03/31>
pdflatex.exe> Babel <3.9q> and hyphenation patterns for 81 language(s) loaded.
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/koma-script/scrreprt.cls
pdflatex.exe> Document Class: scrreprt 2015/10/03 v3.19a KOMA-Script document class (report)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/koma-script/scrkbase.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/koma-script/scrbase.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/koma-script/scrlfile.sty
pdflatex.exe> Package scrlfile, 2015/10/03 v3.19a KOMA-Script package (loading files)
pdflatex.exe>                   Copyright (C) Markus Kohm
pdflatex.exe> 
pdflatex.exe> ))) (c:/texlive/2015/texmf-dist/tex/latex/koma-script/tocbasic.sty)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/koma-script/scrsize11pt.clo)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/koma-script/typearea.sty
pdflatex.exe> Package typearea, 2015/10/03 v3.19a KOMA-Script package (type area)
pdflatex.exe>                   Copyright (C) Frank Neukam, 1992-1994
pdflatex.exe>                   Copyright (C) Markus Kohm, 1994-
pdflatex.exe> 
pdflatex.exe> )) (c:/texlive/2015/texmf-dist/tex/latex/base/fontenc.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/base/t1enc.def))
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/base/inputenc.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/base/utf8.def
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/base/t1enc.dfu)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/base/ot1enc.dfu)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/base/omsenc.dfu)))
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/babel/babel.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/babel-german/ngerman.ldf
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/babel-german/ngermanb.ldf
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/babel/babel.def))))
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/graphics/graphicx.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/graphics/graphics.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/graphics/trig.sty)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/latexconfig/graphics.cfg)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/pdftex-def/pdftex.def
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))))
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/latex/geometry/geometry.sty
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/oberdiek/ifvtex.sty)
pdflatex.exe> (c:/texlive/2015/texmf-dist/tex/generic/ifxetex/ifxetex.sty)))
pdflatex.exe> ! Incomplete \iffalse; all text was ignored after line 12.
pdflatex.exe> <inserted text> 
pdflatex.exe>                 \fi 
pdflatex.exe> <*> document.tex
pdflatex.exe>                 
pdflatex.exe> ! Emergency stop.
pdflatex.exe> <*> document.tex
pdflatex.exe>                 
pdflatex.exe> !  ==> Fatal error occurred, no output PDF file produced!
pdflatex.exe> Transcript written on document.log.

答案1

条件语句仅跟踪某些标记(也称为条件语句),这样您就可以在其他标记中放入许多标记(我现在的英语不太流利)。简单的解决方案:\fi以任何方式添加以平衡\if..\fis。

\providecommand*\comment[1]{}
\let\commentstart=\iffalse
\let\commentend=\fi
\iffalse
\renewcommand\comment[1]{#1}
\let\commentstart=\iftrue
\let\commentend=\fi
\fi

答案2

这很简单:当 TeX 跳过条件的“错误分支”中的标记时,它仍会跟踪条件。因此,\iftrue“错误分支”中的 需要匹配\fi,TeX 在您想要匹配初始 的 中找到它\iffalse。因此错误分支尚未完成,TeX 基本上会吞噬所有内容,直到找到\else\fi

最常见的技巧是在宏中隐藏条件。

\newcommand{\comment}[1]{} % not \providecommand
\let\commentstart=\iffalse
\let\commentend=\fi
\newcommand{\setcommentstarttrue}{\let\commentstart\iftrue}
\iffalse
\renewcommand{\comment}[1]{#1}
\setcommentstarttrue
\fi

但您可能想用不同的方式来做到这一点。

\newcommand{\comment}[1]{}
\makeatletter
\let\commentend=\fi
\newcommand{\showcomments}{%
  \let\commentstart\iftrue
  \let\comment\@firstofone
}
\newcommand{\hidecomments}{%
  \let\commentstart\iffalse
  \let\comment\@gobble
}
\makeatother
\hidecomments % can be \showcomments

相关内容