CTAN Comprehensive TeX Archive Network

Directory macros/luatex/generic/luatex-type-definitions

README.md

Lua Lua API

Type definitions for the Lua API of LuaTeX and related projects.

Lua 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.

Subprojects

Distribution ...

via CTAN

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"]
}

via Visual Studio Code Extension

via LuaCATS git respositories

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).

All related LuaCATS repositories

This repositories in LuaCATS are related to this project:

Upstream LuaCATS repositories

The following repositories are upstream projects. This means: The type definitions are developed in a LuaCATS repository and pulled in by this project.

Subprojects (push) detached from this repository

The following repositories are downstream projects. This means: The type definitions are developed in this project. They are then pushed into a LuaCATS repository.

Directory structure of the repository

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.

Directory library

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 Lua 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.

Directory resources

The folder resources contains manuals and HTML online documentation converted into Lua docstrings.

Directory examples

The example folder contains and Lua files for demonstrating and testing the documented Lua API.

Current version

2025/07/24 v0.2.0

License

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/.

Installation / Setup for Visual Studio Code

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"
  ]
}

How to contribute

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

Minimal example of tex.sp()

---
---@param s string
function tex.sp(s) end

Less minimal example of tex.sp()

---
---@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:

Prime example of tex.sp()

---@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 scaledpoints = tex.sp('1cm') —print(scaledpoints) – 1864679 —— —Reference:


LuaTeX manual: 10.3.15.5 sp page 204 — Source file of the LuaTeX 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:

![](resources/images/tex.sp.png)

