To answer you update, to make the code you linked in the comments to work with latex you need to replace callback.register
with luatexbase.add_to_callback
, since latex does not support callback.register
.
Another change is to replace the line if n.id == node.id("whatsit") and n.subtype == 7 then
, since a dir node are not a whatsit node anymore.
The file char-def.lua
cannot be found in the linked code anymore, but I found it here. Here is an example file:
\documentclass{article}
\directlua{
require("char-def.lua")
local function mirror(hlist)
local rtl = false
if tex.textdir == "TRT" then
rtl = true
elseif tex.textdir == "TLT" then
rtl = false
end
for n in node.traverse(hlist) do
if n.id == node.id('hlist') then
mirror(n.list)
end
if n.id == node.id("dir") then
if n.dir == "+TRT" or n.dir == "-TLT" then
rtl = true
elseif n.dir == "-TRT" or n.dir == "+TLT" then
rtl = false
end
end
if n.id == node.id('glyph') then
if rtl then
local char = n.char
local mirror = characters.data[char].mirror
if mirror then
n.char = mirror
end
end
end
end
end
luatexbase.add_to_callback('pre_linebreak_filter',
function(h)
mirror(h)
return true
end,
'mirror')
}
\usepackage{fontspec}
\setmainfont{Alef}
\newcommand{\Heb}[1]{\bgroup\textdir TRT\fontspec[Script=Hebrew, Language=Hebrew]{Alef}#1\egroup}
\begin{document}
Alpha (Beta) Gamma;
\Heb{אלף (בית) גימל};
A (B) C
\end{document}
Another option is to use babel's data file, without the package itself. The code is pretty much the same, just few adjustments are needed:
\documentclass{article}
\directlua{
require("babel-data-bidi.lua")
local function mirror(hlist)
local rtl = false
if tex.textdir == "TRT" then
rtl = true
elseif tex.textdir == "TLT" then
rtl = false
end
for n in node.traverse(hlist) do
if n.id == node.id('hlist') then
mirror(n.list)
end
if n.id == node.id("dir") then
if n.dir == "+TRT" or n.dir == "-TLT" then
rtl = true
elseif n.dir == "-TRT" or n.dir == "+TLT" then
rtl = false
end
end
if n.id == node.id('glyph') then
if rtl then
local char = n.char
local mirror = Babel.characters[char]
if mirror then
n.char = mirror.m
end
end
end
end
end
luatexbase.add_to_callback('pre_linebreak_filter',
function(h)
mirror(h)
return true
end,
'mirror')
}
\usepackage{fontspec}
\setmainfont{Alef}
\newcommand{\Heb}[1]{\bgroup\textdir TRT\fontspec[Script=Hebrew, Language=Hebrew]{Alef}#1\egroup}
\begin{document}
Alpha (Beta) Gamma;
\Heb{אלף (בית) גימל};
A (B) C
\end{document}
Both produce the following:
As a side note, it is preferable to not use standalone
class for MWE's
char-def.lua
in addition to the linkedmirror.lua
.bidi
does not invert the brackets, it is a feature of the engine (XeTeX). See tex.stackexchange.com/a/686959/264024