Suppose that we have a font with a fixed encoding and want to change mapping between input characters and font slots. How to configure luatex so that if we type A we get B, not changing the font? An example input file follows.

\font\tenrm=cmr10 \tenrm

In the output document we need to get B:

$ luatex test.tex

NOTE: only luatex solution is needed (not lualatex)

  • 1
    Note your note doesn't really make sense, any solution will involve some tex macros and some lua code, so why object to any of those that come from the latex sources? Latex isn't a different executable just tex with some macros (and lua code in the case of lualatex) Oct 7, 2015 at 8:14
  • 1
    Look at section 7.2 of the LuaTeX manual.
    – egreg
    Oct 7, 2015 at 8:58
  • You shouldn't really change the question in edits, better to ask a new one, also better to show working code, If you put return string.gsub(buf,"A", "\u{1071}") into \directlua then it needs to be return string.gsub(buf,"A", "\string\u{1071}") to stop \u expanding, but if you don't show code, I have to guess. Oct 7, 2015 at 12:27
  • 2
    see also tex.stackexchange.com/a/262631/2891 for some comments
    – michal.h21
    Oct 7, 2015 at 12:39

1 Answer 1


Note this maps all the input so you can't use A at all, even in command names, unless you write a smarter mapping function

function atob (buf)
    return string.gsub(buf,"A", "B")

\font\tenrm=cmr10 \tenrm
  • How to specify unicode code points for OFM font's slots in place of "B" (in hexadecimal), and in place of "A" - real utf-8 characters? Oct 7, 2015 at 9:01
  • 1
    For utf aware you need unicode.utf8.gsub as the standard lua string library doesn't know about unicode. You can generate a utf8 sequence in a literal lua string using \u{XXX} where XXX are the hex digits. Oct 7, 2015 at 10:33
  • @IgorLiferenko I rolled back the edit as it was misleading string.char is not unicode aware as I stated in the comment above, it works in the latin1 range only. You need the unicode.utf8 functions to use UTF-8. Oct 8, 2015 at 11:16
  • But it does work - please leave it there, just in case somebody will face the same problem. Oct 8, 2015 at 11:42
  • @IgorLiferenko no it needs updating to use unicode.utf8.char (or if the font API is 1 byte then use string.char but not described as generating utf8 sequences which it does not do) If I get time I may fix it and put it back, but as it was it was simply wrong and I'd rather not have it in an answer under my name (even if it works in this case) Or if you fix it you could re-suggest an edit. Oct 8, 2015 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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