TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've written some macros that randomly selects questions from a database to include on an exam. The start of my .sty file reads as follows:

\ProvidesPackage{randqs}[2012/10/23 ver 0.1]



It seems that in my document, when I call


LuaTex (or whoever) is searching for randqs.lua in the directory of the .tex file rather than in the directory of the .sty file because if the .lua is in the former then it is found, while if it is in the latter then it is not. I'm guessing that dofile is not the way to go here, so my question is: how do I ensure that LuaTex (or whoever) looks for the file in the .sty directory rather than the .tex directory?

share|improve this question
up vote 6 down vote accepted

dofile() is a Lua command and looks for a file with the given path, in your case randqs.lua in the current directory, because you've given a relative path. There are two choices here:

  1. give a relative path to the style file (or an absolute path), but this is non portable, of course.
  2. or use require('randqs') (without .lua) to load the file. But beware: require() uses a special search path, see my answer to another question: "Lua tree" (analogue of texmf tree)
  3. From Khaled: A third option is to locate the file using kpse library, e.g. dofile(kpse.find_file("randqs.lua"))
share|improve this answer
A third option is to locate the file using kpse library, e.g. dofile(kpse.find_file("randqs.lua")). – Khaled Hosny Oct 26 '12 at 20:43
@KhaledHosny, topskip: Thank you both, after refreshing my file name database, both the kpse.find and require methods work. Is there any reason to prefer one to the other? – Scott H. Oct 26 '12 at 21:40
@KhaledHosny I keep forgetting about that. Thanks – topskip Oct 27 '12 at 5:59
@ScottH.: I’m not sure, require() looks cleaner, but kpse.find_file() might give you a bit more control on how to locate the files (LuaTeX manual gives more details on what options you can pass to it). – Khaled Hosny Oct 27 '12 at 22:55
@KhaledHosny Ok, thanks for the reply. – Scott H. Oct 27 '12 at 23:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.