edef の Lua ループではエラーが発生しますが、newcommand では発生しません。Lua ループを使用してマクロを完全に展開するにはどうすればよいですか?

edef の Lua ループではエラーが発生しますが、newcommand では発生しません。Lua ループを使用してマクロを完全に展開するにはどうすればよいですか?

内部に\edeflua ループを含む 'ed (展開) マクロを作成するにはどうすればよいでしょうか。 内部にのような別のマクロが含まれているとエラーが発生します。 lua ループ内でを使用するなど、マクロ以外の文字列を印刷するとエラーは発生しません。マクロ定義を展開せず ( を使用せず)、代わりに を使用する場合にもエラーは発生しません。 なぜでしょうか。tex.sprinttex.sprint\blindtext[<n>]Hello world!tex.sprint\edef\newcommand

\newcommand以下は、最初に を使用し、次に を使用するコードです\edef。最初はコードをそのまま実行し、2 回目の実行で\edefバージョンのコメントを解除してエラーを確認します。

! Use of \\blindtext doesn't match its definition.
\kernel@ifnextchar ...rved@d =#1\def \reserved@a {
                                                  #2}\def \reserved@b {#3}\f...

l.26    \directlua{dofile("blindtextloop.lua")}
                                           %
? 
% lualatex edefloop.tex
\documentclass[notitlepage,letterpaper]{article}
\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

% Note: This will write file blindtextloop.lua in you current directory
\begin{filecontents*}{blindtextloop.lua}
    for i=0,3 do
        tex.sprint(" \\blindtext[1] \\par")
    end
\end{filecontents*}

% Unexpanded blindtext
\newcommand{\myblindtext}{%
    \directlua{dofile("blindtextloop.lua")}%
}%

Expanding next:

\myblindtext

%% Uncomment following lines in second run to see the error:
% %Expanded blindtext
% \edef\myblindtextexpanded{%
%   \directlua{dofile("blindtextloop.lua")}%
% }%

% Already expanded:

% \myblindtextexpanded


\end{document}

スクリーンショット: コンパイルされた出力のスクリーンショット

答え1

edefでLuaループを展開することはできますが、\blindtextこの場合はマクロを展開する必要はありません。端末出力に示されているように、定義は \myblindtextexpanded 次のようになります。

macro:-> \blindtext [1] \par \blindtext [1] \par \blindtext [1] \par \blindtext
 [1] \par 

そのため、ループは の繰り返し呼び出しに展開されました\blindtext

% lualatex edefloop.tex
\documentclass[notitlepage,letterpaper]{article}
\usepackage[english]{babel}
\usepackage{blindtext}

\begin{document}

% Note: This will write file blindtextloop.lua in you current directory
\begin{filecontents*}{blindtextloop.lua}
    for i=0,3 do
        tex.sprint(" \\noexpand\\blindtext[1] \\par")
    end
\end{filecontents*}

% Unexpanded blindtext
\newcommand{\myblindtext}{%
    \directlua{dofile("blindtextloop.lua")}%
}%

Expanding next:

\myblindtext

%% Uncomment following lines in second run to see the error:
 %Expanded blindtext
 \edef\myblindtextexpanded{%
   \directlua{dofile("blindtextloop.lua")}%
 }%

% Already expanded:

\typeout{\meaning\myblindtextexpanded}

\myblindtextexpanded


\end{document}

関連情報