Is it possible be use directlua substitutions inside listings environment?

is it possible to insert a directlua substitution inside a listings environment?

for example -

\begin{luacode}
a=math.random()
b=math.random()
\end{luacode}

\begin{listings}
if(\directlua{tex.print(a)} > \directlua{tex.print(b)) {
result = tex.print(a);
}
else {
result = tex.print(b);
}
\end{listings}


I suppose that you mean lstlisting environment provided by listings package, which is used for typesetting source code listings. It provides escapechar option, which defines character that can be used to mark portions of code in which LaTeX macros are executed:

\documentclass{article}
\usepackage{luacode}
\usepackage{listings}
\begin{document}
\begin{luacode}
a=math.random()
b=math.random()
\end{luacode}

\begin{lstlisting}[escapechar=]
if(\directlua{tex.print(a)} > \directlua{tex.print(b)}) {
result = tex.print(a);
}
else {
result = tex.print(b);
}
\end{lstlisting}

\end{document}


Backtick () character is used in this example. As you can see, if you put it around \directlua command, it will be expanded: