Skip to content
riley levy edited this page May 25, 2024 · 8 revisions

LuaTeX is a version of the TeX typesetting system with an embedded Lua interpreter and customization points.

From the LuaTeX website

"LuaTeX is an extended version of pdfTeX using Lua as an embedded scripting language. The LuaTeX project's main objective is to provide an open and configurable variant of TeX while at the same time offering downward compatibility."

\directfennel: inserting Fennel code directly in a .tex file

The main command for embedding Lua code within TeX is \directlua. From the manual,

"...the basic \directlua command that does the job is:

\directlua{tex.print("Hi there")}

You can put code between curly braces but if it’s a lot you can also put it in a file and load that file with the usual Lua commands." (p. 19)

The following defines \directfennel, an analogous macro for embedding Fennel code within a .tex file:

\def\directfennel#1{%
  \directlua{require("fennel").install().eval("\luaescapestring{#1}")}}
% example usage:
\directfennel{(tex.print "hello, world")}

This code assumes fennel.lua is somewhere LuaTeX can find it, like the .tex file's directory. The call to tex.print sends data back to TeX: the macro \directfennel{(some-code)} expands to the text that was tex.print-ed while running (some-code).

Here's A Fennel-LaTeX hello world showing how to use this macro:

\documentclass{article}
\def\directfennel#1{%
  \directlua{require("fennel").install().eval("\luaescapestring{#1}")}}
\begin{document}
  \directfennel{(tex.print "hello, world")}
\end{document}

TeX expands the parameter of \directlua (and \directfennel) before sending it to the Lua interpreter. This allows passing data from TeX to Lua (and hence Fennel):

\def\greet#1{\directfennel{(tex.print "hello, ") (tex.print "#1")}}
\greet{world}
Clone this wiki locally