0

Note: I am a total beginner to writing with Lua and I'm already struggling with installing Lua modules normally (something about VS Code not running the right code, I don't know, I'll ask over at StackOverflow). Hence, for the purposes of this question, please assume I am stupid and have never used a 'programming language' aside from LaTeX in my life.

For a teaching-school project, I've been asked to develop a resource to help school students work on their equation-solving skills. So I've decided to create a set of algorithmically (but randomly) generated equations with worked solutions, which would be generated and printed inside (well, in the .pdf) a TeX file at compilation. These .tex files would then be given freely to teachers to compile on their end. I've heard that Lua is good for this, as it's well integrated with LuaLaTeX and can easily be written as a separate code file and input with

\directlua{ code.lua }

My problem is that I really want to be able to access functions like map and functional-programming syntax from the LuaFun Library, but I'm really struggling even installing this on my own machine.

Since I want the .tex files to basically be 'plug and play' (assuming access to a proper TeX installation), this is way too much hassle & will prevent people from using my project. Instead, I want an easy way for me to access map etc., without every user having to have Lua and LuaFun already installed on their machine.

I tried copying fun.lua from the GitHub repo and just plain inputting that before my code, but that doesn't seem to work.

So what do I do? What's the best (read: simplest, most lightweight, and portable) way to do this?

Or is this this whole enterprise fundamentally flawed, and should I use somethhing like the python package?

Thanks in advance!

11
  • 1
    you should be able to input lua code with \directlua{require('fun.lua')} but other than that as you show no code or error hard to sa. Nov 30, 2023 at 12:46
  • 3
    Note that the lua in luatex is built in to the executable and is completely separate to any Lua you have installed as Lua on your system, and by default they use different search paths for their packages, so even if you have Lua installed and have installed (for Lua) some third party package it is not by default available to luatex's lua Nov 30, 2023 at 12:48
  • 1
    Your actual use case sounds easily doable in tex and you wouldn't need lua or python or any other language, there are existing latex packages to randomise question and answer sheets Nov 30, 2023 at 12:49
  • 1
    look at ctan for any exam package ctan.org/pkg/exam-randomizechoices for example has a relevant name (I haven't tried it) or search this site for random question has over 700 hits Nov 30, 2023 at 14:12
  • 1
    @Max Chernoff I'll probably end up using your first suggestion, but penlight is a great & helpful find, and closest achieves what I was originally asking. If you write it up into an answer I'll accept it :) Dec 2, 2023 at 11:49

1 Answer 1

2

When loading a Lua file, LuaLaTeX will search the locations given by the following command:

lualatex '\directlua{print((package.path..";"..kpse.show_path("lua")):gsub("[;:]",string.char(10)):gsub("!",""):gsub("/+", "/").."")}\csname@@end\endcsname'

This gives a really long list of system-dependent paths, but only the following locations really make any sense for your purposes:

  • . (the current directory): LuaTeX will search the current directory for any loaded files, so you can just put any .lua files directly in the same folder as your main .tex file, with no subdirectories between the two.

  • $TEXMFDIST/tex/lualatex/<packagename>/: If you're planning on uploading the package to CTAN, then you can put the .lua files in the same folder as your .sty files.

  • An absolute path: While your testing the package, something like

    require("/path/to/file.lua")
    

    will work, although this will make distributing the package quite tricky.

My problem is that I really want to be able to access functions like map and functional-programming syntax from the LuaFun Library, but I'm really struggling even installing this on my own machine.

TeX Live and MiKTeX already include the penlight package which contains many functional programming functions like map, filter, zip, etc.. According to the documentation, to load the package you just need to run either

local penlight = require("penlight")  -- Lua

or

\usepackage[pl, import]{penlight} % LaTeX

So I've decided to create a set of algorithmically (but randomly) generated equations with worked solutions, which would be generated and printed inside (well, in the .pdf) a TeX file at compilation.

The luacas package might be helpful for this.

1

You must log in to answer this question.

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