
我是 LuaLatex 的新手,我正在嘗試編寫簡單的巨集。
但是,當我向 lua 發送包含反斜線(如 )的字串時\R^n
,程式碼無法按預期工作。
例如我的lua代碼是:
function fourier(group, f)
output = '\\mathcal{F}_{'..group..'}'
if f ~= '' then
output = output..'\\left\\{ {'..f..'} \\right\\}'
end
tex.sprint(output)
end
雖然我試圖定義的巨集是:
\DeclareDocumentCommand \F{D<>{}O{}}{%
\directlua{
fourier("\luatexluaescapestring{#1}", "\luatexluaescapestring{#2}")
}
}
然而,當我輸入 時\[ \F<\R^n>[f] \]
,我在 pdf 上看到一些看起來像 的內容\mathrm{F}_{@bgroup\R}^n {f}
。
有人能幫我嗎?
編輯:完整程式碼:
\documentclass{report}
\usepackage{amsmath, amsthm, amssymb, amsfonts}
\usepackage{xparse}
\title{Title}
\author{bkn}
\newcommand{\R}{\mathbb{R}}
\directlua{dofile("functions.lua")}
\DeclareDocumentCommand \F{D<>{}O{}}{%
\directlua{
fourier("\luatexluaescapestring{#1}", "\luatexluaescapestring{#2}")
}
}
\begin{document}
$\F<a>[f]$ % This works
$\F<\mathbb{R}^n>[f]$ % This doesn't
\end{document}
並且functions.lua包含上述函數。
答案1
LuaTeX\edef
在內部做了類似的擴展\directlua
(儘管後者本身是可擴展的)。為了避免這種情況,您可以使用 e-TeX 原語\unexpanded
\DeclareDocumentCommand \F{D<>{}O{}}{%
\directlua{
fourier(
"\luatexluaescapestring{\unexpanded{#1}}",
"\luatexluaescapestring{\unexpanded{#2}}"
)
}
}