# why luacode environments do not ignore lua comment — lines?

I do understand (a little) the difference between luacode and luacode* But I almost fell of my seat when something I wrote inside a commented line caused lualatex to scream at me. I changed luacode* to luacode and got an error due to something in a comment.

I would have thought a comment is a comment, and this tells lualatex to please do not look at anything on this line, regardless if I am running luacode or luacode*

This compiles OK

\documentclass[12pt]{scrbook}%
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}
\usepackage{luacode}
\begin{luacode*}
function foo()
--print("\begin{verbatim}".."\end{verbatim}")
tex.print("done")
end
\end{luacode*}

\begin{document}
\directlua{foo()}
\end{document}


Changed it to luacode, and now it failed

\documentclass[12pt]{scrbook}%
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}

\usepackage{luacode}
\begin{luacode}
function foo()
--print("\begin{verbatim}".."\end{verbatim}")
tex.print("done")
end
\end{luacode}

\begin{document}
\directlua{foo()}
\end{document}


error is

lualatex foo2.tex
This is LuaTeX, Version 0.95.0 (TeX Live 2016)
restricted system commands enabled.
.....
(/usr/local/texlive/2016/texmf-dist/tex/luatex/ctablestack/ctablestack.sty)))
! Undefined control sequence.
\luacode@begin ...#1\relax \edef \luacode@endmark
{\string \end {\@currenvir...

l.10 \end{luacode}
?


But the line is commented out !! really?

From lua help:

I am was trying things. This makes it hard to play around with code and comment lines, if I have to worry also about what is inside comments affecting parser.

Question is : Why comments lines are not ignored in luacode?

• luacode package manual lists which constructs work in which form in a table on page 2. (lucode and luacode* are both documented as supporting -- comments.) – David Carlisle Jun 20 '16 at 21:10

If you change the line

--print("\begin{verbatim}".."\end{verbatim}")


to

--print("\\begin{verbatim}".."\\end{verbatim}")


your code will compile just fine whether you employ the luacode or the luacode* environment.

In the luacode environment, TeX macros are expanded. Since Lua and TeX "see" the backslash character differently, you need to write \\, which Lua will re-write as \ before passing it to TeX; of course, \ is what you want TeX to see.

In contrast, in the luacode* environment, TeX macros are not expanded, and hence it doesn't matter what comes after the -- comment initiator.

• +1 for \\  (even though \\begin is in a comment here, that answers what would be the next question:-) – David Carlisle Jun 20 '16 at 22:14

The luacode package manual lists which constructs work in which form in a table on page 2. (lucode and luacode* are both documented as supporting -- comments.)

The problem is occurring before lua is being called as you have not prevented \begin and \end expanding in TeX while the chunk to be passed to lua is constructed. You need \string\begin and \string\end or similar (or better \\ as Mico notes as you would want \\ not \ if it were not a comment) .

The whole point of luacode* is it changes the catcode of \ so \begin is taken as 6 characters, not a tex command to be expanded.

The error is unrelated to the Lua comment syntax, Lua never started, you get the same error from

\begin{luacode}
a b wibble
?? xx ("\begin{verbatim}".."\end{verbatim}")
@@@@
end
\end{luacode}