lualatex 中宏与字符串的基本比较?

lualatex 中宏与字符串的基本比较?

仍在努力解决数据库问题。大部分都完成了,但被以下问题难住了:

\documentclass{article}

\begin{document}

\newcommand{\testmacro}{U01}

The macro testmacro equals ``U01''

\directlua{
if \testmacro == ("U01") then tex.print ("testmacro equals U01")
    elseif \testmacro == ("U02") then tex.print ("testmacro not equal")
else tex.print ("Nothing matched")end}

\end{document}

输出为:

宏 testmacro 等于“U01”

没有匹配

我使用计数器正常工作时遇到了类似的事情,所以有点感到困惑。

答案1

\directlua进行扩展,因此你正在做

if U01 == ("U01")

这显然返回 false。您必须引用该字符串:

\documentclass{article}

\begin{document}

\newcommand{\testmacro}{U01}

\directlua{
if "\testmacro" == "U01" then tex.print ("testmacro equals U01")
    elseif "\testmacro" == "U02" then tex.print ("testmacro not equal")
else tex.print ("Nothing matched")end}

\renewcommand{\testmacro}{U02}

\directlua{
if "\testmacro" == "U01" then tex.print ("testmacro equals U01")
    elseif "\testmacro" == "U02" then tex.print ("testmacro not equal")
else tex.print ("Nothing matched")end}

\renewcommand{\testmacro}{U03}

\directlua{
if "\testmacro" == "U01" then tex.print ("testmacro equals U01")
    elseif "\testmacro" == "U02" then tex.print ("testmacro not equal")
else tex.print ("Nothing matched")end}

\end{document}

在此处输入图片描述

相关内容