[Tex/LaTex] How to use luacode* to eliminate need to escape “\”

luatex

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

enter image description here

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}

Mathematica graphics

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)
luatex-loader.sty    2010/03/09 v0.4 Lua module loader (HO)
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-loader.sty    2013/05/11 v0.6 Lua module loader 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)

Best Answer

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.

Related Question