# Create a Table with Luatex

I want to make make a template table with parameters. I found different types of solutions but then I try to do some changes, it doesn't work anymore.

I tried two different ways: First way

\documentclass{beamer}
\usepackage{luacode}
\usepackage{xparse}

\NewDocumentCommand{\awesomelist}{%
m
}{%

\directlua{
tex.print([[\begin{tabular}{|c|c|}]])
tex.print([[\hline]])
for i,k in pairs(table.pack("#1"))
do
if i == 1 then
tex.print(String.format([[\%s & \%s \\]],k))
else
tex.print(String.format([[ & \%s \\]],k))
end
end
tex.print([[\hline\end{tabular}]])
}
}

\begin{document}

\awesomelist{Scope,Test A,Test B,Test C,Test D}

hello world

\end{document}


The second idea was to do it that way:

 \documentclass{article}
\usepackage{longtable}
\usepackage{luacode}
\usepackage{luatextra}

\begin{luacode*}
function semisplit(input)
local lst = string.split(input,";")
for i, x in ipairs(lst) do
if i == 0 then
tex.print(string.format([[%s]],x))
else
tex.print(string.format([[ &\multicolumn{4}{p{11cm}}{%s}\\]],x))
end
end
end
\end{luacode*}

\newcommand{\builder}[1]{\directlua{semisplit("#1")}}

\begin{longtable}{p{2.5cm}|p{2.5cm} p{2.5cm} p{3.5cm} p{2.5cm}}
\hline
First Column & \multicolumn{4}{l}{some text inside} \\
\hline
Scope&\multicolumn{4}{p{11cm}}{\builder{Test a}}\\[2pt]
\hline
\end{longtable}


I hope you guys have an idea. It's my first time working with lua, so don't judge me.

• It's quite unclear to me, I'm afraid, what you are trying to achieve here. What is the use case? The intended input? The intended output? Your two functions seem to do different things with different input. – Paul Stanley Apr 2 '19 at 10:57
• i'm trying to create a table where i get the title in the first column und the description in the second column. The description should be split up into multiple rows. The first example was just some testing of me. The second one is the code out of my actual project. The LateX Code is not my rpoblem but to create it in combination with luatex is difficult for me – Narhog Apr 2 '19 at 11:24
• Long time ago I had the idea to use lua to create LaTeX-tabulars and include calculations. Worked, but was way to complicated to use. I found out that my editor Emacs came with orgmode and orgmode had a -- in comparison -- much easier spreadsheet mode. Since then I do tabulars via orgmode, spreadsheet and so called radio-tables. – Keks Dose Apr 2 '19 at 12:54
• why do you have a 5 column table and then use multicolumn{4} each time, rather than a 2 column table? – David Carlisle May 22 '19 at 13:10

I'm still not 100 percent sure what you are trying to do; I don't find your intention completely clear from the code.

So far as using Lua is concerned, I think you will, on balance, be better off setting up a command that does each line, rather than trying to do something that does the whole table. That leaves you setting up the table, but using your command to format each individual line. You obviously could extend this to read in a lot of lines together, but I can't see the advantage of that unless you intended to read them in from a file.

I set it up so the syntax is TITLE=line|line|line You can fiddle with the string.find and string.match lines to change that if you prefer something different. I put in the \cline largely so that one could see where each box ended; it would be easier without really.

But my hunch is that you are just going about this the wrong way. For what I think you want, the idiomatically LaTeX-ish way to do it is, I think, a description list. So I demonstrate that as well.

\documentclass{article}
\usepackage{luacode}
\usepackage{longtable}
\usepackage{enumitem}
\begin{luacode*}
function doline(line)
local splitpoint = string.find(line, "=")
local title = line:sub(1, splitpoint-1)
local description = line:sub(splitpoint + 1)
tex.print(title)
for token in string.gmatch(description, "[^|]+") do
tex.print("&" .. token .. "\\\\")
end
tex.print("\\cline{1-2}")
end
\end{luacode*}
\newcommand{\tabularline}[1]{%
\directlua{
doline("#1")
}}
\begin{document}

\begin{longtable}{|p{3cm} | p{7cm} |}
\cline{1-2}
\tabularline{Title=Description| More Description| Third Line}
\tabularline{Title 2=And plenty| More | Where that | Came From}
\end{longtable}

% And a more latex-y solution IMO
\begin{description}[leftmargin=3.2cm, labelwidth=3cm, labelsep=0.2cm]
\item[Title 1] And here.

We have.

Some text.

\item[Title 2 Like This] And here is a longer description.

Which also has a paragraph in it.
\end{description}

\end{document}


## UPDATE

In comments, the OP asked whether it was essential to use the | character, or whether the split could take place on newlines. I said it could, but the OP then found that didn't work. That's right.

The problem is not with the lua code, but with the fact that because the argument to \tabularline is "processed" by TeX before it gets passed into Lua, newlines have been replaced by spaces. The/a solution is to be found in the answer here: we need to detokenize the argument first.

That, however, ends up with the formatting function receiving a string in which the newlines are replaced by "\par". Because of the way I'm splitting, that is not such an easy solve. Perhaps the best way would be to loop over the string, using string.find to locate \par and splitting there. But it's shorter, though less tidy, simply to substitute \par for \n before doing the split. So we end up with modified code as follows:

\begin{luacode*}
function doline(line)
local splitpoint = string.find(line, "=")
local title = line:sub(1, splitpoint-1)
local description = string.gsub(line:sub(splitpoint + 1), "\\par", "\n")
tex.print(title)
for token in string.gmatch(description, "[^\n]+") do
tex.print("&" .. token .. "\\\\")
end
tex.print("\\cline{1-2}")
end
\end{luacode*}


And

\newcommand{\tabularline}[1]{%
\directlua{
doline("\luatexluaescapestring{\detokenize{#1}}")
}}


For my part, I prefer the pipe (or, still better, the description list) but that is really just a matter of taste.

• I think that could help me, thanks mate. I will try to implement that into my code. – Narhog Apr 2 '19 at 13:53
• Hey, just an other question: Does your Code also work without the pipe symbol? I mean, is it possible to create a new Row every time you begin a new line in the code inside of the makro? – Narhog Apr 3 '19 at 6:47
• You can use any character you like to mark when the split takes place: I just chose the pipe because it was a character that you wouldn't be likely to need for other purposes. I don't see any reason why you couldn't use a newline. Just modify the pattern to string.gmatch, which is doing the splitting. – Paul Stanley Apr 3 '19 at 7:06
• You misunderstand me, I don't want to replace it. Like \tabularline{Title=Descriptionnew line in code More Descriptionnew line in code Third Line}. So that you dont have to type any character and just do a new line in your .tex Code – Narhog Apr 3 '19 at 7:14
• i tried to change the regex code with [^\n]+ but it didn't work – Narhog Apr 3 '19 at 7:55