# How to change input encoding in LuaTeX?

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
A
\bye
``````

In the output document we need to get `B`:

``````\$ luatex test.tex
``````

NOTE: only luatex solution is needed (not lualatex)

• 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) – David Carlisle Oct 7 '15 at 8:14
• Look at section 7.2 of the LuaTeX manual. – egreg Oct 7 '15 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. – David Carlisle Oct 7 '15 at 12:27

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

``````\directlua{
function atob (buf)
return string.gsub(buf,"A", "B")
end
callback.register('process_input_buffer',atob)
}

\font\tenrm=cmr10 \tenrm
A
\bye
``````
• 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? – Igor Liferenko Oct 7 '15 at 9:01
• 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. – David Carlisle Oct 7 '15 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. – David Carlisle Oct 8 '15 at 11:16
• But it does work - please leave it there, just in case somebody will face the same problem. – Igor Liferenko Oct 8 '15 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. – David Carlisle Oct 8 '15 at 12:23