\documentclass[a4paper]{article}

\title{Lua\LaTeX\ to write tables}
\author{Richard}

\usepackage{luacode}
\parskip4pt
\parindent0pt
\newcommand\tstrut{\rule{0pt}{2.6ex}} % top strut of height 2.6ex to add space after \hline
\begin{document}

\maketitle

Here is a trig table.

\begin{luacode}
    x = 0.0
    tex.sprint("\\begin{tabular}{lll}")
    tex.sprint(" $x$ & $\\sin(x)$ & $\\cos(x)$ \\\\ \\hline\\tstrut")
    for n = 0,10 do
      tex.sprint(x .. " & " .. math.sin(x) .. " & " .. math.cos(x) .. " \\\\")
      x = x + 0.1
    end
    tex.sprint("\\end{tabular}")
\end{luacode}

The code that makes this is,

\begin{verbatim}
\begin{luacode}
    x = 0.0
    tex.sprint("\\begin{tabular}{lll}")
    tex.sprint(" $x$ & $\\sin(x)$ & $\\cos(x)$ \\\\ \\hline\\tstrut")
    for n = 0,10 do
      tex.sprint(x .. " & " .. math.sin(x) .. " & " .. math.cos(x) .. " \\\\")
      x = x + 0.1
    end
    tex.sprint("\\end{tabular}")
\end{luacode}
\end{verbatim}

Both \LaTeX\ and Lua modify text somewhat. The point of the `luacode' environment is
to minimise the modifications as much as reasonable.  Specifically,
\verb|\\| (which normally makes a new line) stays as \verb|\\| when it is
in the luacode environment.  Note also that Lua
uses \verb|\| as its ``escape'' character so, in Lua,
\verb|print("\S")| will print the character \verb|S| whatever it is,
including strange characters like \verb|"| or \verb|\|.  That is why the back slashes have to be doubled.

The \verb|..| is Lua's ``concatenation'' operator joining two strings together. Using this, 
Lua prints the \LaTeX\ table and \LaTeX\ typesets it beautifully after Lua
has exited. Note \verb|\tstrut| is a special command that is defined in this
file that helps get the vertical spacing right.

Try to modify this as you desire.

\end{document}
