Como posso ter uma \edef
macro 'ed (expandida) que contenha um loop lua tex.sprint
? Recebo um erro quando tex.sprint
há outra macro como \blindtext[<n>]
ela. Não recebo erro se imprimir algumas strings não macro, como Hello world!
usar tex.sprint
no loop lua. O erro também não acontece caso eu não expanda (não use \edef
) a definição da macro e use-a \newcommand
. Por que?
Abaixo está o código que usa primeiro \newcommand
e depois \edef
. Primeiro execute o código como está, na segunda execute descomente a \edef
versão para ver o erro:
! 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}
Responder1
Você pode expandir um loop Lua em edef, mas não \blindtext
neste caso, embora você não precise expandir a macro, como mostrado na saída do terminal, isso resulta na definição
\myblindtextexpanded
como
macro:-> \blindtext [1] \par \blindtext [1] \par \blindtext [1] \par \blindtext
[1] \par
então o loop foi desenrolado para chamadas repetidas de \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}