将字符串参数从 TeX 传递到 Lua 函数?

将字符串参数从 TeX 传递到 Lua 函数?

“abbr”后面有一个多余的空格,导致“printsorted”无法运行。我犯了什么错误?

梅威瑟:

\documentclass[12pt]{article}
%%%%%%%%%%%%%%%%

\def\addtosorten#1#2{%
    \directlua{%
    tblen=tblen or {}%
    table.insert(tblen, {abbr="\luaescapestring{#1}", desc="\luaescapestring{#2}"})
}
}
\newcommand{\keyen}[2]{#1\addtosorten{#1}{#2}}

%%%%%%%%%%%%%%%%

\def\printsorted{%
    \directlua{%
    tblen=tblen or {}
    table.sort(tblen)
    for i,n in ipairs(tblen) do
        tex.print("\string\\printkw{" .. n.abbr .. "}{" .. n.desc .. "}")
    end
}}

\newcommand\printkw[2]{\textbf{#1}~-- #2.\\}

\newcommand{\placekeywordshere}{%
    \typeout{KW: NOW SHOWING}
    \noindent\rule{\textwidth}{3pt}
            
    \noindent\printsorted
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\keyen{LED}{Light emitting diode}.

\keyen{FET}{Field effect transistor}.

\keyen{SCADA}{Supervisory control and data acquisition}.

\placekeywordshere
\end{document}

答案1

常见问题:未受保护的结束行。不要使用\\来结束段落,除非您在 的范围内\raggedright,例如在 中flushleft

\documentclass[12pt]{article}

\newcommand\addtosorten[2]{%
    \directlua{%
      tblen=tblen or {}%
      table.insert(tblen, {abbr="\luaescapestring{#1}", desc="\luaescapestring{#2}"})
    }% <--- HERE!
}
\newcommand{\keyen}[2]{#1\addtosorten{#1}{#2}}

%%%%%%%%%%%%%%%%

\newcommand\printsorted{%
    \directlua{%
    tblen=tblen or {}
    %table.sort(tblen)
    table.sort(tblen, function (a,b) return (a.abbr < b.abbr) end)
    for i,n in ipairs(tblen) do
        tex.print("\string\\printkw{" .. n.abbr .. "}{" .. n.desc .. "}")
    end
}}
\newcommand\printkw[2]{\textbf{#1}~-- #2.\\}

\newcommand{\placekeywordshere}{%
    \typeout{KW: NOW SHOWING}
    \begin{flushleft}
    \rule{\textwidth}{3pt}\\
    \printsorted
    \end{flushleft}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\keyen{LED}{Light emitting diode}.

\keyen{FET}{Field effect transistor}.

\keyen{SCADA}{Supervisory control and data acquisition}.

\placekeywordshere
\end{document}

在此处输入图片描述

相关内容