我正在尝试定义自己的开始/停止块,用于带有某些格式的条件编译。我尝试使用模式并添加边距规则(如果要显示该块)。
但是,只有启用该模式时,它才能正确编译。如果禁用该模式,则编译不起作用。这是我的 MWE:
\definemode[teacherversion][yes]
\definestartstop[teacheronly]
[before={\startmode[teacherversion]\startmarginrule},
after={\stopmarginrule\stopmode}]
\disablemode[teacherversion]
\starttext
Text before
\startteacheronly
\input knuth
\stopteacheronly
Text after
\stoptext
以及 context 的输出:
adrien@arsenalGear:~/dev/testConteXt$ context blocktest.tex
mtx-context | run 1: luatex --fmt="/opt/context/tex/texmf-cache/luatex-cache/context/5fe67e0bfe781ce0dde776fb1556f32e/formats/luatex/cont-en" --jobname="blocktest" --lua="/opt/context/tex/texmf-cache/luatex-cache/context/5fe67e0bfe781ce0dde776fb1556f32e/formats/luatex/cont-en.lui" --no-parse-first-line --c:currentrun=1 --c:fulljobname="./blocktest.tex" --c:input="./blocktest.tex" --c:kindofrun=1 --c:maxnofruns=9 "cont-yes.mkiv"
This is LuaTeX, Version 0.95.0 (TeX Live 2016)
system commands enabled.
open source > 1 > 1 > /opt/context/tex/texmf-context/tex/context/base/mkiv/cont-yes.mkiv
ConTeXt ver: 2016.07.08 17:19 MKIV beta fmt: 2016.7.9 int: english/english
system > 'cont-new.mkiv' loaded
open source > 2 > 2 > /opt/context/tex/texmf-context/tex/context/base/mkiv/cont-new.mkiv
close source > 2 > 2 > /opt/context/tex/texmf-context/tex/context/base/mkiv/cont-new.mkiv
system > files > jobname 'blocktest', input './blocktest', result 'blocktest'
fonts > latin modern fonts are not preloaded
languages > language 'en' is active
open source > 2 > 3 > /home/adrien/dev/testConteXt/blocktest.tex
fonts > preloading latin modern fonts (second stage)
fonts > 'fallback modern-designsize rm 12pt' is loaded
close source > 2 > 3 > /home/adrien/dev/testConteXt/blocktest.tex
close source > 1 > 3 > /opt/context/tex/texmf-context/tex/context/base/mkiv/cont-yes.mkiv
*
星号是提示。请问我该如何实现条件编译?
答案1
当您禁用该teacherversion
模式时\startmode
,命令会尝试吞噬所有内容,直到\stopmode
将它们嵌入到其他命令中时才起作用。您必须首先创建teacheronly
环境以确保命令存在,然后将启用边距规则的设置放在单独的mode
块中。
\definemode[teacherversion][yes]
%\disablemode[teacherversion]
\definestartstop[teacheronly]
\startmode[teacherversion]
\setupstartstop
[teacheronly]
[before=\startmarginrule,
after=\stopmarginrule]
\stopmode
\starttext
Text before
\startteacheronly
\input knuth
\stopteacheronly
Text after
\stoptext
当您想要吞噬teacheronly
环境的完整内容时,您可以创建不同的定义。
\definemode[teacherversion][yes]
%\disablemode[teacherversion]
\startmode[teacherversion]
\definestartstop
[teacheronly]
[before=\startmarginrule,
after=\stopmarginrule]
\stopmode
\startnotmode[teacherversion]
\definebuffer[teacheronly]
\stopnotmode
\starttext
Text before
\startteacheronly
\input knuth
\stopteacheronly
Text after
\stoptext
对于上述两种方法,您都可以在设置内容之前启用/禁用模式,为了摆脱这个限制,可以使用以下解决方案,即在刷新存储的内容之前检查模式。
\definemode[teacherversion][yes]
\definebuffer[teacheronly]
\def\stopteacheronly
{\doifmode{teacherversion}
{\startmarginrule
\getdefinedbuffer[teacheronly]
\stopmarginrule}}
%\disablemode[teacherversion]
\starttext
Text before
\startteacheronly
\input knuth
\stopteacheronly
Text after
\stoptext