1

I would like to use external packages for LuaLaTeX scripting (eg.penlight). I often test my scripts with texlua command without issues. However, I noticed when I compile the document with the lualatex command, an error is raised on require(). I am certain that package.path and package.cpath contain the directories where my external modules are stored. It seems like I can add modules by providing the absolute path to the external module, but this becomes an issue if the module relatively references another one.

So in summary, require() with relative paths is not working for lualatex, but works for texlua commands.

I know this is a related question, but it is 2011 and the solution is inelegant as I would have to manually change the require call for each external module. "Lua tree" (analogue of texmf tree)

Main.tex

\documentclass{scrartcl}
\directlua{dofile("FileInSameDirAsMainAndRequiresExternalModules.lua")}
\begin{document}
Hello World
\end{document}

FileInSameDirAsMainAndRequiresExternalModules.lua

_LUA_STUFF_DIR = 'C:/Program Files/MiKTeX 2.9/miktex/bin/x64' --just to be sure the path is there...
package.path = package.path..';'.._LUA_STUFF_DIR
package.cpath = package.cpath..';'.._LUA_STUFF_DIR --todo why does this not work for luatex but texlua
--Both calls below work with texlua command
pl = require('pl') -- this fails with lualatex as pl is not found
pl = require(_LUA_STUFF_DIR..'/pl/tablex') -- this also fails with lualatex, but differently
--it "gets" through to the pl.tablex module, but fails since tablex.lua had a require(pl.util) in it

Edit: Also tested this with the same issue

\documentclass{scrartcl}
\directlua{
LUASTUFFDIR = 'C:/Program Files/MiKTeX 2.9/miktex/bin/x64'
package.path = package.path..';'..LUASTUFFDIR
package.cpath = package.cpath..';'..LUASTUFFDIR
pl = require('pl')
}
\begin{document}
Hello
\end{document}
  • Have you tried "./FileInSameDirAsMainAndRequiresExternalModules.lua" ? – projetmbc Apr 17 '21 at 17:18
  • Yes, same issue--and that's because the problem is NOT that main.tex cannot find the "./FileInSameDirAsMainAndRequiresExternalModules.lua", it is because that .lua file fails on the require call is failing for relative paths--or perhaps require is not using package.path or package.cpath when searching. As I pointed out, I don't want to have to modify every module to point to an absolute path. – likethevegetable Apr 17 '21 at 17:21
  • 2
    when used with lualatex, the package search code is replaced and then use kpathsea. See the documentation of luapackageloader https://ctan.org/pkg/luapackageloader – Ulrike Fischer Apr 17 '21 at 17:44
  • Thanks, the frustrating part for me is that I've seen than package come up before and forgot about. Thanks! – likethevegetable Apr 17 '21 at 18:47
  • 5
    I’m voting to close this question because solved in comments – Dr. Manuel Kuehner Apr 17 '21 at 19:41

1 Answers1

0

As @UlrikeFischer pointed out, the \usepackage{luapackageloader} solved the issue.