3

I would like to create a command that simply prints what I give to it, but through Lua. Pointless for now, but I want to expand this to a more complex use-case. I've read a few answers around here, but can't quite figure it out. Simply put, if I give my command hello \bfseries world as an input, I'd like the output to show "hello world".

\documentclass{article}

\usepackage{luacode}

\newcommand\mycmd[1]{% \directlua{tex.sprint("--->".."\luatexluaescapestring{#1}") } }

\begin{document}

\mycmd{Hello world}

\mycmd{Hello \world} % a bit surprised \ works, why?

\mycmd{Hello \bfseries world} %

\end{document}

1 Answers1

1

Since your document loads the luacode package, you could employ its \luastringN macro -- the N stands for "no expansion" [of the argument before it gets passed to Lua] -- to get the job done. The point is that you need to prevent expansion of \bfseries.

I suppose that instead of \luastringN{#1}, you could also write "\luatexluaescapestring{\unexpanded{#1}}". I trust you will agree that the former is easier -- both to remember and to write out.

enter image description here

\documentclass{article}
\usepackage{luacode}
\newcommand\mycmd[1]{\directlua{ tex.sprint ( "--->" .. \luastringN{#1} ) }}

\begin{document} \mycmd{Hello world}

\mycmd{Hello \world} % a bit surprised \ works, why?

\mycmd{Hello {\bfseries world}} \end{document}

Mico
  • 506,678