[Tex/LaTex] Installing Lua Modules for use in LuaLaTeX

lualuatexmodules

I've done all of the searching that I can, and I think I've read the right answer, but am too dumb to know that I read it.

I can't seem to figure out the magic tricks to get Lua modules, installed by luarocks or by any other means, to show up in LuaLaTeX. So here is a minimal:

%!TEX TS-program = lualatex
\documentclass{article}
\usepackage{luatexbase}
\usepackage{luacode}
\begin{document}

    \directlua{package.path = "~/.luarocks/share/lua/5.1/?.lua;~/.luarocks/share/lua/5.1/?/init.lua;" .. package.path }

    \directlua{require('pl')}
    \directlua{require('htk')}

    \directlua{tex.sprint(package.path)}

    the standard approximation $\pi = \directlua{tex.sprint(math.pi)}$

\end{document}

In installed htk using luarocks with the command luarocks --local install HTK
. The Penlight module was installed the same way, except with penlight. Penlight uses the "pl/submodulename" scheme and htk uses just "htk.lua" in the ~/.luarocks/share/lua/5.1
directory.

The log output is:

./luamoduleload.tex:9: LuaTeX error <\directlua >:1: module 'pl' not found:
    no field package.preload['pl']
    [luatexbase.loader] Search failed
    [kpse lua searcher] file not found: 'pl'
    [kpse C searcher] file not found: 'pl'
stack traceback:
    [C]: in function 'require'
    <\directlua >:1: in main chunk.
l.9     \directlua{require('pl')}

The lua interpreter ran into a problem, so the
remainder of this lua chunk will be ignored.

./luamoduleload.tex:10: LuaTeX error <\directlua >:1: module 'htk' not found:
    no field package.preload['htk']
    [luatexbase.loader] Search failed
    [kpse lua searcher] file not found: 'htk'
    [kpse C searcher] file not found: 'htk'
stack traceback:
    [C]: in function 'require'
    <\directlua >:1: in main chunk.
l.10    \directlua{require('htk')}

The lua interpreter ran into a problem, so the
remainder of this lua chunk will be ignored.

Would more of the log file be helpful?

In truth, if i just knew where to instal the lua modules that I want to use, I'd be a happy camper, especially if I don't need to do any magic tricks to get stand-alone lua scripts to work outside of LuaLaTeX.

Please let me know if I can be any clearer or if I'm not reading the right fine manuals. 🙂

-Andrew

Best Answer

This line:

 \directlua{package.path = "~/.luarocks/share/lua/5.1/?.lua;~/.luarocks/share/lua/5.1/?/init.lua;" .. package.path }

looks suspicious. I think you need to have \noexpands before the tilde characters, otherwise the expansion of the ~ macro ends up as part of package.path.

\directlua{package.path = "\noexpand~/.luarocks/share/lua/5.1/?.lua;\noexpand~/.luarocks/share/lua/5.1/?/init.lua;" .. package.path }