I'm writing a package mostly in Lua files. So far I haven't found a way to test in the Lua code whether a given package is loaded. What I'm doing is using \@ifpackageloaded
and call into a Lua function:
% Configure cref names if cleveref is loaded
\makeatletter
\@ifpackageloaded{cleveref}%
{\directlua{lyluatexmp.set_cref_names()}}%
\makeatother
But I think the information should be available for Lua as well, making it significantly easier to deal with this issue.
[EDIT:]
While the solution in this answer does work (on TeX Live 2019/2017) it doesn't work when the function is moved into an external Lua file (which is where I'd want it to be):
local function loaded(ext)
local fmt = "ver@\\csstring\\%s." .. ext
return function(name)
return not not token.get_macro(fmt:format(name))
end
end
return {
package = loaded"sty",
class = loaded"cls"
}
in a test-two.lua file (note the double backslashes that are necessary here) and
\directlua{
local mod = require('test-two.lua')
classloaded = mod.class
packageloaded = mod.package
}
\directlua{
if classloaded"article" then
texio.write_nl"`article` loaded"
else
texio.write_nl"`article` not loaded"
end
}
\documentclass{article}
\directlua{
if classloaded"article" then
texio.write_nl"`article` loaded"
else
texio.write_nl"`article` not loaded"
end
if packageloaded"cleveref" then
texio.write_nl"`cleveref` loaded"
else
texio.write_nl"`cleveref` not loaded"
end
}
\usepackage{cleveref}
\directlua{
if packageloaded"cleveref" then
texio.write_nl"`cleveref` loaded"
else
texio.write_nl"`cleveref` not loaded"
end
}
\stop
always return false
. It turns out that the result of get_macro
is always nil
in the second implementation (both TeX Live 2017 and 2019) while token.create(fmt:format(name)).command
always returns the value for undefined_cs
in the first implementation.
Is there anything that can be done about that?