[luatex] Accessing external Lua libraries

Faheem Mitha faheem at faheem.info
Thu Mar 25 13:12:38 CET 2021

On Wed, 24 Mar 2021, Michal Vlasák wrote:

> Andreas, what Faheem suggests should work for you: use luapackage and
> set package.path/packge.cpath to what you want. You should see section
> 6.3 "Modules" of https://www.lua.org/manual/5.3/manual.html and also the
> source code of luapackageloader (luapackageloader.lua) to understand how
> this works. Also you may have to disable some security precautions
> (--shell-escape and no --safer).
> Something like this should hopefully work for your setup:
> ```
> \input luapackageloader.sty
> \bgroup
> \catcode`~=12
> \catcode`\%=12
> \directlua{
> local luarocks_path = kpse.expand_path("~/.luarocks/share/lua/5.3")
> local luarocks_cpath = kpse.expand_path("~/.luarocks/lib/lua/5.3")
> package.path  = package.path  .. string.format(";%s/?.lua;%s/?/init.lua", luarocks_path, luarocks_path)
> package.cpath = package.cpath .. string.format(";%s/?.so;%s/?/init.so", luarocks_cpath, luarocks_cpath)
> module = require("module")
> }
> \egroup
> \bye
> ```
> Note that this appends to existing paths as you saw them earlier and
> expects the ~/.luarocks directories to exist.
> Michal

I only have a sketchy idea about these things. I discovered 
luapackageloader by accident some time ago. Before I was using it, I had 
no idea how to load external packages. package.path and package.cpath just 
ignored whatever I added to them.

So, a couple of questions.

First, is it necessary to add stuff to the search path at the TeX end 
(kpse or whatever)? My experience is that adding stuff at the Lua end 
suffices for my needs. But of course my experience does not cover all use 
cases, and I think I've avoided using luarocks.

Second, is there some reason not to include the functionality of 
luapackageloader by default in LuaTeX? Then the Lua paths would work as 
expected. It might still make sense to have the functionality as a 
separate package, in case people wanted to disable that functionality for 
some reason. Though I can't imagine why they would want to.

I originally became aware of LuaTeX around 2012, but at that time I could 
not figure out a way to load external packages, though since I don't 
recall asking for help at the time, I probably did not pursue it very 
hard. But if this had worked out of the box for me then (and as stated I 
can't see any reason why it shouldn't), I would have started using LuaTeX 
much earlier.

Regards, Faheem Mitha

More information about the luatex mailing list.