# Printed macros from lua behave differently when entered directly in LaTeX code [duplicate]

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:

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

function texsprint(text)