3

This question already has an answer here:

I have some lua code that should prepare a tikz matrix of nodes similar to this answer. After having run into many issues with whitespaces, I finally got some output but it is not as expected.

I narrowed it down to this example code that should result in the same matrix being displayed twice:

main.tex

\documentclass[preview]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\directlua{require('main')
print_matrix()}
\begin{tikzpicture}[mycell/.style={draw, minimum size=1cm}, dot/.style={mycell,fill=blue!20}]
\matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth, nodes={mycell}, nodes in empty cells] {0&1&2\\
|[dot]|3&|[dot]|4&|[dot]|5\\
6&7&8\\
|[dot]|9&|[dot]|10&11\\};
\end{tikzpicture}
\end{document}

main.lua

function print_matrix()
        tex.print([[\begin{tikzpicture}[mycell/.style={draw, minimum size=1cm}, dot/.style={mycell,fill=blue!20}]
\matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth, nodes={mycell}, nodes in empty cells] {0&1&2\\
|[dot]|3&|[dot]|4&|[dot]|5\\
6&7&8\\
|[dot]|9&|[dot]|10&11\\};
\end{tikzpicture}]])
end

The main.tex calls main.lua which should print the same code as is followed after the call.

However, when I run this through lualatex I get the following output: Left hand site: lua output; Right hand: expected output (from direct LaTeX code)

I don't understand why the same code is handled differently when printed from lua or when entered directly in the .tex document. In the end my question boils down to:

What do I have to do to make the LaTeX code from lua behave exactly like when entered directly into the .tex document?

marked as duplicate by Community Nov 7 '15 at 19:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

So I ended up with adapting this answer. I just added a custom function for printing to latex in Lua:

function texsprint(text)
        for line in unicode.utf8.gmatch(text, "[^\r\n]+") do
                tex.sprint(line)
        end
end

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