9

I'm writing a Lua Module for LuaLaTeX.

Does a LuaTeX C API exist?

For example, how can I use the C version of tex.print into my C code?

Thanks in advance

  • It is impossible to call the C version of tex.print in your C code, because the corresponding C function is marked as static and is therefore only accessible from within the same translation unit. – Henri Menke Feb 14 at 20:50
14

I am by far a person that can give you an authorative answer. As I have written in a comment to David, there is no way to use TeX as a (shared) library. But you can use LuaTeX to load modules and execute LuaTeX's functions.

Say you want to call texio.write_nl("Hello world!"). You have to write a small C program to do that:

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>


static int callprint (lua_State *L)
{
    lua_getglobal(L, "texio");
    lua_getfield(L, -1, "write_nl");
    lua_remove(L, -2);
    lua_pushstring(L, "Hello, world!");
    lua_call(L, 1, 0);
    return 1;
}


int luaopen_printfromc(lua_State *L) {
  struct luaL_Reg myfuncs[] = {
    {"callprint", callprint},
    {NULL, NULL},
  };
  lua_newtable (L);
  luaL_setfuncs (L, myfuncs, 0);
  return 1;
}

Then you can compile this as a shared library, on my Mac it is:

cc -c  printfromc.c -I /opt/homebrew/Cellar/lua/5.2.4_1/include/
cc -flat_namespace -bundle -undefined suppress -o printfromc.so  printfromc.o 

and copy the result to a TeX directory

cp printfromc.so /some/path/to/TeX/luatex/test

where you can load this library from the TeX source:

\documentclass{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
x = require("printfromc")
x.callprint()

\end{luacode*}
\end{document}
  • Thanks for taking the time to write this. This is really interesting. – cjorssen May 21 '17 at 11:16
5

In theory you can (although making work across all the platforms supported by texlive is tricky) look at the FFI module up to TeXlive 2016 that requires luajittex rather than luatex but in texlive 2017 prerelease there is some support to work with luatex.

Alternatively there is the swig library for an alternative linking mechanism.

links for ffi and swig

http://www.luatex.org/svn/trunk/source/libs/luajit/LuaJIT-src/doc/ext_ffi_semantics.html

http://www.luatex.org/swiglib.html

  • You are right, of course, but I'd answer 'no' to the question. In a strict sense, there is no C-API where you can load LuaTeX as a library. One can work around it... – topskip May 21 '17 at 10:19
  • @topskip er yes perhaps I misread the direction the OP was going. Want to put up another answer? I could just delete this but the links might be useful in anycase. – David Carlisle May 21 '17 at 10:21
  • No, I think your answer is good (+1). I made my comment just in case the OP was looking for a C-API in the sense of TeX as a library. Perhaps (unlikely) I'll give an additional one with an example. – topskip May 21 '17 at 10:33
  • @DavidCarlisle FFI will not work for this reason. – Henri Menke Feb 14 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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