
我對 lualatex 和 mongo 有疑問
% !config
% arara: lualatex :{shell : yes , synctex : yes}
% arara : tikzmake :{ jobs : 1, force : yes}
\documentclass[12pt,a4paper]{book}
\usepackage{Bibles}
\usepackage{lipsum}
%\usepackage[utf8x]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\author{PADDEU Dylan}
\title{bible accessibilité}
\usepackage{ifluatex}
\usepackage{luatexbase}
\usepackage{luacode}
\begin{document}
\maketitle
\begin{luacode*}
dofile("bible.lua")
\end{luacode*}
\end{document}
我的lua代碼:
mongo = require('mongo')
client = mongo.Client('mongodb://127.0.0.1:27017')
ERP = client:getCollection('bible','Etablissement Recevant du Public (ERP)')
vote = client:getCollection('bible','bureau de vote')
enseignement = client:getCollection('bible',"etablissement enseignement")
logement = client:getCollection('bible','logement')
transport = client:getCollection('bible','transport')
voirie = client:getCollection('bible','voirie')
stationnement = client:getCollection('bible','carte de stationnement')
travail = client:getCollection('bible','lieux de travail')
CCIA = client:getCollection('bible','commission communale et intercommunale accessibilite')
CCDSA = client:getCollection('bible','COMMISSION CONSULTATIVE DEPARTEMENTALE DE SECURITE ET D’ACCESSIBILITE')
formation_acces = client:getCollection('bible','formation accessibilite')
test1 = client:getCollection('bible','test')
function essais()
local q = mongo.BSON{_id=9}
local r = test1:findOne(q):value()
print("nom du domaine",r.domaine)
end
和問題
module 'mongo' not found:
no field package.preload['mongo']
[kpse lua searcher] file not found: 'mongo'
[kpse C searcher] file not found: 'mongo'
stack traceback:
[C]: in function 'require'
./bible.lua:1: in main chunk
[C]: in function 'require'
[\directlua]:1: in main chunk.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }
看待
答案1
我不能自稱只是 Luatex 的涉足者,也不能說是真正的 Lua 人;我希望那些比我更有見識的人能夠原諒我的錯誤,但我至少可以報告我的經驗。
在 Luatex 中,package.searchers
被替換,因此當您使用require
TeX 的模組時,Luatex 使用 TeX 的搜尋機製而不是通常的搜尋機制。這意味著在實踐中,當您從 Luatex 中執行 Lua 時,它不會找到正常系統 Lua 找到的模組。 (我對架構了解不夠,但我認為這是故意的,因為它希望能夠安全地維護不同的平行結構,並確保 LuaTeX 始終選擇適合 TeX 的模組,即使其他模組安裝在其他地方。
實際上,這意味著如果您透過 luarocks 安裝了 lua 模組,則需要在kpathsea
可以找到的目錄中放置一個副本,或包含一個符號連結。據我所知,Luarocks 是一個非常簡單的東西,它只是將文件轉儲到一個方便的目錄中,因此您應該能夠找到它們並連結到它們或複製它們。該文件相當可愛地警告“當然,編寫一個替代加載器並在宏包中使用它沒什麼大不了的”,儘管對於我們中的一些人(比如我!)來說這可能是一個很大的問題。
如果你使用的 lua 模組不是純 Lua 而是依賴從 C 編譯的程式碼,我沒有經驗如何發揮作用slnunicode
。 。我希望它能“正常工作”,但是...
當你正在建造一些東西,你想要既作為獨立的東西又作為與 TeX 綁定的東西來測試時,我透過反覆試驗發現:
如果您需要(如您想像的那樣)僅在一種環境或另一種環境中執行的程式碼,請測試
lua.version
在 Luatex 中執行時將定義的定義。我認為嚴格來說,只需檢查 lua 就足夠了(檢查lua.version
不安全,因為如果lua
未定義,則會出錯)。if lua and lua.version then -- we're in LuaTeX else -- we're not end
並行開發的一種方法是用作
texlua
lua 解釋器。如果你這樣做,你將要自動存取靜態連結庫,這些庫始終是 luatex 的一部分(例如LPEG
和slnunicode
,即不需要require
它們)。您的腳本將或多或少地「就像」透過 TeX 文件的呼叫運行一樣。但有一個問題。 Texlua 不會自動「找到」該kpathsea
函數,因此您需要「告訴」它做什麼:kpse.set_program_name("kpsewhich") -- or whatever
現在,例如
local xml = require("luaxml-domobject")
會在 TeX 目錄樹中找到適當的模組,使用: 沒有這個魔法,你會因為找不到模組而
kpsewhich
收到令人沮喪的抱怨。package.loader
或者,更糟的是,你會得到A模組具有該名稱,但不是當 Luatex“真正”負責時您將得到的模組。
下面的程式碼可能比解釋更容易理解!如果你運行它,texlua
你會得到一個輸出;如果lua
輸出略有不同,但在兩種情況下都有效。
local luatexing = lua and lua.version
-- local lpeg shadows global lpeg if luatex is running this
local lpeg = lpeg
if luatexing then
kpse.set_program_name("kpsewhich")
else
-- if luatex is not in charge we need to require the library
lpeg = require("lpeg")
end
-- The output here will differ depending on
-- whether we have luatex (with statically
-- linked unicode library) or not. Of course,
-- we could then `require` something
-- appropriate.
if luatexing then
io.write(unicode.utf8.lower("Ê") .. "\n")
else
io.write(string.lower("Ê") .. "!\n")
end
-- But this will work in either case: with
-- luatex via the statically linked lpeg
-- library. With regular lua via the require
j = lpeg.P("a matcher")
io.write(j:match("a matcher") .. "\n")
我不知道的是,因為我還沒有真正探索過,是否需要小心地kpathsea
從已設定的 TeX 文件中直接調用的文件中刪除該設定。但出於開發目的,此設定(如果確實需要)使您能夠相當接近從 TeX 運行中調用腳本時解釋腳本的方式。