Lua での TeX 条件文

Lua での TeX 条件文

なぜ使えるのか

\startluacode
context.mymacro("\noexpand\\iftrue")
\stopluacode

だがしかし

\startluacode
context.mymacro([[\noexpand\iftrue]])
\stopluacode

か否か

\startluacode
context.mymacro([[\unexpanded{\iftrue}]])
\stopluacode

か否か

\startluacode
context.mymacro("\luaescapestring{\normalunexpanded{\iftrue}}")
\stopluacode

\iftrue拡張可能なプリミティブであると信じています。

編集

\def\mymacro#1{%
    \let\mycond#1%
    \show\mycond}

答え1

\startluacode<code>\stopluacode大まかに翻訳すると次のようになります:

\begingroup
  <catcode settings> (basically verbatim, but \ is catcode 0)
  <shorthands> (\\ is defined to be the two backslash characters, as
    well as \|, \-, \(, \), \{, \}, etc.)
\normalexpanded {\endgroup \noexpand \directlua {<code>}}

ここで問題なのは、が<code>拡大されていることです2回: 一度は\normalexpanded(\expandedプリミティブ) によって、もう一度は によって\directlua

最初の例は、 の展開が\normalexpanded終わると が残り、\directlua{context.mymacro("\\iftrue")}これが目的の動作をするため機能します。\\は に展開するように定義されているため、 は必要なく、最初の例を次のように簡略化できることに注意してください。\12\12\noexpand

\startluacode
context.mymacro("\\iftrue")
\stopluacode

2番目:

\startluacode
context.mymacro([[\noexpand\iftrue]])
\stopluacode

は機能しません。なぜなら、 は\noexpandとともに消えて\normalexpandedが残り\directlua{context.mymacro([[\iftrue]])}、 が早期に\directlua展開されるからです\iftrue。 を 2 回実行する必要があります\noexpand

\startluacode
context.mymacro([[\noexpand\noexpand\noexpand\iftrue]])
\stopluacode

3番目:

\startluacode
context.mymacro([[\unexpanded{\iftrue}]])
\stopluacode

は 3 つの理由で動作しません。まず、ConTeXt では はプリミティブ\unexpandedであり\protected、 ではありません\unexpanded\normalunexpandedここで必要です。2 番目に、\startluacode中括弧内は catcode 12 なので、エラー\normalunexpandedが発生しますMissing { inserted。3 番目は上記と同じです。展開が 2 回行われるため、 が 2 つ必要です\normalunexpanded

\everyluacode\expandafter{\the\everyluacode
  \catcode`\{=1 \catcode`\}=2\relax} % catcode setting to have braces be braces
\startluacode
context.mymacro([[\normalunexpanded{\normalunexpanded{\iftrue}}]])
\stopluacode

4番目:

\startluacode
context.mymacro("\luaescapestring{\normalunexpanded{\iftrue}}")
\stopluacode

上記と同じ理由で動作しません。中括弧は catcode 12 なので、必要なもの\luaescapestringが見つからず{Missing { insertedエラーが発生します。まず正しい catcode を設定する必要があります。

\everyluacode\expandafter{\the\everyluacode
  \catcode`\{=1 \catcode`\}=2\relax} % catcode setting to have braces be braces
\startluacode
context.mymacro("\luaescapestring{\normalunexpanded{\iftrue}}")
\stopluacode

関連情報