summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/luapackageloader/luapackageloader.tex
blob: 4e2a9885ec99d468ded15f6ec2ea044377904eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage[colorlinks=true,
            linkcolor = blue,
            urlcolor  = blue,
            citecolor = blue,
            anchorcolor = blue]{hyperref}
\usepackage{xcolor}
\usepackage{minted}
\usepackage{csquotes}

\definecolor{LG}{HTML}{F9F9F9}

%% Informations
\title{\textsf{luapackageloader} package}
\author{Deepak Jois \\ \href{deepak.jois@gmail.com}{deepak.jois@gmail.com}}

\begin{document}
\maketitle

\section*{Introduction}
LuaTeX modifies the package loading behavior for Lua package, as mentioned in
Section 3.2 of the LuaTeX manual:

\begin{displayquote}
LuaTEX is able to use the kpathsea library to find require()d modules. For this
purpose, \texttt{package.searchers[2]} is replaced by a different loader function,
that decides at runtime whether to use kpathsea or the built-in core Lua
function. It uses kpathsea when that is already initialized at that point in
  time, otherwise it reverts to using the normal \texttt{package.path} loader.
\end{displayquote}

What this means is that once kpathsea is initialized, the normal package
loading behavior is no longer available.

This package allows Lua package loading behavior inside LuaTeX to use \textit{both}
kpathsea and the default loading mechanism. If the module is not available in kpathsea,
LuaTeX will try to load it from \texttt{package.path}. This functionality is very useful,
for instance when you need to use \href{https://luarocks.org/}{LuaRocks} modules inside LuaTeX.

\section{Installation}

Install this package like any other \TeX~or \LaTeX~package.

\section{Dependencies}

This package depends on \href{https://www.ctan.org/pkg/ifluatex}{ifluatex} CTAN package.

\section{Usage}

\subsection{Plain \TeX}
In Plain \TeX, you can import the module as follows:

\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
\input luapackageloader.sty
\end{minted}

\subsection{\LaTeX}

In \LaTeX, add the \texttt{luapackageloader} packages to your document:

\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
\usepackage{luapackageloader}
\end{minted}

\subsection{Loading Packages}

Once you have imported \texttt{luapackageloader}, it adds a table called
\texttt{luapackageloader} under the global namepace, and automatically
overrides the default LuaTeX package searchers. It should be possible to load a
Lua package (including \href{https://luarocks.org/}{LuaRocks} modules, as long
as \texttt{package.path} and \texttt{package.cpath} is setup properly in your
environment) as follows:

\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
\directlua{
  local serpent = require("serpent") % luarocks install serpent
}
\end{minted}

\subsection{Reverting to default loading behavior}

The package loading can be reverted back to the default LuaTeX behavior using the \texttt{restore\_kpse\_searchers()}
\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
\directlua{
  luapackageloader.resetore_kpse_searchers()
}
\end{minted}

\subsection{Manually setting up to load Lua packages}

This package exposes the function \texttt{add\_lua\_searchers()}, to manually setup the package searchers again, if it was reverted for some reason, after loading the package.

\begin{minted}[bgcolor=LG, fontsize=\footnotesize]{tex}
\input luapackageloader.sty
\directlua{
  % Let’s revert the package searchers to their default loading behavior.
  luapackageloader.restore_kpse_searchers()

 % Some code here...

  % Now let’s setup the package searchers to use Lua searchers again.
  luapackageloader.add_lua_searchers()
}
\end{minted}

\end{document}