2

I want to make a character active/sensitive and pass its preceding character as an argument of some command. I could do it using LuaLaTeX engine and luacode package. Thanks to this and this post.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function myligs ( s )
   s = unicode.utf8.gsub ( s , '(\\?)([%a@]+)' , function( backslash, text )
       if backslash=='' then 
           text = unicode.utf8.gsub(text, '(.)(æ)', '%1%\\textbf{%1}' )  
       end
       return backslash .. text
   end)
   return s
end
\end{luacode}

\begin{document} \directlua{luatexbase.add_to_callback("process_input_buffer", myligs, "myligs")} aæ

bæ \end{document}

but this seems to not work with dependent diacritic characters. See the following example -

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function myligs ( s )
   s = unicode.utf8.gsub ( s , '(\\?)([%a@]+)' , function( backslash, text )
       if backslash=='' then 
           text = unicode.utf8.gsub(text, '(.)(◌́)', '%1%\\textbf{%1}' )  
       end
       return backslash .. text
   end)
   return s
end
\end{luacode}

\begin{document} \directlua{luatexbase.add_to_callback("process_input_buffer", myligs, "myligs")}

a◌́ \end{document}

Probably the dependent diacritic is not interpreted in the same way a normal character is. Can somebody please explain me what is exactly wrong in the second code? I want the output of both of these codes to be identical i.e. aa where the second a is bold.

Mico
  • 506,678
Niranjan
  • 3,435
  • The reason why your second example doesn't work is because the dependent diacritic character ◌́ does not match either element of the [%a@] "character class" (a Lua jargon term), i.e., either %a ("any alphabetic character") or @ ("at character"). Because the ◌́ character is never matched in the first-stage or "outer" gsub operation, it doesn't get passed on to the second-stage or "inner" gsub operation, and hence no match involving this character can ever occur in that second stage. – Mico Sep 20 '20 at 14:43
  • 1
    Thanks for this explanation. This answer helped me to complete a package named unitipa ! I have submitted it. Soon it'll appear on the CTAN. I have cited your answer in the documentation. It helped me a lot. :) If you are curious about the package you can see it here. – Niranjan Sep 20 '20 at 16:22
  • 1
    I look forward to checking out your package when it becomes available on the CTAN. – Mico Sep 20 '20 at 18:03
  • 1
    https://ctan.org/pkg/unitipa – Niranjan Sep 22 '20 at 13:30

1 Answers1

7

Dependent diacritic characters are somewhat special; I will assume that they never occur inside the names of LaTeX commands and environments. If this assumption is correct, there's no need for the two-stage gsub procedure employed in your code.

The following code assumes that the character immediately before the dependent diacritic character is an alphabetic character (%a).

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{luacode}
\begin{luacode}
function makebold ( s )
   return ( unicode.utf8.gsub ( s , '(%a)◌́' , '%1\\textbf{%1}' ) )
end
\end{luacode}

\begin{document} a◌́

% Assign 'makebold' to 'process_input_buffer' callback: \directlua{luatexbase.add_to_callback("process_input_buffer", makebold, "makebold")}

a◌́ \end{document}

Mico
  • 506,678
  • 1
    Thank you so much! This worked for me. I would just like to know what does the makebold, "makebold" stand for? – Niranjan Sep 20 '20 at 09:13
  • 1
    @Niranjan - Since the Lua function doesn't appear to be related to the processing of ligatures, I thought it might be a good idea to give it a more descriptive name -- makebold. (You're obviously free to choose a different name.) The luatexbase.add_to_callback function takes three, mandatory arguments: the name of the LuaTeX callback, the name of the Lua function to be assigned to that callback, and a handle. The names of the Lua function and of the handle do not have to be identical. – Mico Sep 20 '20 at 09:20
  • Thanks, the explanation was very helpful :) – Niranjan Sep 20 '20 at 10:00
  • How to mark a following character in this code? – Niranjan Sep 20 '20 at 10:30
  • @Niranjan - Do you mean the character that follows a dependent diacritic? – Mico Sep 20 '20 at 10:31
  • Yes, I need something like this a -> diacritic -> b gives \textbf{ab} – Niranjan Sep 20 '20 at 10:34
  • Right now what we have is a -> diacritic gives \textbf{a} – Niranjan Sep 20 '20 at 10:36
  • 1
    s = unicode.utf8.gsub ( s , '(%a)◌́(%a)', '%1\\textbf{%1%2}' ). The items in round parentheses before and after the dependent diacritic in the search phrase are called "captures" in Lua jargon, and they can be addressed as %1 and %2 in the action phrase. – Mico Sep 20 '20 at 10:38
  • 1
    Lua is just amazing! I simply love it! Thanks :) – Niranjan Sep 20 '20 at 10:41
  • 1
    @Niranjan - Lua's pattern-matching capabilities are indeed immensely powerful. – Mico Sep 20 '20 at 12:43