1

I'm quite new to \LaTeX, but wanted to create a little macro much like \verb. Specifically, I'm writing a little package, in which I would like to pass an argument (that will quite often be containing special characters like %, #, },...) to Lua. I know it's possible by defining an environment (see this question).

However, I can't find any solution to pass such an argument by a single command. At least not without first save a verbatim text to a variable and then passing it as argument, which isn't quite as practical. All \verb-related packages seem to either be focusing on the verbatim environment, or on a version allowing to use it inside a macro - which doesn't help me.

Edit: a small code snippet to clarify: I have already something like:

<package.lua>

function process(input)
    -- do something with input
end

<document.tex>

\passToLua¨<some text with special characters %$[+=\#>¨

But I can't properly define a command that accepts a parameter with these special characters - ideally it would look something like the this (although I know it will probably look much uglier):

<package.sty>

\providecommand{\passToLua}[1]{  % This will need adapting as it doesn't accept %, #, ... in it's argument
    \directlua{
        require("package")
        process([[#1]])
    }
}

Anyone got an idea? A huge thanks in advance!

2
  • you don't give many clues but \newcommand\foo[1]{\directlua{s="\luaescapestring{\detokenize{#1}}"} may be what you are looking for Dec 25, 2022 at 21:19
  • @DavidCarlisle thanks for the quick answer, however I search something else. I edited my question and hope it's more clear now. Dec 25, 2022 at 22:22

1 Answer 1

1

You can define a verbatim (v) argument:

\documentclass{article}

\NewDocumentCommand\passToLua{v}{\directlua{
     print ("\string\n======\string\n")
     print ([[#1]])
     print ("\string\n======\string\n")
    }}
\begin{document}

\passToLua{a string with # \ and %}

\end{document}

Which will print

======

a string with # \ and %

======

to the terminal

2
  • Is there a way to make this work for characters \, %, { and } too? Dec 26, 2022 at 9:17
  • @WouterVermeulen it does work for them, as I showed. if you want a mis matched { you can use verb syntax using any unused character \passtolua| %{ | will pass space, percent, open brace space. Dec 26, 2022 at 9:29

You must log in to answer this question.

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