[Tex/LaTex] How to pass a LaTeX argument verbatim to lua

luatexmacros

I would like to pass an argument verbatim from LaTeX to lua including LaTeX commands and end of line characters. The example below takes care of the former issue, but not of the latter. Is there a way of addressing both issues simultaneously, perhaps by using \obeylines to address the second problem? If so, how?

There is a solution to a similar question at In LuaLaTeX, how do I pass the content of an environment to Lua verbatim? but it still prints output in addition to passing the argument.

Thanks!

\documentclass[12pt]{article}

\usepackage{luacode}

\directlua{dofile("myluaprogram.lua")}
\newcommand{\myluaroutine}[1]
   {\directlua{myluaroutine("\luatexluaescapestring{\detokenize{#1}}")}}

\begin{document}

\myluaroutine{
I
want
to 
have every line
separate
in
lua
without
expanding
off
\latexcommands
}

\end{document}

Best Answer

There are a few approaches. I'd go for the simplest in the first instance: a modified version of the standard verbatim environment

\documentclass{article}
\makeatletter
% A modified version of verbatim
\begingroup
  \catcode `| = 0 %
  \catcode `[ = 1 %
  \catcode `] = 2 %
  \catcode `\{ = 12 %
  \catcode `\} = 12 %
  \catcode `\\ = 12 %
  |gdef|@xluaverbatim#1\end{luaverbatim}%
    [|luaverbatimpayload[#1]|end[luaverbatim]]%
|endgroup
\def\luaverbatim{%
  \let\do\@makeother
  \dospecials
  \obeylines
  \obeyspaces
  \@xluaverbatim
}
\def\endluaverbatim{}
\def\luaverbatimpayload#1{%
  \directlua{%
    print([[#1]])
  }%
}
\makeatother

\begin{document}
\begin{luaverbatim}
I
want
to 
have every line
separate
in
lua
without
expanding
off
\latexcommands
\end{luaverbatim}
\end{document}
Related Question