6

I need to replace all characters given as argument with other characters, doing some sort of transliteration.

This answer provides a solution that works nicely with XeLaTeX and Unicode. Unfortunately it cannot replace within arguments of commands that occur within the \myreplace:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myreplace}{m}
 {
  \tl_set:Nn \l__maxd_argument_tl { #1 }
  \tl_replace_all:Nnn \l__maxd_argument_tl { e } { ẹ }
  \tl_replace_all:Nnn \l__maxd_argument_tl { E } { Ẹ }
  \tl_use:N \l__maxd_argument_tl
 }
\tl_new:N \l__maxd_argument_tl
\ExplSyntaxOff

\begin{document}

\myreplace{Ee} \myreplace{E\emph{e}}

\end{document}

A general solution for all text-commands would be fantastic, but solving it for \emph only would be fine for my purpose.

I'd also be interested to learn whether I could add some basic error-handling, e.g. by making the macro replace all characters other than the specified ones by something that's spotted easily, like "???".

0

2 Answers 2

6

regex_replace_all:nnN does work here:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myreplace}{m}
 {
  \tl_set:Nn \l__maxd_argument_tl { #1 }
  \regex_replace_all:nnN { e } { x } \l__maxd_argument_tl
  \regex_replace_all:nnN { E } { X } \l__maxd_argument_tl
  \tl_use:N \l__maxd_argument_tl
 }
\tl_new:N \l__maxd_argument_tl
\ExplSyntaxOff

\begin{document}

\myreplace{Ee} \myreplace{E\emph{e}}

\end{document}

To make it work for your special characters with pdfLaTeX one has to specify the correct category codes for the replacement (don't use this with LuaLaTeX or XeLaTeX -- it will throw an error):

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\myreplace}{m}
 {
  \tl_set:Nn \l__maxd_argument_tl { #1 }
  \regex_replace_all:nnN { e } { \cA(ẹ) } \l__maxd_argument_tl
  \regex_replace_all:nnN { E } { \cA(Ẹ) } \l__maxd_argument_tl
  \tl_use:N \l__maxd_argument_tl
 }
\tl_new:N \l__maxd_argument_tl
\ExplSyntaxOff

\begin{document}

\myreplace{Ee} \myreplace{E\emph{e}}

\end{document}
2
  • @Mico I thought OP was using XeLaTeX?! I just replaced it with x and X because my vim has a binding to pdflatex and none to xelatex. Your answer doesn't work with pdflatex, too :P
    – Skillmon
    Nov 27, 2017 at 1:07
  • 1
    @Mico see my edit for pdflatex compatibility.
    – Skillmon
    Nov 27, 2017 at 1:18
5

Here's a LuaLaTeX-based solution. It can handle utf8-encoded characters in the text substitution operation, and it can handle all embedded macro names, including \emph. (The only assumption is that argument of \myreplace contains no macros whose names contain the characters or to begin with. This shouldn't be a binding restriction, right?)

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode}
\begin{luacode*}
function myreplace ( s )
  s = unicode.utf8.gsub ( s, "E", "Ẹ" )
  s = unicode.utf8.gsub ( s, "e", "ẹ" )
  s = unicode.utf8.gsub ( s , "\\[%aẸẹ]+" , 
        function ( x )
           x = unicode.utf8.gsub ( x , "ẹ" , "e" )
           x = unicode.utf8.gsub ( x , "Ẹ" , "E" )
           return ( x )
        end )
  tex.sprint ( s )
end
\end{luacode*}
\newcommand\myreplace[1]{\directlua{myreplace(\luastringN{#1})}}

\begin{document}
\myreplace{Ee} \myreplace{E\textit{e}\emph{e}\textbf{E}}
\end{document}
0

You must log in to answer this question.

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