# [Tex/LaTex] My first luaLaTeX example

lualuatex

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*}


(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 \\]],