Guidelines for using mylatexformat with luatex

I'm a big fan of custom LaTeX formats built with mylatexformat. So far, pdftex has been my default engine. I'm now going to use luatex as my default engine so I'd like to adapt my format customization workflow to luatex particularities.

What kind of packages/macros can I dump in my custom format for luatex?

• you can not dump lua state or opentype fonts so you need to avoid any luatex-specific setup: more trouble than it's worth, probably:-) Aug 12, 2016 at 16:25
• @DavidCarlisle Indeed. Hard to guess that e.g. pdftexcmds has a lua module without looking at the code. So no polyglossia, no fontspec, no tikz, no pgfplots, etc. That's really bad... Aug 12, 2016 at 16:35
• if it really mattered you could define things to catch the lua parts and load in \everyjob (as the latex format itself loads the luatexbase code in \everyjob) but really it's not a lot slower loading from a file than from a format so in most cases the benefits are probably not worth the complication Aug 12, 2016 at 18:12
• @DavidCarlisle Well, I'd be interested in a generic way to catch the lua code, say for pdftexcmds as an example without knowing the package has luacode. Aug 12, 2016 at 18:17
• no real generic way as if you load the tex into the format but delay the lua parts until everyjob, the relative order of execution is different and it may or may not need adjustment to the code in each case. Aug 12, 2016 at 18:19

It's a bit hard to make a system that would work for all packages, but for any given package you can usually make something work, I preload pdftexcmds and article class here.

I did try to see if you could redefine \directlua while making the format to save its argument to run automatically in \everyjob in documents using the custom format, but even in a simple package like pdftexcmds that is tricky as some \directlua you do need to restore, but some are just transient tests while loading the file which break if you do them later. Here in fact you just need to make sure that require("pdftexcmds") is executed.

Rather than use mylatexfmt I have used a simpler custom test, but it uses the same principles.

In a normal run the test file produces terminal output of

$lualatex cc421 This is LuaTeX, Version 1.10.0 (TeX Live 2019) restricted system commands enabled. (./cc421.tex LaTeX2e <2019-10-01> patch level 3 luaotfload | main : initialization completed in 1.882 seconds (/usr/local/texlive/2019/texmf-dist/tex/latex/base/article.cls Document Class: article 2019/10/25 v1.4k Standard LaTeX document class (/usr/local/texlive/2019/texmf-dist/tex/latex/base/size10.clo)) (/usr/local/texlive/2019/texmf-dist/tex/latex/pdftexcmds/pdftexcmds.sty (/usr/local/texlive/2019/texmf-dist/tex/generic/infwarerr/infwarerr.sty) (/usr/local/texlive/2019/texmf-dist/tex/generic/iftex/iftex.sty) (/usr/local/texlive/2019/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty)) (./cc421.aux) -1 | 0 | 1 (./cc421.aux)) 384 words of node memory still in use: 2 hlist, 1 vlist, 1 rule, 2 glue, 3 kern, 1 glyph, 5 attribute, 44 glue_spec , 5 attribute_list, 2 write nodes avail lists: 2:7,3:1,4:1,5:4,7:1,9:1 warning (pdf backend): no pages of output. Transcript written on cc421.log.  showing article loading and the -1 | 0 | 1 test of the Lua strcmp implementation. You can dump a format with luatex -ini \&lualatex cc421  and use it as follows showing a much reduced terminal output $ luatex \&cc421 cc421
This is LuaTeX, Version 1.10.0 (TeX Live 2019)
restricted system commands enabled.
(./cc421.tex
-1 | 0 | 1
(./cc421.aux))
369 words of node memory still in use:
2 hlist, 1 vlist, 1 rule, 2 glue, 3 kern, 1 glyph, 2 attribute, 44 glue_spec
, 2 attribute_list, 1 write nodes
avail lists: 2:12,3:2,4:1,5:4,7:3,9:3

warning  (pdf backend): no pages of output.
Transcript written on cc421.log.


The test file here is

\ifx\dumped\undefined
\documentclass{article}

\usepackage{pdftexcmds}

\begin{document}
\def\dumped{}
\dump
\else
\directlua{require("pdftexcmds")}
\fi

\makeatletter

\typeout{
\pdf@strcmp{aaa}{bbb} | \pdf@strcmp{aaa}{aaa} | \pdf@strcmp{bbb}{aaa}
}

\end{document}


To make this really usable you'd want to do the same for (at least) luaotfload, but hopefully it shows that the basic aim of preloading at least the tex part of major packages into the format is not impossible.