3

When i try create new command,which contains formatted string, I get an error:

\directlua]:1: unfinished string near <eof>.

Help me please fixed my error. this is a code of my document and thanks everybody for the help.

\documentclass{article}
\usepackage{luacode}
\pagestyle{empty}
\thispagestyle{empty}
\newcommand\formula[4]{
\luaexec{
tex.print("test")
for a=#1,#2 do
for b=#1,#2 do
tex.print(string.format("\luatexluaescapestring{#3}".."(%s+%s)^2=%s^2+2*%s*%s+%s^2=%s".."\luatexluaescapestring{#4}",a,b,a,a,b,b,(a+b)^2))
end
end
}}
\begin{document}
\formula{-5}{5}{\[}{\]}
\end{document}
2

\luaexec tries to change the catcodes, such that you can use % literally in it's argument, but this does not work inside a macro: When the macro is defined, \luaexec is only saved and not executed, so the catcodes do not change yet. So the % in the lua string starts a TeX comment and the remaining line is discarded long before the macro is ever executed.

Tl;dr: Do not use \luaexec inside a macro.

Instead, you can move all the Lua stuff into a function and only call the function from the macro:

\documentclass{article}
\usepackage{luacode,amsmath}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode*}
  function myfunc(start, finish, prefix, suffix)
    tex.print("test")
    for a=start,finish do
      for b=start,finish do
        tex.print(string.format("%s(%s+%s)^2=%s^2+2*%s*%s+%s^2=%s%s",prefix,a,b,a,a,b,b,(a+b)^2,suffix))
      end
    end
  end
\end{luacode*}
\newcommand\formula[4]{\directlua{myfunc(#1,#2,"\luaescapestring{\unexpanded{#3}}","\luaescapestring{\unexpanded{#4}}")}}
\begin{document}
\formula{-5}{5}{\[}{\]}
\end{document}

Additonally, I would always prefer the token scanning functions instead of using arguments as literal Lua expressions:

\documentclass{article}
\usepackage{luacode,amsmath}
\pagestyle{empty}
\thispagestyle{empty}
\begin{luacode*}
  function myfunc(start, finish, prefix, suffix)
    tex.print("test")
    for a=start,finish do
      for b=start,finish do
        tex.print(string.format("%s(%i+%i)^2=%i^2+2*%i*%i+%i^2=%i%s",prefix,a,b,a,a,b,b,(a+b)^2,suffix))
      end
    end
  end
\end{luacode*}
\newcommand\formula[4]{%
  \directlua{myfunc(token.scan_int(), token.scan_int(),token.scan_string(),token.scan_string())}
    \numexpr#1\relax
    \numexpr#2\relax
    {\unexpanded{#3}}
    {\unexpanded{#4}}}
\begin{document}
\formula{-5}{5}{\[}{\]}
\end{document}

This improves the integration into the TeX ecosystem because it allows to use the same arithmetic expressions as in other TeX commands instead of using the Lua rules. For examples TeX counters can be used asinstead of number literals.

  • Thank you for your help very mutch. Now it works for me,but i want understand two commands. Can you please explain me,why we must use environment luacode with * and what means commands \luaescapestring and \unexpanded? Thanks everybody for explanation,because i very new in lualatex,so i want inprove my skils in lualatex. – Aleksandr Kozlovskiy Mar 3 at 16:27
  • \luaescapestring is just \luatexluaescapestring, in recent LaTeX version you no longer need the luatex prefix. \unexpanded avoids expansion: You want \[ and \] to be copied without change to Lua, so you have to use \unexpanded to stop TeX from replacing the macro with it's definition. – Marcel Krüger Mar 3 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.