TransWikia.com

Contextual substitution of one word by another with LuaLaTeX

TeX - LaTeX Asked on May 9, 2021

I would like to be able to use LuaLaTeX in such a way that one word is replaced by another according to its context of use.

Here’s a simple example: replace the word " and " (with a space before the "a" and after the "d") with " & ", when I write in italics.

But it’s not practical to always write "and" instead of "and". It’s easy to forget one "" and it reduces the speed of writing.

I’m not very familiar with all the possibilities offered by LuaLaTeX (I only recently abandoned PDFLaTeX), but it seems to me that LuaLaTeX allows such a word substitution without always using an "and" command.

Do you know how to do that?

documentclass{article}

begin{document}

LuaLaTeX code to automatically convert “textvisiblespace andtextvisiblespace” to “textvisiblespace&textvisiblespace” if texttt{textbackslash textit} is used : bigskip

noindent salt and pepper
textit{salt and pepper}
should produce :
salt and pepper
textit{salt & pepper}

end{document}

One Answer

documentclass{article}
usepackage{luacode}
begin{luacode}
     fonts.handlers.otf.addfeature {
         name    = "itand",
         type    = "chainsubstitution",
         lookups = {
             {
                 type = "ligature",
                 data = {
                     ['&'] = { "a", "n", "d" },
                 },
             },
         },
         data = {
             rules = {
                 {
                     -- the space is redundant as 0xFFFC contains it
                     before  = { { " ", 0xFFFC } },
                     after   = { { " ", 0xFFFC } },
                     current = { { "a" }, { "n" },  {"d" } },
                     lookups = { 1 },
                 },
              },
         },
     }
end{luacode}
usepackage{fontspec}

setmainfont{texgyretermes}[ItalicFeatures={RawFeature=+itand}]
begin{document}
abc and cde bande band and ande 
itshape abc and cde bande band and ande 
end{document}

enter image description here

Correct answer by Ulrike Fischer on May 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP