3

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:

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

\RequirePackage{xparse}

\directlua{dofile('randqs.lua')}

It seems that in my document, when I call

\documentclass[answers]{exam}
\usepackage{randqs}

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?

1 Answer 1

6

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"))
4
  • A third option is to locate the file using kpse library, e.g. dofile(kpse.find_file("randqs.lua")). Oct 26, 2012 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, 2012 at 21:40
  • @KhaledHosny I keep forgetting about that. Thanks
    – topskip
    Oct 27, 2012 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). Oct 27, 2012 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .