3

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)

Igor Liferenko
  • 7,063
  • 2
  • 14
  • 47
  • 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) – David Carlisle Oct 07 '15 at 08:14
  • 1
    Look at section 7.2 of the LuaTeX manual. – egreg Oct 07 '15 at 08: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 07 '15 at 12:27
  • 2
    see also http://tex.stackexchange.com/a/262631/2891 for some comments – michal.h21 Oct 07 '15 at 12:39

1 Answers1

4

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
David Carlisle
  • 757,742
  • 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 07 '15 at 09: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. – David Carlisle Oct 07 '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 08 '15 at 11:16
  • But it does work - please leave it there, just in case somebody will face the same problem. – Igor Liferenko Oct 08 '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 08 '15 at 12:23
  • Why this directlua call stops working if I put it to the format file? – Igor Liferenko Oct 08 '15 at 22:12
  • @IgorLiferenko the lua state is not dumped into the format, you need to put it into a file then put \everyjob{\directlua(require("yourfile")} into the format so the code runs each time, lualatex already does this for some things (or for small bits of code you can of course inline it as \everyjob{\directlua{function xx ....}} – David Carlisle Oct 08 '15 at 23:20
  • Sorry to ask it off-topic - please help: I have found a minor inconsistency in the console output of tex vs. luatex. Is anybody concerned about this? If yes, where this can be reported? – Igor Liferenko Oct 22 '15 at 06:26
  • @IgorLiferenko the luatex list (luatex@tug.org) but note that compatibility of log output is explicitly not an aim of luatex (which sometimes causes us difficulties with the latex test suite:-) – David Carlisle Oct 22 '15 at 06:48