I am trying to create a Lua script that lets me replace all instances of -> inside my equations with \rightarrow. I found this answer that suggests a hack involving a process_input_buffer callback:
-- replacearrow.lua
callback.register('process_input_buffer', function(line)
return line:gsub('%-%>', '\\rightarrow')
end)
% mydocument.tex
\directlua{ dofile('replacearrow.lua')}
Hello $A -> B$ World
The problem I'm getting now is that replacing text from the input buffer wholesale like this breaks a bunch of packages. I managed to restrain the damage a bit by only replacing text between dollar signs:
-- replacearrow.lua
callback.register('process_input_buffer', function(line)
return line:gsub('%$(.-)%$', function(mathstr)
mathstr = mathstr:gsubgsub('%-%>', '\\rightarrow')
return '$'..mathstr..'$'
end)
end)
Is there a way to make this also work for math-environments defined without dollar signs, such as \[ and \begin{math}?
Maybe the more precise way to do this would be to use some callback other than process_input_buffer but I don't know if there exists a callback that does what I want.
BTW, I am aware of the semantic package, which is what got hooked to using -> in the first place. The problem is that now that I converted to luatex that package is conflicting with the unicode-math, and some "Extended mathchar errors used as mathchar" show up. At this point, I decided that doing some preprocessing with Lua might end up being cleaner than the macro magic that the semantic package is doing.
verbatimorlistingsenvironments? You should probably be looking at themlist_to_hlistcallback. – Andrew Swann Jul 06 '15 at 12:54