This question is probably trivial, but I started exploring Lua(La)TeX a couple of hours ago so bear with me.
The MWE is the following:
test.tex
:
\documentclass{article}
\directlua{require("test.lua")}
\begin{document}
\foo{bar}
\end{document}
test.lua
:
tex.print("\\newcommand{\\foo}[1]{")
tex.print("\\texttt{#1}")
-- arg = ?? how do I pass #1 to arg ??
-- arg = arg .. " baz"
-- tex.print("\\textit{" .. arg .. "}")
tex.print("}")
I know I could put \newcommand
in the tex
file and then call directlua
inside the command definition, but in my actual use case I'm looping over a table where the keys are the command names, so I need to leave that as is.
EDIT: As my initial question might have been a bit of an XY problem, I'm posting my real use case below.
My goal is: given a bunch of theorem environments defined by the thmtools
package, redefine them by embedding them in a tcolorbox
environment.
I started doing this via expl3
but got frustrated very quickly, partly because of my incompetence in the LaTeX3 way of programming, partly because programming in LaTeX is so painful compared to every other "normal" programming language (for loops, if statements, etc..), which is why is started diving deeper into LuaTeX.
Now to the code. I have this table
boxed_theorems = {
definition = "definition",
example = "example",
lemma = "theorem",
theorem = "theorem",
corollary = "theorem",
proof = "proof",
}
where the keys are environment names (again, coming from thmtools), and the values are tcolorbox
options (here definition
, example
, theorem
and proof
are a bunch of tcolorbox styles.
The way I loop over that table (and the rest of the .lua file included via \directlua
) is the following:
tex.print("\\ExplSyntaxOn")
for theorem,tcolorbox_theorem in pairs(boxed_theorems) do
-- copy <theorem_name> in a new environment inner<theorem_name>
tex.print("\\let\\inner" .. theorem .. "\\" .. theorem)
tex.print("\\let\\endinner" .. theorem .. "\\end" .. theorem)
tex.print("\\RenewDocumentEnvironment{ " .. theorem .. " }{ O{} }{")
-- I want to be able to call the new environment like this:
--
-- \begin{theorem}[name={foo}, label={bar}, tcolorbox={baz}]
--
-- where 'name={foo}, label={bar}' is the argument of the
-- innertheorem and should be assigned to 'theorem_options',
-- while '{baz}' is an argument of the embedding tcolorbox and
-- should be assigned to 'tcolorbox_local'.
-- I haven't got that far yet (I need to create a function that
-- parses #1, hence the original post), so for now they are just
-- left empty.
theorem_options = ""
tcolorbox_local = ""
-- The rest is pretty self-explanatory
tex.print("\\begin{tcolorbox}[" .. tcolorbox_theorem .. ","
.. tcolorbox_local .. "]")
if theorem_options == "" then
tex.print("\\begin{inner" .. theorem .. "}")
else
tex.print("\\begin{inner" .. theorem .. "}[" .. theorem_options .. "]")
end
tex.print("}{")
tex.print("\\end{inner" .. theorem .. "}")
tex.print("\\end{tcolorbox}")
tex.print("}")
end
tex.print("\\ExplSyntaxOff")
tex.print(\\directlua{luaarg='#1'})
but I am not sure that is the answer you want.