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>확장되었다는 것입니다.두 배: 한 번은 \normalexpanded( \expanded기본)으로, 한 번 더는 \directlua.

첫 번째 방법은 확장이 끝나면 원하는 작업을 수행하는 이 \normalexpanded남기 때문에 작동합니다. \directlua{context.mymacro("\\iftrue")}\\으로 확장되도록 정의되었으므로 거기가 필요하지 않으므로 첫 번째 예를 다음과 같이 단순화할 수 있습니다.\12\12\noexpand

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

두 번째:

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

\noexpand이( 가) 사라지고 \normalexpanded이 남아 있다 \directlua{context.mymacro([[\iftrue]])}가 너무 일찍 \directlua확장되기 때문에 작동하지 않습니다 \iftrue. 두 차례의 작업이 필요합니다 \noexpand.

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

세 번째:

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

세 가지 이유로 작동하지 않습니다. 첫째, ConTeXt \unexpanded에서는 . 여기에 필요합니다 . 둘째, 내부 중괄호는 catcode 12이므로 오류 가 발생합니다 . 세 번째는 위와 동일합니다. 두 차례의 확장이 있으므로 두 가지가 필요합니다 .\protected\unexpanded\normalunexpanded\startluacode\normalunexpandedMissing { inserted\normalunexpanded

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

네 번째 것:

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

위와 같은 이유로 작동하지 않습니다. 중괄호는 catcode 12이므로 필요한 \luaescapestring것을 보지 못하고 오류가 발생합니다. 먼저 올바른 catcode를 설정해야 합니다:{Missing { inserted

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

관련 정보