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 LuaHBTeX, Version 1.15.1 (TeX Live 2023/dev)
restricted system commands enabled.
(./cc421.tex
LaTeX2e <2022-11-01> patch level 1
L3 programming layer <2022-12-17>
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/article.cls
Document Class: article 2022/07/02 v1.4n Standard LaTeX document class
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2022/texmf-dist/tex/generic/pdftexcmds/pdftexcmds.sty
(/usr/local/texlive/2022/texmf-dist/tex/generic/infwarerr/infwarerr.sty)
(/usr/local/texlive/2022/texmf-dist/tex/generic/iftex/iftex.sty)
(/usr/local/texlive/2022/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty))
(/usr/local/texlive/2022/texmf-dist/tex/latex/l3backend/l3backend-luatex.def)
(./cc421.aux) (/usr/local/texlive/2022/texmf-dist/tex/latex/base/ts1cmr.fd)
-1 | 0 | 1
(./cc421.aux))
413 words of node memory still in use:
3 hlist, 1 vlist, 1 rule, 2 glue, 3 kern, 1 glyph, 5 attribute, 48 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
luahbtex -ini \&lualatex cc421
and use it as follows showing a much reduced terminal output
$ luahbtex \&cc421 cc421
This is LuaHBTeX, Version 1.15.1 (TeX Live 2023/dev)
restricted system commands enabled.
(./cc421.tex
-1 | 0 | 1
(./cc421.aux))
398 words of node memory still in use:
3 hlist, 1 vlist, 1 rule, 2 glue, 3 kern, 1 glyph, 2 attribute, 48 glue_spec
, 2 attribute_list, 1 write nodes
avail lists: 2:13,3:2,4:2,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.
updated to use luahbtex as required for current latex
pdftexcmds
has a lua module without looking at the code. So nopolyglossia
, nofontspec
, notikz
, nopgfplots
, etc. That's really bad...