# How to use luacode* to eliminate need to escape “\”?

I am struggling trying to understand how to actually use luacode* environment, so that I do not have to escape all the \ characters in the strings that Lua builds before sending to Tex.

The whole point of using luacode* vs. luacode is that one does not have to escape the \ inside lua strings before sending them to Tex. According to this table from https://www.ctan.org/pkg/luacode?lang=en

But when making a string and using tex.print, Latex gives error. So I am doing something wrong, or I do not understand something basic about this.

Here is a MWE

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
i=6
j=8
tex.print("\begin{tabular}{|l|l|}\hline")
tex.print(i.."&"..j)
tex.print("\\\hline")
tex.print("\end{tabular}")
\end{luacode*}
\end{document}


lualatex foo.tex gives

(./foo1.aux)
! LuaTeX error [\directlua]:3: invalid escape sequence near '\h'.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

l.13 \end{luacode*}


Only when escaping all the \ does it work:

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
i=6
j=8
tex.print("\\begin{tabular}{|l|l|}\\hline")
tex.print(i.."&"..j)
tex.print("\\\\ \\hline")
tex.print("\\end{tabular}")
\end{luacode*}
\end{document}


I have looked at which-lua-environment-should-i-use-with-luatex-lualatex and printing-backslash-from-a-luacode-environment and do not see the solution for this. i.e. how to write the above code as is without having escape \ or do any other special processing. Since this is supposed to be the whole point of using luacode*. I do not want to write the code in an external file. I need to process it as shown above, but avoid having to escape \.

Using TL 2015.

*File List*
article.cls    2014/09/29 v1.4h Standard LaTeX document class
size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
luacode.sty    2012/01/23 v1.2a lua-in-tex helpers (mpg)
ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO)
luatexbase.sty    2013/05/11 v0.6 Resource management for the LuaTeX macro progr
ammer
luatex.sty    2010/03/09 v0.4 LuaTeX basic definition package (HO)
infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO)
etex.sty    2015/03/02 v2.1 eTeX basic definition package (PEB,DPC)
luatexbase-compat.sty    2011/05/24 v0.4 Compatibility tools for LuaTeX
luatexbase-modutils.sty    2013/05/11 v0.6 Module utilities for LuaTeX
luatexbase-regs.sty    2011/05/24 v0.4 Registers allocation for LuaTeX
luatexbase-attr.sty    2013/05/11 v0.6 Attributes allocation for LuaTeX
luatexbase-cctb.sty    2013/05/11 v0.6 Catcodetable allocation for LuaTeX
luatexbase-mcb.sty    2013/05/11 v0.6 Callback management for LuaTeX
***********


(too few examples on the net showing how to use lua from inside Latex)

• luacode allows you to control what you sent from tex to lua. The way back to tex is another story. There you will have to consider the lua rules regarding backslashes. See lua.org/pil/2.4.html – Ulrike Fischer Jul 4 '15 at 21:25
• @UlrikeFischer this is all so confusing. I do not know about Tex->Lua part, since I am just looking at Lua->Tex now. But if you are saying the luacode* is meant to apply for the "Tex->Lua" and not to Lua->Tex then this makes using strings in Lua very hard if one have to escape all the "\" each time. I can do all the above much easier in Python, without having to escape anything, using Pythons r""" multi-line raw string support. So I am trying to understand why would I need to use Lua then in this case. – Nasser Jul 4 '15 at 21:31
• You have a tex file. So you are starting in tex. – Ulrike Fischer Jul 4 '15 at 21:40
• Note in Lua itself the backslash is also a escape character inside "...". You may want [[...]]. The latter is the equivalent (more or less) of r"..." in Python. – Javier Bezos Jul 5 '15 at 17:47
• @JavierBezos I can't get the [[..]] to work inside Latex. I see the correct string build by printing it, but the PDF shows grabage. Feel free to modify the example I have above to show me how. Here is one of my attempts.\documentclass{article} \usepackage{luacode} \begin{document} The following is my table \begin{luacode*} i=6; j=8; a = [[\begin{tabular}{|l|l|}\hline ]]..i.."&"..j..[[\\ \hline \end{tabular} ]]; print(a); tex.print(a); \end{luacode*} \end{document} I can do all this in Python using r""..."" so easily, I wonder why would I need to use Lua inside Latex for this. – Nasser Jul 6 '15 at 1:37

In Lua itself the backslash is also a escape character inside "...". Replace "..." by [[...]]. With the latter \ is not an escape character any longer:

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
i=6
j=8
tex.print([[\begin{tabular}{|l|l|}\hline]])
tex.print(i.."&"..j)
tex.print([[\\ \hline]])
tex.print([[\end{tabular}]])
\end{luacode*}
\end{document}


In my experience, new lines inside double brackets are problematic, so I avoid them.

• thanks. I tried everything, except removing the new lines. Since according to lua manual one can have long multi line string using [[...]] and they show an example there as well. So this tells me that Lua inside Tex is not really the same as Lua outside Tex. I tried my example I posted in the comment above, and it worked in the standalone Lua on my Linux. So it should also have worked inside Latex. I hope who ever maintain LuaTex can look into this and see why it does not work inside Latex as outside. – Nasser Jul 6 '15 at 15:56
• @Nasser I think new lines are more a problem with luacode* than luatex itself, but to be honest I haven't investigated. – Javier Bezos Jul 7 '15 at 15:46