0

I have some foo.lua code that needs to behave slightly differently when loaded from luatex or texlua. Here is my example document.

%!TeX Program=LuaLaTeX
\documentclass{article}
\RequirePackage{luacode}
\begin{document}
\begin{luacode}
require("foo.lua")
\end{luacode}
\end{document}

and here is foo.lua

if is_luatex then
  tex.sprint("Hello LuaTeX World!")
else
  print("Hello TeXLua World!")
end

How to define is_luatex? My first idea is to parse arg[0] but I really need a very strong solution that covers the widest range of situations (99.999%).

0

2 Answers 2

2

You can just test for the function you are using


if tex.sprint then
  tex.sprint("Hello LuaTeX World!")
else
  print("Hello TeXLua World!")
end

luatex typesets

enter image description here

texlua does

$ texlua foo.lua 
Hello TeXLua World!

If you first tested for the tex table, you could also detect stock Lua rather than texlua

2
  • Is it documented? Mar 7, 2021 at 15:53
  • @JérômeLAURENS I'm not sure what you mean? The construct is just basic Lua, the fact that texlua doesn't supply the tex related features in the tex table is sort of hinted at in the luatex manual but (like many things) could perhaps be more explicit. Mar 8, 2021 at 12:21
0

There is no documented straightforward flag to make the difference between LuaTeX and TeXLua, but we can take advantage of the fact that LuaTeX defines command sequences whereas TeXLua does not. The number of command sequences defined is available through status.cs_count:

local is_luatex = status.cs_count > 0
if is_luatex then
  tex.sprint("Hello LuaTeX World!")
else
  print("Hello TeXLua World!")
end
3
  • David's solution is stronger than yours, IMO. Unless you restrict yourself to LuaLaTeX, it won't work properly
    – user226564
    Mar 8, 2021 at 15:14
  • @Jairo Could you be more explicit? Mar 8, 2021 at 15:36
  • Nowadays there are LuaTeX (and variations of it) and LuaMetaTeX, the latter currently only used by ConTeXt. While the tex library is defined for both of them (and then the tex.print check works fine), status.cs_count will trigger an error message in latest ConTeXt. Again, if you're only interested in LuaLaTeX, your test is fine, but David's one is really as generic as possible.
    – user226564
    Mar 8, 2021 at 15:47

You must log in to answer this question.

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