答案1
您必须在扫描参数\directlua
之前进行扩展。这可以使用原语来完成:\foreach
\expanded
\documentclass[]{article}
\usepackage[]{tikz}
\begin{document}
\expanded{\unexpanded{\foreach \a / \n in }{\directlua{tex.sprint("a/b")}}}
{\a\ and \n}
\end{document}
答案2
当我想覆盖文件内容时,我会使用这个foreach
(不确定,但可能是从使用 \foreach 从文件中读取要迭代的数据):
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}
\begin{filecontents}[overwrite]{mylist.txt}
a/b, 1/2,
c/d
\end{filecontents}
\usepackage{catchfile}
\newcommand\loaddata[1]{\CatchFileDef\loadeddata{#1}{\endlinechar=-1}}
\begin{document}
\loaddata{mylist.txt}
\foreach \one/\two in \loadeddata {\one--\two\par}
\end{document}
答案3
您可以使用下面提供的补丁来提供expand list
在使用前完全展开列表的选项。
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach [expand list=true] \a / \n in {\directlua{tex.sprint("a/b")}} {
\a
}
\end{document}
diff --git a/tex/generic/pgf/utilities/pgffor.code.tex b/tex/generic/pgf/utilities/pgffor.code.tex
index cdb77b31..65b62b8c 100644
--- a/tex/generic/pgf/utilities/pgffor.code.tex
+++ b/tex/generic/pgf/utilities/pgffor.code.tex
@@ -79,10 +79,13 @@
\fi%
}
+\def\pgffor@expand@list@true{\let\pgffor@expand@list\edef}
+\def\pgffor@expand@list@false{\let\pgffor@expand@list\def}
\def\pgffor@macro@list#1{%
\expandafter\pgffor@normal@list\expandafter{#1}}
\def\pgffor@normal@list#1{%
- \def\pgffor@values{#1, \pgffor@stop,}%
+ \pgffor@expand@list\pgffor@values{#1}%
+ \expandafter\def\expandafter\pgffor@values\expandafter{\pgffor@values, \pgffor@stop,}%
\ifx\pgffor@values\pgffor@emptyvalues
\def\pgffor@values{\pgffor@stop,}%
\fi%
@@ -619,6 +622,9 @@
count/.code=\pgffor@count@parse#1\pgffor@stop,
parse/.is if=pgffor@assign@parse,
parse/.default=false,
+ expand list/.is if=pgffor@expand@list@,
+ expand list/.default=true,
+ expand list=false,
}
\def\pgffor@assign@parse#1{%
您无需修补 PGF 安装(下次更新无论如何都会覆盖该安装),也可以直接在文档中包含必要的定义。
\documentclass{article}
\usepackage{pgffor}
\makeatletter
\def\pgffor@expand@list@true{\let\pgffor@expand@list\edef}
\def\pgffor@expand@list@false{\let\pgffor@expand@list\def}
\def\pgffor@normal@list#1{%
\pgffor@expand@list\pgffor@values{#1}%
\expandafter\def\expandafter\pgffor@values\expandafter{\pgffor@values, \pgffor@stop,}%
\ifx\pgffor@values\pgffor@emptyvalues
\def\pgffor@values{\pgffor@stop,}%
\fi%
\let\pgffor@body=\pgfutil@empty%
\global\pgffor@continuetrue%
\pgffor@collectbody}
\makeatother
\pgfqkeys{/pgf/foreach}{
expand list/.is if=pgffor@expand@list@,
expand list/.default=true,
expand list=false,
}
\begin{document}
\foreach [expand list=true] \a / \n in {\directlua{tex.sprint("a/b")}} {
\a
}
\end{document}