The description text can be or was taken from the official [LuaTeX
reference manual](https://github.com/TeX-Live/luatex/tree/trunk/manual).
In the project folder
[resources/manuals/luatex](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/tree/main/resources/manuals/luatex)
you will find slightly edited `Lua` versions of the `LuaTeX` manual
sources.

1.  [Preamble](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/01_preamble.tex.lua)
2.  [Basic TEX enhancements](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/02_enhancements.tex.lua)
3.  [Modifications](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/03_modifications.tex.lua)
4.  [Using LUATEX](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/04_lua.tex.lua)
5.  [Languages, characters, fonts and glyphs](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/05_languages.tex.lua)
6.  [Font structure](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/06_fonts.tex.lua)
7.  [Math](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/07_math.tex.lua)
8.  [Nodes](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/08_nodes.tex.lua)
9.  [LUA callbacks](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/09_callbacks.tex.lua)
10. [The TEX related libraries](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/10_tex.tex.lua)
11. [The graphic libraries](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/11_graphics.tex.lua)
12. [The fontloader](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/12_fontloader.tex.lua)
13. [The HarfBuzz libraries](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/13_harfbuzz.tex.lua)
14. [The backend libraries](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/luatex/14_backend.tex.lua)

This quick hacked and very ugly Python script
[resources/manuals/convert-tex-to-lua-docstrings.py](https://github.com/Josef-Friedrich/LuaTeX_Lua-API/blob/main/resources/manuals/convert-tex-to-lua-docstrings.py)
was used to convert the source files.

### Navigation table `_N`

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.
If the API documentation is published, the `_N` table can be commented
out.

### Documentation of function overloading

`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> glueset, <number> gluesign, <number> glueorder, <node> n) <number> w, <number> h, <number> d = node.dimensions(<number> glueset, <number> gluesign, <number> glueorder, <node> n, <string> dir) <number> w, <number> h, <number> d = node.dimensions(<number> glueset, <number> gluesign, <number> glueorder, <node> n, <node> t) <number> w, <number> h, <number> d = node.dimensions(<number> glueset, <number> gluesign, <number> glueorder, <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](https://github.com/sumneko/lua-language-server/issues/1456)

Function overloading in `tex.sp()`

![](resources/images/tex.sp_overload.png)

### Documentation of nodes

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.
lua

—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.
lua

—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)


 lua —@alias KernNodeSubtype —|0 # fontkern —|1 # userkern —|2 # accentkern —|3 # italiccorrection

The `@cast` annotation forces a unspecific node to a distinct node type.
lua 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.
lua local kern = node.new('kern') –@as KernNode

![](resources/images/cast.png)


### Documentation of callback functions

How a callback function is documented is shown using the
`pre_linebreak_filter` as an example.

#### @alias `PreLinebreakFilterGroupCode`
lua

—The string called groupcode identifies the nodelist's context within —'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—|'adjustedhbox' #hbox in vertical mode—|'vbox' # vbox—|'vtop' # vtop' # —|'align' # halign or valign—|'disc' # discretionaries —|'insert' # packaging an insert —|'vcenter' # vcenter—|'localbox' # localleftbox or localrightbox—|'splitoff' # top of a vsplit —|'splitkeep' # remainder of a vsplit —|'alignset' # alignment cell —|'finrow' # alignment row


#### @alias `NodeCallbackReturn`

lua

—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


#### @alias `PreLinebreakFilter`
lua

—# pre_linebreak_filter callback


—This callback is called just before Lua starts converting a list of nodes —into a stack of hboxes, 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`.
lua —@type PreLinebreakFilter local function visitnodes(head, group) return true end

luatexbase.addtocallback('prelinebreakfilter', visitnodes, 'visit nodes')


![](resources/images/PreLinebreakFilter.png)

Quick info `node.id(type)`

![](resources/images/node.id.png)

Type error in `node.id(type)`

![](resources/images/node.id_wrong-type.png)

`node.id(type)` type definition

![](resources/images/node.id_definition.png)

Quick info `node.write(n)`

![](resources/images/node.write.png)

Documentation for the field `data` of the `pdf_colorstack` node:

![](resources/images/PdfColorstockWhatsitNode.data.png)

## Documentation

* [LuaTeX Repo](https://gitlab.lisn.upsaclay.fr/texlive/luatex/) [LuaTeX Mirror](https://github.com/TeX-Live/luatex)
* [pdfTeX Mirror](https://github.com/tex-mirror/pdftex)

## Howtos

* [Custom libraries](https://github.com/sumneko/lua-language-server/wiki/Libraries#custom)
* [@meta annotation](https://github.com/sumneko/lua-language-server/wiki/Annotations#meta)

## Other type definition / stub repos:

* [Template of the Lua interface itself](https://github.com/sumneko/lua-language-server/tree/master/meta/template)
* [In the lua-language-server included 3rd party libraries](https://github.com/sumneko/lua-language-server/tree/master/meta/3rd)
* [FiveM Lua Stubs](https://github.com/jamie-34254/fivem_lua_stubs)


## References
lua

Reference:


Corresponding C source code: ltexlib.c#L1430-L1436 Source file of the LuaTeX manual: luatex-nodes.tex#L1199-L1211 Corresponding plain control sequence: sfcode Victor Eijkhout. “ by Topic” (1991, 2007) page 185 Donald Ervin Knuth. “The TexBook” (1984): page 76. Also: 76, 271, 285, 363, 433. -Source PDF —David Bausum. “ Reference Manual” (2002)


 lua

Reference:


Source file of the LuaTeX manual:


Global namespaces

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 Lua global namespace:

make namespace_luatex

To print the LuaMeta global namespace:

make namespace_luametatex

Download the contents of this package in one zip archive (568.3k).

luatex-type-definitions – Type definitions for the Lua API of Lua

Lua 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.

Packageluatex-type-definitions
Bug trackerhttps://github.com/Josef-Friedrich/LuaTeX_Lua-API/issues
Repositoryhttps://github.com/Josef-Friedrich/LuaTeX_Lua-API
Version0.2.0 2025-07-24
LicensesGNU General Public License, version 2 or newer
Copyright2022–2025 Josef Friedrich
MaintainerJosef Friedrich
Contained inTeX Live as luatex-type-definitions
TopicsLua
Lua supp
...
Guest Book Sitemap Contact Contact Author