2

I'm trying to convert some strings with accented letters. I'd like to be able to remove only the accent marks while keeping the letters. (E.g. convert ô to o, so Hôspital would become Hosiptal).

What is the best method for achieving this?

I can do it like this

\documentclass{article}
\usepackage[T1]{fontenc}

\ExplSyntaxOn
\tl_new:N  \l_accented_tl
\cs_generate_variant:Nn \regex_replace_all:nnN { neN }
\seq_set_from_clist:Nn \g_my_seq { é , è , ê , à , â , ô , ö, î , ï , ç , É , È , Ê , À , Â , Ô , Ö , Î , Ï , Ç }
\prop_const_from_keyval:Nn \g_my_pl  {
    é = e , è = e , ê = e ,
    à = a , â = a ,
    ô = o , ö = o ,
    î = i , ï = i , ç = c , 
    É = E , È = E , Ê = E ,
    À = A , Â = A ,
    Ô = o , Ö = O , 
    Î = I , Ï = I , Ç = C }

\NewDocumentCommand{\remove}{ m }
 {
    \tl_set:Nn \l_accented_tl { #1 }
    \seq_map_inline:Nn \g_my_seq
      {
        \regex_replace_all:neN { ##1 } { \prop_item:Nn \g_my_pl { ##1 } } \l_accented_tl
      }
    \tl_use:N \l_accented_tl
 }
\ExplSyntaxOff
\begin{document}
\remove{Hörmander}, \remove{Hôspital} 
\end{document}

Is it possible to achieve this with somthing like

\tl_set:Nn \l_accented_tl { #1 }
    \regex_replace_all:nnN { (é)|(è)|(à)|(ç) } { (?1:e)|(?2:e)|(?3:a)|(?4:c) } \l_accented_tl
\tl_use:N \l_accented_tl

or better?

2
  • Presumably you mean "Hôspital would become Hospital". However, I'm a bit confused because "Hôspital" isn't the correct spelling, but the circumflex indicates an <s> was removed, so perhaps you mean "Hôpital" becomes "Hospital"? Aug 3, 2021 at 18:50
  • @AzorAhai-him- Thank you :-). Aug 5, 2021 at 11:22

1 Answer 1

6

If you're using pdflatex, then it's very simple: just redefine (locally) the accent macros to be no-ops.

\documentclass{article}

\makeatletter
\newcommand{\removeaccents}[1]{%
  \begingroup
  \let\'\@firstofone
  \let\`\@firstofone
  \let\^\@firstofone
  \let\"\@firstofone
  % add the other accent commands you need
  #1
  \endgroup
}
\makeatother

\begin{document}

\removeaccents{Hörmander}

\removeaccents{Hôpital}

\end{document}

enter image description here

Oh, by the way, “Hôspital” is wrong: it's either “Hospital” or “Hôpital”. The famous marquis used the latter spelling.

6
  • This is even better. But still curious, in regex_replace solution. Aug 3, 2021 at 10:49
  • 1
    @studentmaths In pdflatex ô is actually two byte…
    – egreg
    Aug 3, 2021 at 12:38
  • i didn't undestande what you mean. Aug 5, 2021 at 11:33
  • For my real need this is exactly what i want, however, the aim of my question is to understand regex_replace so i am really sorry. What i want is replace a set of letters one by one (the case of accented letters is also is also interesting because of the particularity). Should i edit my question? ask new one? Aug 5, 2021 at 11:34
  • (1) For string replacement, \tl_replace_all:Nnn is much faster, and "replacing letters" is just string substitution; (2) composite glyphs, like o+^, are also strings, so you will need two checks: o+^>o and ô>o; (3) \regex_replace_all:nnN can be used for patterns, e.g., replace \r{A} - which typesets Å - with A, and the pattern could be: \r{*<any letter>*}. (4) For regex, as exercise, do a replace all \textit{...} with, say, {\itshape ...}; (5) Regex can do (ô) or (o+^) > os in 1 step, replace function would need 2 steps.
    – Cicada
    Aug 6, 2021 at 14:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .