# My first luaLaTeX example

I am totaly inexperienced in LuaLaTeX and I don't know why but it was somehow very difficult to find working LuaLaTeX example so I can start playing around.

I finaly found the example in a document Numerical methods using LuaLaTeX (p.3):

\documentclass{article}
\usepackage{luacode}

\begin{luacode*}
function trigtable ()
for t=0, 45, 3 do
tex.print(string.format(
’%2d$^{\\circ}$ & %1.9f & %1.9f & %1.9f & %1.9f \\\\’, t, x, math.sin(x), math.cos(x), math.tan(x)))
end
end
\end{luacode*}

\begin{document}
\begin{tabular}{rcccc}
\hline
& $x$ & $\sin(x)$ & $\cos(x)$ & $\tan(x)$ \\
\hline
\trigtable
\hline
\end{tabular}
\end{document}


What I found out from this example is, how to implement Lua code in LaTeX document and it is all great, but how come that this example fails to compile with LuaLaTeX if I save it as text.tex and process it using lualatex test.tex?

I get this command line output:

This is LuaTeX, Version 1.0.4 (TeX Live 2017/Arch Linux)
restricted system commands enabled.
(./test.tex
LaTeX2e <2017-04-15>
(using write cache: /home/ziga/.texlive/texmf-var/luatex-cache/generic)(using r
atex-cache/generic)
luaotfload | main : initialization completed in 0.128 seconds
Babel <3.12> and hyphenation patterns for 1 language(s) loaded.
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
texmf-var/luatex-cache/generic/fonts/otl/lmroman10-regular.luc)))
(/usr/share/texmf-dist/tex/lualatex/luacode/luacode.sty
(/usr/share/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/share/texmf-dist/tex/luatex/luatexbase/luatexbase.sty
(/usr/share/texmf-dist/tex/luatex/ctablestack/ctablestack.sty)))[\directlua]:5:
malformed number near '2d'.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

l.12 \end{luacode*}

• You have ’%2d in your code (the ’ there is U+2019 RIGHT SINGLE QUOTATION MARK). If that's what you have in your real code as well, then that is probably the bug. How did it get there? Edit: Yes, changing the two occurrences of ’ to ' each allows lualatex to successfully process the file and produce a trig table as you probably intended. Is that it? – ShreevatsaR Aug 17 '17 at 18:04
• Escaping characters is always a pain and error-prone. Have you looked at lua long strings [[<your raw string>]] ? – marsupilam Aug 17 '17 at 18:12
• And you're right, good lualatex examples are rare online. Here is one of them : Pascal Triangle. – marsupilam Aug 17 '17 at 18:36
• Maybe you can give the question a meaningful title? – Dr. Manuel Kuehner Aug 17 '17 at 21:20
• For the benefit of others who find this thread: LuaLaTeX can be used without knowing Lua code, unless you are using Lua intentionally. The specific question does involve Lua code, so the "totally inexperienced" user is looking at something advanced. New users: Don't let LuaLaTeX frighten you! – user139954 Aug 17 '17 at 23:26

(Answering the question rather than voting to close, because I can find no other occurrences of “malformed number near” on this site, so it would be useful to have this question around for anyone else who is likely to make a similar typo.)

The error message

[\directlua]:5: malformed number near '2d'.

tells you where to look: on line 5 within the luacode block, near the string 2d. And this is what your file contains there (part of line 5 replaced with ...):

        tex.print(string.format(
’%2d$^{\\circ}$ ...


note the character before the %2d: it's ’ which is U+2019 RIGHT SINGLE QUOTATION MARK, and the Lua interpreter doesn't know what to do with it. Actually it does: it thinks that ’ is the name of a variable, then looks at the % and decides you're trying to take the remainder, but on the right side of the % operator you have 2d which is not a number. (Hence the error message about a malformed number.) If you had ’ = 23 and then string.format(’%4), the latter would correctly give you 3. (In this case even though you haven't defined ’ to be a variable, the syntax-analysis part of Lua cannot give you an error immediately, because you could define a variable with that name later.)

How did ’ get in there? Possibly a bug in your editor; make sure you fix it or it will happen again. Just change ’ to ' in both places, and your code works; congratulations on getting your first LuaLaTeX file working correctly!

Aside: I have found it better to put all Lua code in a separate .lua file and in your .tex file have only \directlua{dofile('myluascript.lua')} (or whatever). This way you get better syntax highlighting for your lua code, when editing your file or when showing it on a site like this.

Your code, with the lua code moved out and with @marsupilam's advice of using [[ ... ]] strings:

.tex file:

\documentclass{article}
\directlua{dofile('lualatex-bug.lua')}
\newcommand{\trigtable}{\directlua{trigtable()}}

\begin{document}
\begin{tabular}{rcccc}
\hline
& $x$ & $\sin(x)$ & $\cos(x)$ & $\tan(x)$ \\
\hline
\trigtable
\hline
\end{tabular}
\end{document}


lualatex-bug.lua:

function trigtable ()
for t=0, 45, 3 do
tex.print(string.format([[%2d$^{\circ}$ & %1.9f & %1.9f & %1.9f & %1.9f \\]],
t, x, math.sin(x), math.cos(x), math.tan(x)))
end
end

• +1 for the good end advice. Also better completion, etc. and the possibilty to test outside of latex. I think the bad ' got there from copy-pasting from a poorly encoded website. – marsupilam Aug 17 '17 at 18:29
• This is really helpful! Thank you very much! It looks like copy-pasting the code from PDF's isn't the best way to program. You never know what might sneak in... – 71GA Aug 17 '17 at 21:10

You may be interested in lua long strings to discard all special characters in the string, and have them raw.

This is especially relevant to luaLaTeX, as the escape character in lua is the backslash \.

If you want to pass a command to TeX, the \ has to be doubled, and it may thus be less annoying to type [[\mycmd]] than "\\mycmd", or [[\\]] than "\\\\"

## The code

\RequirePackage{luatex85}
\documentclass[12pt,border=2pt]{standalone}
\usepackage{luacode}
\begin{luacode*}
function trigtable ()
for t=0, 45, 3 do
local sf =[[%2d$^{\circ}$ & %1.9f & %1.9f & %1.9f & %1.9f \\]]
tex.print(string.format(sf, t, x, math.sin(x), math.cos(x), math.tan(x)))
end
end
\end{luacode*}

& $x$ & $\sin(x)$ & $\cos(x)$ & $\tan(x)$ \\

• Good idea to use [[ ... ]] raw strings. – ShreevatsaR Aug 17 '17 at 18:34