12

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}
1

1 Answer 1

6

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}
2
  • How to return the output to the Latex document instead of to the terminal? I tried tex.sprint([[#1]]) and tex.print([[#1]]) but both show everything on one line, not on separate lines as exactly the input was. Thanks.
    – Nasser
    Mar 30, 2022 at 22:00
  • fyi, I am ok now :) I found an example here
    – Nasser
    Mar 30, 2022 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .