2

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

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:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.