Automatically filling in accents/diacritics for characters in LaTeX

accentsformattingmacros

There are a number of times when I'm writing LaTeX documents when I need to write a character with an accent or diacritic in it. For instance, the "ô" needed to write "L'Hôpital" (in reference to his titular rule) came up quite frequently in writing notes for my calculus class.

Of course, this extends to other accented characters and the like, often for names (e.g. Paul Erdős, René Descartes, Otto Hölder). Some can be seen here.

Writing these out in LaTeX has become a bit of a chore for me, mainly because I struggle to remember the actual codes to use them or often mix them up. I'm writing on a standard English keyboard as well, so "just type the character into the document" results in me having to copy-and-paste it from elsewhere. So either way I have to look something up – the list of codes, or something with the character in it.

It would be easier for me if I could have some sort of piece of code that automatically detects a relevant string, and replaces it with one with the corrected accent. So for instance, something that would see that I'd typed L'Hopital, and replace it with L'Hôpital instead, in the resulting PDF.

Of course, it would be trivial to define a macro that does it, something like

    \newcommand{\lhopital}{L'Hôpital}

but using this feels clunky and unnatural, especially for something that is almost surely going to be rendered outside of math mode.

I feel like something like this may be possible based on some searching I've done in the past. For instance, this post changes it so that := generates what you would expect, but with better vertical alignment of the colon with respect to the equal sign. Granted, I have a hard time parsing what exactly the snippet of code is doing… But regardless, that code works exactly like I'd envision: zero extra effort on my part aside from putting in the code.

So in short, what I'd ideally like is something which can automatically replace a text string I type in the document, with a particular text string I want to replace it with. Hence whenever I type L'Hopital I get L'Hôpital, Erdos I get Erdős, and so on and so forth – in general, I type stringA and obtain stringB in the output.

Thanks for any help you can give!

Best Answer

If you're willing and able to use LuaLaTeX, you could make use of its process_input_buffer callback, which can be made to perform as a pre-processor on the input stream, before TeX even starts its usual work. Users will just need to populate the name_table Lua table with suitable search and replacement strings.

enter image description here

\documentclass{article}

\directlua{ 
% -- Set up a Lua table with search and replacement strings:
  name_table = {
     { "L'?Hopital" , "L'Hôpital" },
     { "Erdos"      , "Erdős"     },
     { "Holder"     , "Hölder"    }, 
     { "Godel"      , "Gödel"     },
     { "Rene"       , "René"      },
  }
% -- Next, define the Lua function that does the actual work:
  function change_table_vals ( s )
    for _,j in pairs ( name_table ) do 
      s = s:gsub ( j[1] , j[2] ) 
    end
    return s
  end
}
%% assign the Lua function to the 'process_input_buffer' callback
\AtBeginDocument{\directlua{luatexbase.add_to_callback ( 
   "process_input_buffer" , change_table_vals , "change" )}}

\begin{document}
L'Hopital LHopital Erdos Holder Rene Godel
\end{document}
Related Question