Type definitions for the Lua API of LuaTeX
and related projects.
LuaTeX has a very large Lua
API. This project tries to make this API accessible in the text editor
of your choice. This is made possible by the
lua-language-server - a
server that implements the Language Server Protocol
(LSP) for the
Lua
language. Features such as code completion, syntax highlighting
and marking of warnings and errors, should therefore not only be
possible in Visual Studio Code, but in
a large number of
editors that support
the LSP
.
The type definitions are published on CTAN as a single file to avoid cluttering the CTAN directory with many individual Lua files. Since this one file is just under 1.5 MB in size, a configuration must be made so that the language server can load the file. The following configuration example sets the preload file size to a maximum of 5000 kB.
{
"Lua.workspace.preloadFileSize": 5000,
}
There are several ways to include type definitions in a project.
The easiest way is to copy the file into the project folder.
Or you can use the configuration Lua.workspace.library
:
{
"Lua.workspace.library": ["/path/to/luatex-type-definitions.lua"]
}
LuaCATS is a Github organisation and stands for “Lua Comment And Type System”. This organization provides a place for community projects to live. These projects are addons for popular libraries/frameworks. The repositories in this organization are used by LLS-Addons, a repository that is used by the addon manager of the VS Code extension for the Lua Language Server.
This repositories in LuaCATS are related to this project:
- lmathx
- lpeg
- luafilesystem
- luaharfbuzz
- luasocket
- luazip
- lzlib
- md5
- slnunicode
- tex-lualatex
- tex-lualibs
- tex-luametatex
- tex-luatex
The following repositories are upstream projects. This means: The type definitions are developed in a LuaCATS repository and pulled in by this project.
- LuaCATS: lmathx → library/luametatex/lmathx.lua
- LuaCATS: lpeg → library/luatex/lpeg.lua
- LuaCATS: luaharfbuzz → library/luatex/luaharfbuzz.lua
- LuaCATS: luasocket → library/luatex/socket.lua → library/luatex/mime.lua
- LuaCATS: luazip → library/luatex/zip.lua
- LuaCATS: lzlib → library/luatex/zlib.lua
- LuaCATS: md5 → library/luatex/md5.lua
- LuaCATS: slnunicode → library/luatex/unicode.lua
The following repositories are downstream projects. This means: The type definitions are developed in this project. They are then pushed into a LuaCATS repository.
- LuaCATS: tex-lualatex ← library/lualatex
- LuaCATS: tex-luatex ← library/luatex
- LuaCATS: tex-lualibs ← library/lualibs
- LuaCATS: tex-luametatex ← library/luametatex
In the subfolder library
are files named after the global libraries
they document. For example, the library/tex.lua
file contains the
documentation for the tex
library. These Lua files don’t contain
real Lua code. They consist only of function bodies and empty
tables. The main focus is in the docstrings.
The API documentation is written in a well documented annotation format. This format is based on the EmmyLua format. Unfortunately, the Lua community has not yet been able to agree on a standarized annotation format. Many Lua project are documented in the LDoc format. However, the differences between these formats are marginal.
The actual definitions are located in the directory library
. This
directory is divided into further subdirectories. In the folder luatex
you will find the definitions that the engine LuaTeX provides. The
folder lualibs
documents the extension library of the same name. If
you use lualatex
, you may be interested in the folder of the same
name.
The folder resources
contains TeX manuals and HTML online
documentation converted into Lua docstrings.
The example
folder contains TeX and Lua files for demonstrating
and testing the documented Lua API.
2025/07/24 v0.2.0
Copyright (C) 2022-2025 by Josef Friedrich josef@friedrich.rocks
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
Install Visual Studio Code and the lua-language-server.
git clone https://github.com/Josef-Friedrich/LuaTeX_Lua-API.git
.vscode/settings.json
:
{
"Lua.workspace.library": [
"<repo>/library"
]
}
The preferred method of contributing to the project is via Github pull requests. You can also email patches to josef@friedrich.rocks. It is ok if you only document the data types of the input parameters.
Use imperative mood for the first line: https://peps.python.org/pep-0257/
Please contribute!
messages
Default message:
---😱 [Types](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/library/luatex/pdf.lua) incomplete or incorrect? 🙏 [Please contribute!](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/pulls)
No documentation at all:
---
---Warning! Undocumented code!<p>
---TODO: Please contribute
---https://github.com/Josef-Friedrich/LuaTeX_Lua-API#how-to-contribute
---
---@param s string
function tex.sp(s) end
---
---@param s string
---
---@return integer
function tex.sp(s) end
Or if you have more time you can also expand the documentation to this level:
---@meta
tex = {}
---
---Convert a string `s` that represents an explicit
---dimension into an integer number of scaled points.
---
---For parsing the string, the same scanning and conversion rules are used that
---*LuaTeX* would use if it was scanning a dimension specifier in its *TeX*-like
---input language (this includes generating errors for bad values), expect for the
---following:
---
---* only explicit values are allowed, control sequences are not handled
---* infinite dimension units (`fil...`) are forbidden
---* `mu` units do not generate an error (but may not be useful either)
---
---__Example:__
---
---```lua
---local scaled_points = tex.sp('1cm')
---print(scaled_points) -- 1864679
---```
---
---__Reference:__
---
---* `LuaTeX` manual: 10.3.15.5 `sp` page 204
---* Source file of the `LuaTeX` manual: [luatex-tex.tex#L1386-L1413](https://gitlab.lisn.upsaclay.fr/texlive/luatex/-/blob/f52b099f3e01d53dc03b315e1909245c3d5418d3/manual/luatex-tex.tex#L1386-L1413)
---
---@param s string # A string to convert into scaled points.
---
---@return integer # The dimension in the scaled points format.
function tex.sp(s) end
The docstring above is rendered as follows in Visual Studio Code:
The description text can be or was taken from the official LuaTeX
reference manual.
In the project folder
resources/manuals/luatex
you will find slightly edited Lua
versions of the LuaTeX
manual
sources.
- Preamble
- Basic TEX enhancements
- Modifications
- Using LUATEX
- Languages, characters, fonts and glyphs
- Font structure
- Math
- Nodes
- LUA callbacks
- The TEX related libraries
- The graphic libraries
- The fontloader
- The HarfBuzz libraries
- The backend libraries
This quick hacked and very ugly Python script resources/manuals/convert-tex-to-lua-docstrings.py was used to convert the source files.
Some Lua files contain a table named _N
. _N
stands for navigation.
With the help of this table and the outline view of the editor, it is
easier to navigate through the documentation. The name is inspired by
the global Lua table _G
. Many parts of the documentation, such as the
definition of the various Node
classes, are not shown in the outline.
In the released version, this navigation table is removed using the
manage.py
management script.
---A helper table to better navigate through the documentation using the
---outline: https://github.com/Josef-Friedrich/LuaTeX_Lua-API#navigation-table-_n
_N = {}
The different node types are defined as classes. Since this class definition takes place entirely in the comments, it is not displayed in the outline.
_N.hlist = 0
---@class HlistNode: ListNode
_N.vlist = 1
---@class VlistNode: ListNode
The following example refers to section “8.7.2 is_node” on page 149 in the LuaTeX documentation.
_N._8_7_2_is_node = "page 149"
LuaTeX
makes extensive use of function overloading. The following
example is taken from the LuaTeX
manual:
<number> w, <number> h, <number> d =
node.dimensions(<node> n)
<number> w, <number> h, <number> d =
node.dimensions(<node> n, <string> dir)
<number> w, <number> h, <number> d =
node.dimensions(<node> n, <node> t)
<number> w, <number> h, <number> d =
node.dimensions(<node> n, <node> t, <string> dir)
<number> w, <number> h, <number> d =
node.dimensions(<number> glue_set, <number> glue_sign, <number> glue_order, <node> n)
<number> w, <number> h, <number> d =
node.dimensions(<number> glue_set, <number> glue_sign, <number> glue_order, <node> n, <string> dir)
<number> w, <number> h, <number> d =
node.dimensions(<number> glue_set, <number> glue_sign, <number> glue_order, <node> n, <node> t)
<number> w, <number> h, <number> d =
node.dimensions(<number> glue_set, <number> glue_sign, <number> glue_order, <node> n, <node> t, <string> dir)
This can easily be done by documenting the function with the same name but different signatures multiple times.
Issue for further improvement of the function loading
Function overloading in tex.sp()
A node (object) can be described by the @class
annotation and provided
with some documentation about its attributes using @field
. There is a base class Node
for all node type classes.
---
---A node that comprise actual typesetting commands. A few fields are
---present in all nodes regardless of their type, these are:
---
---@class Node
---@field next Node|nil # the next node in a list, or nil
---@field prev Node|nil # That prev field is always present, but only initialized on explicit request ...
The KernNode
class for example inherits from Node
and represents
a kern node.
---
---The `kern` command creates such nodes but for instance the font and math
---machinery can also add them.
---
---@class KernNode: Node
---@field subtype KernNodeSubtype
---@field kern integer # Fixed horizontal or vertical advance (in scaled points)
---@alias KernNodeSubtype
---|0 # fontkern
---|1 # userkern
---|2 # accentkern
---|3 # italiccorrection
The @cast
annotation forces a unspecific node to a distinct node type.
while n do
if n.id == node.id('kern') then
---@cast n KernNode
print(n.kern)
end
n = n.next
end
Use --[[@as <node type>]]
to force a node type onto an expression.
local kern = node.new('kern') --[[@as KernNode]]
How a callback function is documented is shown using the
pre_linebreak_filter
as an example.
---
---The string called `groupcode` identifies the nodelist's context within
---*TeX*'s processing. The range of possibilities is given in the table below, but
---not all of those can actually appear in `pre_linebreak_filter`, some are
---for the `hpack_filter` and `vpack_filter` callbacks that will be
---explained in the next two paragraphs.
---@alias PreLinebreakFilterGroupCode
---|'' # main vertical list
---|'hbox' # hbox` in horizontal mode
---|'adjusted_hbox' #hbox` in vertical mode
---|'vbox' # vbox`
---|'vtop' # vtop' #
---|'align' # halign` or `valign`
---|'disc' # discretionaries
---|'insert' # packaging an insert
---|'vcenter' # vcenter`
---|'local_box' # localleftbox` or `localrightbox`
---|'split_off' # top of a `vsplit`
---|'split_keep' # remainder of a `vsplit`
---|'align_set' # alignment cell
---|'fin_row' # alignment row
---
---As for all the callbacks that deal with nodes, the return value can be one of
---three things:
---
---* boolean `true` signals successful processing
---* `<node>` signals that the “head” node should be replaced by the
--- returned node
---* boolean `false` signals that the “head” node list should be
--- ignored and flushed from memory
---@alias NodeCallbackReturn true|false|Node
---
---# `pre_linebreak_filter` callback
---
---This callback is called just before *LuaTeX* starts converting a list of nodes
---into a stack of `hbox`es, after the addition of `parfillskip`.
---
---```lua
------@type PreLinebreakFilter
---function(head, groupcode)
--- --- true|false|node
--- return true
---end
---```
---
---This callback does not replace any internal code.
---@alias PreLinebreakFilter fun(head: Node, groupcode: PreLinebreakFilterGroupCode): NodeCallbackReturn
Annotation your custom callback function with @type
.
---@type PreLinebreakFilter
local function visit_nodes(head, group)
return true
end
luatexbase.add_to_callback('pre_linebreak_filter', visit_nodes, 'visit nodes')
Quick info node.id(type)
Type error in node.id(type)
node.id(type)
type definition
Quick info node.write(n)
Documentation for the field data
of the pdf_colorstack
node:
- Template of the Lua interface itself
- In the lua-language-server included 3rd party libraries
- FiveM Lua Stubs
---
---__Reference:__
---
---* Corresponding C source code: [ltexlib.c#L1430-L1436](https://gitlab.lisn.upsaclay.fr/texlive/luatex/-/blob/f52b099f3e01d53dc03b315e1909245c3d5418d3/source/texk/web2c/luatexdir/lua/ltexlib.c#L1430-L1436)
---* Source file of the `LuaTeX` manual: [luatex-nodes.tex#L1199-L1211](https://gitlab.lisn.upsaclay.fr/texlive/luatex/-/blob/f52b099f3e01d53dc03b315e1909245c3d5418d3/manual/luatex-nodes.tex#L1199-L1211)
---* Corresponding plain TeX control sequence: [\sfcode](https://www.tug.org/utilities/plain/cseq.html#sfcode-rp)
---* Victor Eijkhout. “TeX by Topic” (1991, 2007) [page 185](http://mirrors.ctan.org/info/texbytopic/TeXbyTopic.pdf)
---* Donald Ervin Knuth. “The TexBook” (1984): page 76. Also: 76, 271, 285, 363, 433. [TeX-Source](https://ctan.org/tex-archive/systems/knuth/dist/tex/texbook.tex) [PDF](https://visualmatheditor.equatheque.net/doc/texbook.pdf)
---David Bausum. “TeX Reference Manual” (2002) [](https://www.tug.org/utilities/plain/cseq.html)
---
---__Reference:__
---
---* Source file of the `LuaTeX` manual: []()
---
/usr/local/texlive/texmf-dist/tex/luatex/lualibs/lualibs-basic-merged.lua /usr/local/texlive/texmf-dist/tex/luatex/lualibs/lualibs-extended-merged.lua
Filename | ConTeXt | LaTeX |
---|---|---|
dir.lua | context/base/mkiv/l-dir.lua | lualibs-dir.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
file.lua | context/base/mkiv/l-file.lua | lualibs-file.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
gzip.lua | lualibs-util-zip.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
context/base/mkiv/l-io.lua | lualibs-io.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
lfs.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
lpeg.lua | context/base/mkiv/l-lpeg.lua | lualibs-lpeg.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
lualibs.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
lua.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
math.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
modules.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
number.lua | context/base/mkiv/l-number.lua | lualibs-number.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
os.lua | context/base/mkiv/l-os.lua | lualibs-os.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
package.lua | context/base/mkiv/l-package.lua | lualibs-package.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
set.lua | context/base/mkiv/l-set.lua | lualibs-set.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
statistics.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
string.lua | context/base/mkiv/l-string.lua | lualibs-string.lua |
boolean.lua | context/base/mkiv/l-boolean.lua | lualibs-boolean.lua |
lpeg.lua | context/base/mkiv/l-lpeg.lua | lualibs-lpeg.lua |
unicode.lua | context/base/mkiv/l-unicode.lua | lualibs-unicode.lua |
util-dim.lua (dimensions ) |
context/base/mkiv/util-dim.lua | lualibs-util-dim.lua |
util-str.lua (strings ) |
context/base/mkiv/util-str.lua | lualibs-util-str.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
table.lua | context/base/mkiv/l-table.lua | lualibs-table.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
trackers.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
url.lua | context/base/mkiv/l-url.lua | lualibs-url.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
utf.lua |
Filename | ConTeXt | LaTeX |
---|---|---|
util-deb.lua (debugger ) |
context/base/mkiv/util-deb.lua | lualibs-util-deb.lua |
util-fil.lua (files ) |
context/base/mkiv/util-fil.lua | lualibs-util-fil.lua |
util-jsn.lua (json ) |
context/base/mkiv/util-jsn.lua | lualibs-util-jsn.lua |
util-lua.lua (lua ) |
context/base/mkiv/util-lua.lua | lualibs-util-lua.lua |
util-prs.lua (parsers ) |
context/base/mkiv/util-prs.lua | lualibs-util-prs.lua |
util-sac.lua (streams (string access)) |
context/base/mkiv/util-sac.lua | lualibs-util-sac.lua |
util-sta.lua (stacker ) |
context/base/mkiv/util-sta.lua | lualibs-util-sta.lua |
util-sto.lua (storage ) |
context/base/mkiv/util-sto.lua | lualibs-util-sto.lua |
util-str.lua (strings ) |
context/base/mkiv/util-str.lua | lualibs-util-str.lua |
util-tpl.lua (templates ) |
context/base/mkiv/util-tpl.lua | lualibs-util-tpl.lua |
The Makefile provides targets for printing the global namespace available on the specific engines. The Makefile uses some Lua functions to output all functions and tables of the global namespace that can be found in utils.lua.
To print the LuaTeX global namespace:
make namespace_luatex
To print the LuaMetaTeX global namespace:
make namespace_luametatex