TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to use typo-dir.lua from ConTeXt distribution in LaTeX but I could not. I searched all context sources and I added all missing functions but still it did not work. Is it possible to use this Lua file in LaTeX? I am only trying to see how that works in LaTeX.

\luatexpardir TRT
\luatextextdir TRT

Edit: Using @phg's files, I get the following error:

./test-typo-dir.tex:4: LuaTeX error ...al/texlive/2013/texmf-dist/tex/context/ba
se/typo-dir.lua:48: attempt to index local 'mathcodes' (a nil value)
stack traceback:
    ...al/texlive/2013/texmf-dist/tex/context/base/typo-dir.lua:48: in main chunk
    [C]: in function 'require'
    ./import-typo-dir.lua:172: in main chunk
    [C]: in function 'require'
    ...xlive/2013/texmf-dist/tex/luatex/luatexbase/modutils.lua:57: in function 're
    [string "\directlua "]:1: in main chunk.
\lltxb@requirelua ...xluaescapestring {#2}" \fi )}


share|improve this question
welcome back to the site – David Carlisle Sep 8 '13 at 20:04
Thanks a lot David – Vafa Khalighi Sep 9 '13 at 1:49

Short, correct answer

Don’t do this. Use Context instead. The code is integrated too tightly into Context to be of practical use outside of it.

(I know this is not a real answer to your question but it’s nevertheless correct.)

Longer, detailed answer

Since you ask how hard it would be: I consider it a medium to hard task if you know what you are doing. Probably medium if you (unlike me) are already familiar with the Unicode Bidirectional Algorithm. This gist: https://gist.github.com/phi-gamma/6488187 contains the necessary Lua code as well as some example wrapper macros. It requires the beta (unstable) version of Context (the version in Texlive 2013 is too old) which is most conveniently acquired by installing the Standalone distribution.

In Context, Unicode bidi is implemented as a node processor, which is a managed version of Luatex callbacks. Normally, with some (in your case: extensive) preparation you can hook any of these processors into the raw callback mechanism. For an overview of what this entails you can refer to the steps taken by Luaotfload in order to load Context’s letterspacing code.

typo-dir.lua makes heavy use of Unicode character properties. For its purposes, the character information provided by Luaotfload does not suffice, so we have to load the entire character table (char-def.lua). Also, char-ini.lua is required because it installs a series of special handlers that facilitate access to these properties.

UPDATE On 2013-09-09 the bidi code was split into typo-dir.lua (node processor handling and interface), typo-dha.lua (main bidi node processor), and math-dir.lua (math bidi). The gist has been updated to reflect these changes. Judging from the source and by the recent activity further changes can be expected in the near future.

Besides those there are several further prerequisites: we need to properly map Context’s interface to Luatex attributes onto Luatexbase and preallocate three attributes that contain the bidi state. Also, the context namespace must be present, so we set up a rudimentary substitute which supports both the __index and the __call metamethods. These are only the more complex requirements. For orientation I annotated the source briefly.

I tested the result only against the demonstration section of typo-dir.mkiv. Automatic switching of directions appears to work. I cannot explain the differences in font rendering, though, but that’s not what you asked B-). This is what it looks like -- Latex on the left, Context on the right side:

Comparison between Context and Latex output

share|improve this answer
Thanks for the answer. Could you use typo-dir.lua with LaTeX? If the answer is yes, could you please show me how to use it in LaTeX? – Vafa Khalighi Sep 9 '13 at 1:49
Have you read the gist I linked? The included .tex shows how. – Philipp Gesang Sep 9 '13 at 8:14
Great, thanks. sorry I am almost getting old and my eyes sight gets weaker every day. – Vafa Khalighi Sep 9 '13 at 8:19
Oh, that’s sad to hear. Hope the code will get you started. – Philipp Gesang Sep 9 '13 at 8:31
Please see the edit in my question. When I run lualatex, I get error. – Vafa Khalighi Sep 9 '13 at 8:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.